saravanakumar1987 opened a new pull request, #30218:
URL: https://github.com/apache/camel/pull/30218

   # Description
   
   Fixes [CAMEL-27033](https://issues.apache.org/jira/browse/CAMEL-27033), the 
follow-up that the review of apache/camel#30120 (CAMEL-27003) suggested.
   
   ## What was wrong
   
   `MNSEndpoint.initClient()` returned nothing. After it, `MNSProducer.process` 
and `MNSConsumer.poll` read the client again with `endpoint.getMnsClient()`. 
`MNSEndpoint.doStop` closes a client that the endpoint created and sets the 
field to `null`, under the lock of the endpoint, so a stop between `initClient` 
and that read gave `null`:
   
   - In `MNSProducer.process`, `sendMessage`, `deleteMessage` and 
`publishMessage` failed with a `NullPointerException`.
   - `MNSConsumer.poll` failed with a `NullPointerException` before it received 
a message.
   
   A client that is set or autowired is neither closed nor cleared by `doStop`, 
so only a client that the endpoint created was affected, and only by a stop 
between two statements of the same call.
   
   ## The change
   
   - `MNSEndpoint.initClient()` returns the client, in the form of 
`OSSEndpoint.initClient()`: it reads the field into a local variable and, when 
that is `null`, reads it again under the lock, creates the client only if it is 
still `null`, and returns the client that it read or created. Its Javadoc says 
that it returns the client. `getMnsClient()` stays.
   - The return type of the public method `initClient()` changes from `void` to 
`MNSClient`. A call that ignores the result compiles as before, but code 
compiled against an earlier version must be recompiled, and a subclass that 
overrides it must return the client. The upgrade guide of 4.24 has a section on 
it.
   - `MNSProducer.process` passes the returned client to `sendMessage`, 
`deleteMessage` and `publishMessage`, private methods that took the endpoint 
before.
   - `MNSConsumer.poll` gets the queue from the returned client.
   
   No option, header, default value or description text changes, and no 
generated file changes.
   
   ## Tests
   
   The new `MNSInitClientTest` uses mock clients only and contacts no server. 
In its four tests the endpoint creates the mock client in `initClient()`, but 
its `getMnsClient()` returns `null`, as it would after a stop between 
`initClient` and a later `getMnsClient()`:
   
   - `sendMessageUsesTheClientThatInitClientReturns`, 
`deleteMessageUsesTheClientThatInitClientReturns` and 
`publishMessageUsesTheClientThatInitClientReturns`: the producer sends, deletes 
and publishes with the client.
   - `pollUsesTheClientThatInitClientReturns`: a poll of a consumer whose 
scheduler does not start receives the message.
   
   `MNSEndpointTest#concurrentInitClientCreatesOneClient` now takes the client 
that `initClient` returns, instead of calling `getMnsClient()` after it.
   
   ## How it was verified
   
   - With the new test and without the fix (the three changed classes and 
`MNSEndpointTest` restored from `main`), `MNSInitClientTest` ran 4 tests with 4 
errors, each a `NullPointerException`: `Cannot invoke 
"com.aliyun.mns.client.MNSClient.getQueueRef(String)" because the return value 
of "org.apache.camel.component.alibaba.mns.MNSEndpoint.getMnsClient()" is null` 
for send, delete and poll, and the same with `getTopicRef(String)` for publish.
   - With the fix, the 4 tests and the 3 tests of `MNSEndpointTest` pass.
   - With the fix, `mvn install -nsu` in the module ran all 32 tests of the 
module with no failure, error or rerun, and changed no generated file.
   
   ## Not changed
   
   The three points below are about a client that the endpoint created. 
`doStop` does not close or clear a client that is set or autowired.
   
   - `MNSConsumer.processCommit` still reads the client of the endpoint with 
`getMnsClient()` when an exchange completes. After a stop it gets `null`, 
unless a call of `initClient` has created a new client. With `null`, it gives 
the `NullPointerException` to the exception handler and sends no request, so 
the message is not deleted. A delete with the client of the poll after the stop 
would use a client that `doStop` has closed, and aliyun-sdk-mns 2.0.0 does not 
refuse that: it starts a new HTTP client for the closed client, which nothing 
closes.
   - If the endpoint stops between `initClient` and the request, the producer 
or the poll sends that request with the closed client. Before this change, that 
was already so when the stop came after the read of `getMnsClient()`.
   - As before this change, a call of `initClient` after the endpoint has 
stopped creates a new client. Nothing closes it, unless the endpoint is started 
and stopped again.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [x] I have run `mvn clean install -Dquickly -DskipTests` from the root 
folder on upstream `main` (27efa340d2df, the parent of this commit), and then, 
with the change, `mvn install -nsu` in 
`components/camel-alibaba/camel-alibaba-mns`, which also runs its code 
generators, the formatter and the import sorter. It changed no generated file, 
so there are no auto-generated changes to commit.
   
   The commands that were run:
   
   ```
   # in components/camel-alibaba/camel-alibaba-mns, with the new test and 
without the fix
   mvn -o -B test -Dtest=MNSInitClientTest 
-Dsurefire.failIfNoSpecifiedTests=false -Dsurefire.rerunFailingTestsCount=0
   # with the fix
   mvn -o -B test -Dtest='MNSInitClientTest,MNSEndpointTest' 
-Dsurefire.failIfNoSpecifiedTests=false -Dsurefire.rerunFailingTestsCount=0
   mvn install -nsu
   ```
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
   
   The change, the tests and this text were drafted with Claude Code 
(Anthropic), from the ticket, the code of camel-alibaba-mns and 
camel-alibaba-oss, and the sources of aliyun-sdk-mns 2.0.0. The tests were run 
as described above. The change and the claims of this text were checked by 
independent AI reviews.
   
   _Claude Code on behalf of saravanakumar1987_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to