allthingssecurity opened a new pull request, #27856: URL: https://github.com/apache/camel/pull/27856
# Description [CAMEL-25659](https://issues.apache.org/jira/browse/CAMEL-25659) Follow-up of point 2 of the [review of #27799](https://github.com/apache/camel/pull/27799#pullrequestreview-5513241706) ([CAMEL-25612](https://issues.apache.org/jira/browse/CAMEL-25612), merged). Since CAMEL-25612, `SimpleMessageListenerContainer.recoverConnection` tries again when `onException` dropped the consumers while the attempt was creating them: `initConsumers` assigns the `sessions`/`consumers` sets before its loop, `onException` sets them to `null`, and the attempt checks `consumers == null` under `connectionLock` at the end. One window was left: an `onException` for the new connection that comes after `refreshConnection()` but before `initConsumers` assigns the sets. It sets fields to `null` that are already `null`, and it does not schedule another recovery because one is running. If the following create calls fail, the attempt fails and is repeated, as before. If the JMS provider still creates the sessions and consumers on the failed connection, the attempt reports success, and the container stays on a connection that delivers nothing until the next `onException`. Changes, all in `SimpleMessageListenerContainer`: - `onException` counts the failures (an `AtomicLong`), as its first statement. - The recovery attempt reads the count inside `refreshConnection`, after the old connection is closed and before the new one is created (so before the new connection has the container as its exception listener), and compares it under `connectionLock` at the end of the attempt, next to the existing `consumers == null` check. If it changed, the attempt sets the two sets to `null` (the consumers on the failed connection are closed with it by the next attempt) and returns `false`, so the recovery task runs again after `recoveryInterval`. - `refreshConnection()` keeps its signature (`protected final void`) and its behaviour; it calls a new private method that also returns the count. The count is read after the old connection is closed, not before the attempt, so that a provider that calls the exception listener when a connection is closed does not make every attempt fail. An `onException` of the old connection that is delivered late (after that point) costs one more attempt; nothing tells which connection an `onException` is for. No new retry loop and no new log: the extra attempts are those of the existing recovery task, after `recoveryInterval`, and the case logs at DEBUG like the `consumers == null` case. The count is never reset; only its change during an attempt is used. No upgrade guide entry: nothing a route can rely on changes. Tests: - New test in `SjmsConsumerRecoveryFailedAttemptTest`, `testRecoveryTriesAgainWhenConnectionFailsBeforeConsumersAreCreated`, with the tracking connection factory of the class (embedded Artemis, `concurrentConsumers=3`, `recoveryInterval=200`). After a simulated `onException`, the connection that the recovery creates calls its exception listener when it is started and does not start (it delivers nothing), while sessions and consumers can still be created on it. The test expects a third connection with 3 consumers, the failed one closed, and a message sent afterwards to be consumed. All waits are Awaitility with `atMost` or `MockEndpoint` assertions; no sleeps. - Without the change (main's `SimpleMessageListenerContainer`, same test) it fails: `connections: the recovery must try again, the connection failed before the consumers were created ==> expected: <3> but was: <2> within 20 seconds`. The other four tests of the class pass on main and with the change. - Removing the reset of the two sets in the new branch makes the new test fail (`consumers on the new connection ==> expected: <3> but was: <0>`): the next attempt would otherwise take the consumers of the failed connection for its own. - With the change: camel-sjms 139 tests, 0 failures, 5 skipped; camel-sjms2 (it uses the same container through `SjmsEndpoint`) 23 tests, 0 failures. Test reruns switched off. What was not run: no broker other than the embedded Artemis; the failure of the new connection is simulated by the connection factory of the test. Notes: - `ExclusiveQueueMessageListenerContainer` and `TemporaryQueueMessageListenerContainer` (the reply listeners of the InOut producer) do not override `onException` or `recoverConnection`, so they get the change too. - The open pull request #27173 (CAMEL-16039) changes `onException` in the same file (it moves the reset of the two sets into a protected `invalidateConsumers()`). The two merge without a conflict today, and its `BatchMessageListenerContainer.onException` calls `super.onException`, so its failures are counted. Two points for whichever lands second: its worker-exit path calls `invalidateConsumers()` and `scheduleConnectionRecovery()` directly, not `onException`, so it is not counted (it is not a connection failure); and the reset that this change adds in `recoverConnection` would then better call `invalidateConsumers()` too. # 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). [CAMEL-25659](https://issues.apache.org/jira/browse/CAMEL-25659), requested in the [review of #27799](https://github.com/apache/camel/pull/27799#pullrequestreview-5513241706). # Apache Camel coding standards and style - [x] I checked that each commit in the pull request has a meaningful subject line and body. - [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes. (I built and tested camel-sjms and camel-sjms2, including the formatter and import-sort plugins. I did not run the full root build.) # 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. This PR was prepared with Claude Code (Claude Opus 5.5). The commit carries a `Co-Authored-By` trailer. _Claude Code on behalf of allthingssecurity_ 🤖 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]
