gnodet-bot commented on code in PR #27173: URL: https://github.com/apache/camel/pull/27173#discussion_r4165109006
########## components/camel-sjms/src/main/java/org/apache/camel/component/sjms/consumer/BatchEndpointMessageListener.java: ########## @@ -0,0 +1,105 @@ +/* + * 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.List; + +import jakarta.jms.Message; +import jakarta.jms.Session; + +import org.apache.camel.AggregationStrategy; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.component.sjms.SjmsConstants; +import org.apache.camel.component.sjms.SjmsEndpoint; +import org.apache.camel.component.sjms.SjmsHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BatchEndpointMessageListener { + + public static final String SJMS_BATCH_SIZE_HEADER = "CamelSjmsBatchSize"; + + private static final Logger LOG = LoggerFactory.getLogger(BatchEndpointMessageListener.class); + + private final SjmsEndpoint endpoint; + private final Processor processor; + private final AggregationStrategy aggregationStrategy; + + public BatchEndpointMessageListener(SjmsEndpoint endpoint, Processor processor, AggregationStrategy aggregationStrategy) { + this.endpoint = endpoint; + this.processor = processor; + + this.aggregationStrategy = aggregationStrategy; + } + + private Exchange aggregate(List<Message> rawMessages, Session session) { + Exchange result = null; + for (Message m : rawMessages) { + Exchange e = endpoint.createExchange(m, session); + // Populate the headers and body of the Exchange in message + e.getIn().getHeaders(); + e.getIn().getBody(); + + result = aggregationStrategy.aggregate(result, e); + } + // 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(); + return result; Review Comment: 🔴 **Compilation error: `e` is not in scope.** These lines are outside the `for` loop (which closes at line 59). The variable `e` was declared inside the loop at line 53 and is not accessible here. This block is a leftover from a misapplied suggestion — the comment + eager-loading calls were supposed to replace lines 54-56 *inside* the loop, but instead got appended *after* the loop. The correct `aggregate()` method should look like: ```java private Exchange aggregate(List<Message> rawMessages, Session session) { Exchange result = null; 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(); result = aggregationStrategy.aggregate(result, e); } if (result != null) { aggregationStrategy.onCompletion(result); } return result; } ``` Note: `aggregationStrategy.onCompletion(result)` was deleted by commit 0a8c2b60 and is still missing — this is a behavioral regression. ########## components/camel-sjms/src/main/java/org/apache/camel/component/sjms/consumer/BatchEndpointMessageListener.java: ########## @@ -0,0 +1,105 @@ +/* + * 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.List; + +import jakarta.jms.Message; +import jakarta.jms.Session; + +import org.apache.camel.AggregationStrategy; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.component.sjms.SjmsConstants; +import org.apache.camel.component.sjms.SjmsEndpoint; +import org.apache.camel.component.sjms.SjmsHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BatchEndpointMessageListener { + + public static final String SJMS_BATCH_SIZE_HEADER = "CamelSjmsBatchSize"; + + private static final Logger LOG = LoggerFactory.getLogger(BatchEndpointMessageListener.class); + + private final SjmsEndpoint endpoint; + private final Processor processor; + private final AggregationStrategy aggregationStrategy; + + public BatchEndpointMessageListener(SjmsEndpoint endpoint, Processor processor, AggregationStrategy aggregationStrategy) { + this.endpoint = endpoint; + this.processor = processor; + + this.aggregationStrategy = aggregationStrategy; + } + + private Exchange aggregate(List<Message> rawMessages, Session session) { + Exchange result = null; + for (Message m : rawMessages) { + Exchange e = endpoint.createExchange(m, session); + // Populate the headers and body of the Exchange in message + e.getIn().getHeaders(); + e.getIn().getBody(); + + result = aggregationStrategy.aggregate(result, e); + } + // 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(); + return result; + } + + void onBatch(List<Message> rawMessages, Session session) throws Exception { + Exchange batchExchange = null; + Exception failure = null; + try { + batchExchange = aggregate(rawMessages, session); + if (batchExchange != null) { + batchExchange.setProperty(SjmsConstants.JMS_SESSION, session); + batchExchange.getMessage().setHeader(SJMS_BATCH_SIZE_HEADER, rawMessages.size()); + processor.process(batchExchange); + } + } catch (Exception e) { + failure = e; + } + + boolean failed = failure != null + || (batchExchange != null && (batchExchange.isFailed() || batchExchange.isRollbackOnly())); + Message lastMessage = rawMessages.get(rawMessages.size() - 1); + + if (!failed) { + SjmsHelper.commitIfNeeded(session, lastMessage); + } else { + if (rawMessages.isEmpty()) { + return; + } + Message lastMessage = rawMessages.get(rawMessages.size() - 1); + if (cause != null) { + if (!failed) { + SjmsHelper.commitIfNeeded(session, lastMessage); + } else { + Exception cause = failure != null ? failure : batchExchange.getException(); + if (cause != null) { + LOG.warn("Batch of {} message(s) failed processing on {}: {}", rawMessages.size(), + endpoint.getEndpointUri(), cause.getMessage(), cause); + } else { + LOG.warn("Batch of {} message(s) rolled back on {} (rollback-only or failed exchange)", + rawMessages.size(), endpoint.getEndpointUri()); + } + SjmsHelper.rollbackIfNeeded(session); + } Review Comment: 🔴 **Multiple compilation errors — entire block is corrupted.** This section has: 1. **Duplicate `if (!failed)` block** — lines 85-87 and 93-95 are the same check. The first one starts an else branch at line 87, then the code goes off the rails. 2. **Duplicate `Message lastMessage` declaration** — line 83 and line 91 both declare the same variable, which won't compile. 3. **`cause` used before declaration** — line 92 references `cause` but it's declared at line 96. 4. **Empty-list guard in wrong scope** — the `if (rawMessages.isEmpty()) return;` at line 88-90 is inside the `else` branch of `if (!failed)` — this only executes when the batch *failed*, which is nonsensical. 5. **Missing closing braces** — the class and method are not properly closed. The correct `onBatch()` method should be: ```java void onBatch(List<Message> rawMessages, Session session) throws Exception { if (rawMessages.isEmpty()) { return; } Exchange batchExchange = null; Exception failure = null; try { batchExchange = aggregate(rawMessages, session); if (batchExchange != null) { batchExchange.setProperty(SjmsConstants.JMS_SESSION, session); batchExchange.getMessage().setHeader(SJMS_BATCH_SIZE_HEADER, rawMessages.size()); processor.process(batchExchange); } } catch (Exception e) { failure = e; } boolean failed = failure != null || (batchExchange != null && (batchExchange.isFailed() || batchExchange.isRollbackOnly())); Message lastMessage = rawMessages.get(rawMessages.size() - 1); if (!failed) { SjmsHelper.commitIfNeeded(session, lastMessage); } else { Exception cause = failure != null ? failure : batchExchange.getException(); if (cause != null) { LOG.warn("Batch of {} message(s) failed processing on {}: {}", rawMessages.size(), endpoint.getEndpointUri(), cause.getMessage(), cause); } else { LOG.warn("Batch of {} message(s) rolled back on {} (rollback-only or failed exchange)", rawMessages.size(), endpoint.getEndpointUri()); } SjmsHelper.rollbackIfNeeded(session); } } ``` -- 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]
