davsclaus commented on code in PR #27173:
URL: https://github.com/apache/camel/pull/27173#discussion_r4298281212


##########
components/camel-sjms/src/main/java/org/apache/camel/component/sjms/consumer/BatchMessageListenerContainer.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.sjms.consumer;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.locks.ReentrantLock;
+
+import jakarta.jms.JMSException;
+import jakarta.jms.MessageConsumer;
+import jakarta.jms.Session;
+
+import org.apache.camel.component.sjms.SjmsEndpoint;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BatchMessageListenerContainer extends 
SimpleMessageListenerContainer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(BatchMessageListenerContainer.class);
+
+    private final SjmsEndpoint endpoint;
+    private BatchEndpointMessageListener batchListener;
+    private ExecutorService workerExecutorService;
+    private final ReentrantLock workersLock = new ReentrantLock();
+    private final List<BatchConsumerWorker> workers = new ArrayList<>();
+
+    public BatchMessageListenerContainer(SjmsEndpoint endpoint) {
+        super(endpoint);
+        this.endpoint = endpoint;
+    }
+
+    public void setBatchListener(BatchEndpointMessageListener batchListener) {
+        this.batchListener = batchListener;
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        workerExecutorService = 
endpoint.getCamelContext().getExecutorServiceManager().newFixedThreadPool(
+                this, "SjmsBatchConsumer[" + endpoint.getDestinationName() + 
"]",
+                Math.max(1, this.getConcurrentConsumers()));
+
+        // triggers connection + session/consumer creation, calling 
configureConsumer() below
+        // for each session per concurrentConsumers, and re-invokes it again 
on reconnection
+        super.doStart();
+    }
+
+    private void addWorker(BatchConsumerWorker worker) {
+        workersLock.lock();
+        try {
+            workers.add(worker);
+        } finally {
+            workersLock.unlock();
+        }
+    }
+
+    private boolean removeWorker(BatchConsumerWorker worker) {
+        workersLock.lock();
+        try {
+            return workers.remove(worker);
+        } finally {
+            workersLock.unlock();
+        }
+    }
+
+    @Override
+    protected void configureConsumer(MessageConsumer consumer, Session 
session) {
+        BatchConsumerWorker worker = new BatchConsumerWorker(
+                this.endpoint, this.batchListener, session, consumer);
+        addWorker(worker);
+        CompletableFuture.runAsync(worker, workerExecutorService)
+                .whenComplete((v, ex) -> onWorkerExit(worker, ex));
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        shutdownWorkers();
+        // shutdown scheduled executor after all in-flight exchanges have 
completed
+        if (workerExecutorService != null) {
+            
getEndpoint().getCamelContext().getExecutorServiceManager().shutdownGraceful(workerExecutorService);
+            LOG.debug("BatchConsumerWorker executor service shutdown");
+            workerExecutorService = null;
+        }
+
+        super.doStop();
+    }
+
+    private void invalidateWorkers() {
+        workersLock.lock();
+        try {
+            for (BatchConsumerWorker worker : workers) {
+                worker.invalidate();
+            }
+            workers.clear();
+        } finally {
+            workersLock.unlock();
+        }
+    }
+
+    private void shutdownWorkers() {
+        workersLock.lock();
+        try {
+            for (BatchConsumerWorker worker : workers) {
+                worker.shutdown();
+            }
+        } finally {
+            workersLock.unlock();
+        }
+    }
+
+    private void onWorkerExit(BatchConsumerWorker worker, Throwable ex) {
+
+        if (!removeWorker(worker)) {
+            // already removed: a stop or a recovery is already in charge of 
this worker
+            return;
+        }
+
+        if (isStopping() || worker.isShutdownRequested()) {
+            return;
+        }
+
+        if (ex != null) {
+            Throwable cause = ex.getCause() != null ? ex.getCause() : ex;
+            LOG.warn("Batch consumer worker for {} exited unexpectedly, 
triggering recovery",
+                    endpoint.getEndpointUri(), cause);
+        }
+
+        invalidateWorkers();
+        invalidateConsumers();
+        scheduleConnectionRecovery();
+    }
+
+    @Override
+    public void onException(JMSException exception) {
+        invalidateWorkers();
+        super.onException(exception);
+    }

Review Comment:
   The merge of `main` changed how `SimpleMessageListenerContainer` recovers 
(CAMEL-25612, CAMEL-25659): an attempt can now be abandoned and tried again. 
`initConsumers()` closes the consumers it did create when a later one fails 
(L395-407), and `recoverConnection()` drops the consumers of a new connection 
that failed during the attempt without closing them (L224-233). Each of those 
consumers already has a worker from `configureConsumer()`, and nothing 
invalidates it: the `invalidateWorkers()` here runs before the attempt creates 
them, or in the middle of it.
   
   So these workers stay in `workers`. Once their consumer is closed (at once 
in the first case, by the `refreshConnection()` of the next attempt in the 
second), `receive()` throws an `IllegalStateException`, and `onWorkerExit` logs 
"exited unexpectedly, triggering recovery" and calls `invalidateWorkers()` and 
`invalidateConsumers()`. If that lands while the next attempt is creating its 
consumers, or just after it succeeded, it drops those as well and costs another 
recovery cycle.
   
   Invalidating the workers when an attempt does not succeed keeps them in step 
with the consumers, for example:
   
   ```java
   @Override
   protected boolean recoverConnection(BackgroundTask task) {
       boolean recovered = false;
       try {
           recovered = super.recoverConnection(task);
           return recovered;
       } finally {
           if (!recovered) {
               // the consumers of this attempt are dropped, and so are the 
workers it started for them
               invalidateWorkers();
           }
       }
   }
   ```
   
   `SjmsConsumerRecoveryFailedAttemptTest` (added by those two fixes) has the 
failure scenarios; a `batching=true` variant of 
`testRecoveryTriesAgainWhenConnectionFailsBeforeConsumersAreCreated` would 
cover this.



-- 
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