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


##########
components/camel-sjms/src/main/java/org/apache/camel/component/sjms/consumer/BatchEndpointMessageListener.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * 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 jakarta.jms.Message;
+import jakarta.jms.Session;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.component.sjms.SjmsConstants;
+import org.apache.camel.component.sjms.SjmsConsumer;
+import org.apache.camel.component.sjms.SjmsEndpoint;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BatchEndpointMessageListener {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(BatchEndpointMessageListener.class);
+
+    private final SjmsConsumer consumer;
+    private final SjmsEndpoint endpoint;
+    private final Processor processor;
+
+    public BatchEndpointMessageListener(SjmsConsumer consumer, SjmsEndpoint 
endpoint, Processor processor) {
+        this.consumer = consumer;
+        this.endpoint = endpoint;
+        this.processor = processor;
+    }
+
+    private Exchange aggregate(List<Message> rawMessages, Session session) {
+        List<Exchange> exchanges = new ArrayList<>(rawMessages.size());
+        for (Message m : rawMessages) {
+            Exchange e = endpoint.createExchange(m, session);
+            // Force eager materialization of JMS message headers and body 
into the
+            // Camel Exchange, before the session is committed/closed after 
dispatch.
+            e.getIn().getHeaders();
+            e.getIn().getBody();
+
+            exchanges.add(e);
+        }
+
+        Exchange batchExchange = consumer.createExchange(false);
+        batchExchange.getIn().setBody(exchanges);
+        batchExchange.setProperty(SjmsConstants.JMS_SESSION, session);
+        
batchExchange.getMessage().setHeader(SjmsConstants.SJMS_BATCH_SIZE_HEADER, 
rawMessages.size());
+
+        return batchExchange;
+    }
+
+    void onBatch(List<Message> rawMessages, Session session) throws Exception {
+        LOG.trace("onBatch START");
+
+        LOG.debug("{} consumer received batch message: {}", endpoint, 
rawMessages);
+        Exchange batchExchange = null;
+        try {
+            batchExchange = aggregate(rawMessages, session);
+            processor.process(batchExchange);
+            consumer.releaseExchange(batchExchange, false);
+        } catch (Exception e) {
+            batchExchange.setException(e);
+        }
+
+        Exception exception = batchExchange.getException();
+
+        if (exception != null) {
+            LOG.trace("onBatch END throwing exception: {}", 
exception.getMessage());
+            throw exception;
+        }
+    }

Review Comment:
   This block still has three problems from my earlier reviews:
   - a rollback-only (or failed-without-exception) batch returns normally and 
is committed;
   - if `aggregate()` throws, `batchExchange` is null and the catch throws an 
NPE that hides the cause;
   - the exchange is released before `getException()` is read (and not at all 
on failure). With a pooled exchange factory, the release resets the exchange, 
so the exception is lost.
   
   One way to cover all three (needs `import 
org.apache.camel.RollbackExchangeException;`):
   
   ```suggestion
           Exchange batchExchange = aggregate(rawMessages, session);
           try {
               try {
                   processor.process(batchExchange);
               } catch (Exception e) {
                   batchExchange.setException(e);
               }
   
               Exception exception = batchExchange.getException();
               if (exception != null) {
                   LOG.trace("onBatch END throwing exception: {}", 
exception.getMessage());
                   throw exception;
               }
               if (batchExchange.isRollbackOnly() || batchExchange.isFailed()) {
                   LOG.debug("Batch exchange {} is marked rollback-only, 
rolling back the batch", batchExchange.getExchangeId());
                   throw new RollbackExchangeException("Batch marked as 
rollback-only", batchExchange);
               }
           } finally {
               consumer.releaseExchange(batchExchange, false);
           }
       }
   ```
   
   A test with `.rollback()` / `markRollbackOnly()` in the batching route that 
asserts the batch is redelivered would keep this from regressing.



##########
components/camel-sjms/src/main/java/org/apache/camel/component/sjms/consumer/BatchMessageListenerContainer.java:
##########
@@ -0,0 +1,156 @@
+/*
+ * 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<>();
+    private volatile boolean stopping;
+
+    public BatchMessageListenerContainer(SjmsEndpoint endpoint) {
+        super(endpoint);
+        this.endpoint = endpoint;
+        this.stopping = false;
+    }
+
+    public void setBatchListener(BatchEndpointMessageListener batchListener) {
+        this.batchListener = batchListener;
+    }
+
+    @Override
+    protected void doStart() throws Exception {

Review Comment:
   Still open from my previous review: `stopping` is set to `true` in 
`doStop()` and never reset. Since CAMEL-25312 the same container is started 
again after a route stop/start, so after a restart `onWorkerExit` always 
returns early and a worker that dies on a receive `JMSException` no longer 
triggers recovery.
   
   ```suggestion
       protected void doStart() throws Exception {
           stopping = false;
   ```
   
   The new `BatchConsumerStopStartTest` would be a good place for a test: 
stop/start the route, then force a connection loss and assert that the consumer 
recovers.



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