gnodet commented on code in PR #27173: URL: https://github.com/apache/camel/pull/27173#discussion_r4238716315
########## components/camel-sjms/src/main/java/org/apache/camel/component/sjms/consumer/BatchEndpointMessageListener.java: ########## @@ -0,0 +1,111 @@ +/* + * 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.RollbackExchangeException; +import org.apache.camel.RuntimeCamelException; +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; + +import static org.apache.camel.RuntimeCamelException.wrapRuntimeCamelException; + +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.getIn().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; + RuntimeCamelException rce = null; + try { + batchExchange = aggregate(rawMessages, session); + try { + processor.process(batchExchange); + } catch (Exception e) { + batchExchange.setException(e); + } + + // same evaluation as EndpointMessageListenerAsyncCallback.done(), sync branch only + if (batchExchange.isRollbackOnly()) { + rce = wrapRuntimeCamelException(new RollbackExchangeException(batchExchange)); + } else if (batchExchange.isFailed()) { + rce = wrapRuntimeCamelException(batchExchange.getException()); + } + } catch (Exception e) { + // aggregate() failed, so there is no exchange to inspect + rce = wrapRuntimeCamelException(e); + } finally { + if (batchExchange != null) { + // only after the outcome has been extracted + consumer.releaseExchange(batchExchange, false); + } + } + + if (rce != null) { + LOG.trace("onBatch END throwing exception: {}", rce.getMessage()); + throw rce; + } + } + + // BatchEndpointMessageListener Review Comment: 💬 **Stale comment artifact.** `// BatchEndpointMessageListener` is a leftover file-label from a review snippet — it has no purpose in the source. ```suggestion ``` ########## components/camel-sjms/src/test/java/org/apache/camel/component/sjms/batch/BatchConsumerTransactedTest.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.batch; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.IntStream; + +import org.apache.camel.Processor; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.sjms.support.JmsTestSupport; +import org.junit.jupiter.api.Test; + +import static java.lang.String.format; +import static org.apache.camel.component.sjms.batch.BatchTestHelper.BATCH_ROUTEBUILDER_MOCK_FINISH; +import static org.apache.camel.component.sjms.batch.BatchTestHelper.BATCH_ROUTEBUILDER_MOCK_START; +import static org.apache.camel.component.sjms.batch.BatchTestHelper.assertBatchSizesInOrder; +import static org.apache.camel.component.sjms.batch.BatchTestHelper.createBatchRoute; +import static org.apache.camel.component.sjms.batch.BatchTestHelper.getBatchBodiesAsString; +import static org.apache.camel.component.sjms.batch.BatchTestHelper.sendMessagesWithText; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BatchConsumerTransactedTest extends JmsTestSupport { + + private static final String QUEUE_NAME_TEMPLATE = "batch.consumer.%s.BatchConsumerTransactedTest"; + + private static final String ROUTE_ID_SESSION_TX = "tx"; + private static final String ROUTE_ID_CLIENT_ACK_NO_TX = "no-tx-client-ack"; + private static final String ROUTE_ID_AUTO_ACK_NO_TX = "no-tx-auto"; + + private static final String MESSAGE_TEXT = "Message %d"; + + @Test + public void testSessionTransacted() throws Exception { + MockEndpoint mockStart = getMockEndpoint(format(BATCH_ROUTEBUILDER_MOCK_START, ROUTE_ID_SESSION_TX)); + mockStart.expectedMessageCount(2); + + MockEndpoint mockFinish = getMockEndpoint(format(BATCH_ROUTEBUILDER_MOCK_FINISH, ROUTE_ID_SESSION_TX)); + mockFinish.expectedMessageCount(1); + + sendMessagesWithText(template, format("sjms:queue:" + QUEUE_NAME_TEMPLATE, ROUTE_ID_SESSION_TX), + 5, MESSAGE_TEXT); + + MockEndpoint.assertIsSatisfied(context); + assertBatchSizesInOrder(mockFinish, 5); + assertEquals( + IntStream.rangeClosed(1, 5) + .mapToObj(i -> String.format(MESSAGE_TEXT, i)) + .toList(), + getBatchBodiesAsString(mockFinish.getExchanges().get(0))); + } + + @Test + public void testClientAcknowledgedNotTransacted() throws Exception { + MockEndpoint mockStart = getMockEndpoint(format(BATCH_ROUTEBUILDER_MOCK_START, ROUTE_ID_CLIENT_ACK_NO_TX)); + mockStart.expectedMessageCount(2); + + MockEndpoint mockFinish = getMockEndpoint(format(BATCH_ROUTEBUILDER_MOCK_FINISH, ROUTE_ID_CLIENT_ACK_NO_TX)); + mockFinish.expectedMessageCount(1); + + sendMessagesWithText(template, + format("sjms:queue:" + QUEUE_NAME_TEMPLATE, ROUTE_ID_CLIENT_ACK_NO_TX), + 5, MESSAGE_TEXT); + + MockEndpoint.assertIsSatisfied(context); + assertBatchSizesInOrder(mockFinish, 5); + assertEquals( + IntStream.rangeClosed(1, 5) + .mapToObj(i -> String.format(MESSAGE_TEXT, i)) + .toList(), + getBatchBodiesAsString(mockFinish.getExchanges().get(0))); + } + + @Test + public void testAutoAcknowledgedNotTransacted() throws Exception { + MockEndpoint mockStart = getMockEndpoint(format(BATCH_ROUTEBUILDER_MOCK_START, ROUTE_ID_AUTO_ACK_NO_TX)); + mockStart.expectedMessageCount(1); + + MockEndpoint mockFinish = getMockEndpoint(format(BATCH_ROUTEBUILDER_MOCK_FINISH, ROUTE_ID_AUTO_ACK_NO_TX)); + mockFinish.expectedMessageCount(0); + + sendMessagesWithText(template, + format("sjms:queue:" + QUEUE_NAME_TEMPLATE, ROUTE_ID_AUTO_ACK_NO_TX), 5, MESSAGE_TEXT); + + MockEndpoint.assertIsSatisfied(context); + } + + @Override + protected RoutesBuilder[] createRouteBuilders() { + return new RoutesBuilder[] { + createBatchRoute(QUEUE_NAME_TEMPLATE, ROUTE_ID_SESSION_TX, 5, + 1000, + true, null, 1, new ThrowExceptionProcessor()), + createBatchRoute(QUEUE_NAME_TEMPLATE, ROUTE_ID_CLIENT_ACK_NO_TX, + 5, 1000, false, "CLIENT_ACKNOWLEDGE", 1, new ThrowExceptionProcessor()), + createBatchRoute(QUEUE_NAME_TEMPLATE, ROUTE_ID_AUTO_ACK_NO_TX, 5, 1000, false, + "AUTO_ACKNOWLEDGE", 1, new ThrowExceptionProcessor()) + }; + } + + private static class ThrowExceptionProcessor implements Processor { + private final AtomicInteger counter = new AtomicInteger(); + + @Override + public void process(org.apache.camel.Exchange exchange) { Review Comment: 💬 **FQN not needed — add import instead.** `org.apache.camel.Exchange` is fully qualified here. Add `import org.apache.camel.Exchange;` at the top of the file and use the short name: ```suggestion public void process(Exchange exchange) { ``` ########## components/camel-sjms/src/test/java/org/apache/camel/component/sjms/batch/BatchConsumerRollbackOnlyTest.java: ########## @@ -0,0 +1,94 @@ +/* + * 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.batch; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.camel.Processor; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.sjms.support.JmsTestSupport; +import org.junit.jupiter.api.Test; + +import static java.lang.String.format; +import static org.apache.camel.component.sjms.batch.BatchTestHelper.BATCH_ROUTEBUILDER_MOCK_FINISH; +import static org.apache.camel.component.sjms.batch.BatchTestHelper.BATCH_ROUTEBUILDER_MOCK_START; +import static org.apache.camel.component.sjms.batch.BatchTestHelper.assertBatchSizesInOrder; +import static org.apache.camel.component.sjms.batch.BatchTestHelper.createBatchRoute; +import static org.apache.camel.component.sjms.batch.BatchTestHelper.sendMessages; + +public class BatchConsumerRollbackOnlyTest extends JmsTestSupport { + + private static final String QUEUE_NAME_TEMPLATE = "batch.consumer.%s.BatchConsumerRollbackOnlyTest"; + + private static final String ROUTE_ID_SESSION_TX = "tx"; + private static final String ROUTE_ID_CLIENT_ACK_NO_TX = "no-tx-client-ack"; + + @Test + public void testSessionTransacted() throws Exception { + MockEndpoint mockStart = getMockEndpoint(format(BATCH_ROUTEBUILDER_MOCK_START, ROUTE_ID_SESSION_TX)); + mockStart.expectedMessageCount(2); + + MockEndpoint mockFinish = getMockEndpoint(format(BATCH_ROUTEBUILDER_MOCK_FINISH, ROUTE_ID_SESSION_TX)); + mockFinish.expectedMessageCount(1); + + sendMessages(template, format("sjms:queue:" + QUEUE_NAME_TEMPLATE, ROUTE_ID_SESSION_TX), + 5); + + MockEndpoint.assertIsSatisfied(context); + assertBatchSizesInOrder(mockFinish, 5); + } + + @Test + public void testClientAcknowledgedNotTransacted() throws Exception { + MockEndpoint mockStart = getMockEndpoint(format(BATCH_ROUTEBUILDER_MOCK_START, ROUTE_ID_CLIENT_ACK_NO_TX)); + mockStart.expectedMessageCount(2); + + MockEndpoint mockFinish = getMockEndpoint(format(BATCH_ROUTEBUILDER_MOCK_FINISH, ROUTE_ID_CLIENT_ACK_NO_TX)); + mockFinish.expectedMessageCount(1); + + sendMessages(template, + format("sjms:queue:" + QUEUE_NAME_TEMPLATE, ROUTE_ID_CLIENT_ACK_NO_TX), + 5); + + MockEndpoint.assertIsSatisfied(context); + assertBatchSizesInOrder(mockFinish, 5); + } + + @Override + protected RoutesBuilder[] createRouteBuilders() { + return new RoutesBuilder[] { + createBatchRoute(QUEUE_NAME_TEMPLATE, ROUTE_ID_SESSION_TX, 5, + 1000, + true, null, 1, new MarkRollBackProcessor()), + createBatchRoute(QUEUE_NAME_TEMPLATE, ROUTE_ID_CLIENT_ACK_NO_TX, + 5, 1000, false, "CLIENT_ACKNOWLEDGE", 1, new MarkRollBackProcessor()) + }; + } + + private static class MarkRollBackProcessor implements Processor { + private final AtomicInteger counter = new AtomicInteger(); + + @Override + public void process(org.apache.camel.Exchange exchange) { Review Comment: 💬 **FQN not needed — add import instead.** `org.apache.camel.Exchange` is fully qualified here. Add `import org.apache.camel.Exchange;` at the top of the file and use the short name: ```suggestion public void process(Exchange exchange) { ``` ########## components/camel-sjms2/src/test/java/org/apache/camel/component/sjms2/batch/BatchConsumerTransactedTest.java: ########## @@ -0,0 +1,175 @@ +/* + * 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.sjms2.batch; + +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import jakarta.jms.ConnectionFactory; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.sjms2.support.Jms2TestSupport; +import org.apache.camel.test.infra.artemis.services.ArtemisService; +import org.apache.camel.test.infra.artemis.services.ArtemisServiceFactory; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import static java.lang.String.format; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class BatchConsumerTransactedTest extends Jms2TestSupport { + + private static final String QUEUE_NAME_TEMPLATE = "sjms2.batch.consumer.%s.BatchConsumerTransactedTest"; + + static final String MOCK_START = "mock:%s.start"; + static final String MOCK_FINISH = "mock:%s.complete"; + + private static final String ROUTE_ID_SESSION_TX = "tx"; + private static final String ROUTE_ID_CLIENT_ACK_NO_TX = "no-tx-client-ack"; + private static final String ROUTE_ID_AUTO_ACK_NO_TX = "no-tx-auto"; + + private static final String MESSAGE_TEXT = "Message %d"; + + @RegisterExtension + public static ArtemisService service = ArtemisServiceFactory.createTCPAllProtocolsService(); + + @Test + public void testClientAcknowledgedNotTransacted() throws Exception { + MockEndpoint mockStart = getMockEndpoint(format(MOCK_START, ROUTE_ID_CLIENT_ACK_NO_TX)); + mockStart.expectedMessageCount(2); + + MockEndpoint mockFinish = getMockEndpoint(format(MOCK_FINISH, ROUTE_ID_CLIENT_ACK_NO_TX)); + mockFinish.expectedMessageCount(1); + + for (int i = 1; i <= 5; i++) { + template.sendBody("sjms2:queue:" + format(QUEUE_NAME_TEMPLATE, ROUTE_ID_CLIENT_ACK_NO_TX), + format(MESSAGE_TEXT, i)); + } + + MockEndpoint.assertIsSatisfied(context); + + List<Exchange> batch = mockFinish.getExchanges().get(0).getMessage().getBody(List.class); + assertNotNull(batch); + assertEquals(5, batch.size()); + + int messageNumber = 1; + for (Exchange batchExchange : batch) { + assertEquals( + String.format(MESSAGE_TEXT, messageNumber++), + batchExchange.getMessage().getBody(String.class)); + } + } + + @Test + public void testSessionTransacted() throws Exception { + MockEndpoint mockStart = getMockEndpoint(format(MOCK_START, ROUTE_ID_SESSION_TX)); + mockStart.expectedMessageCount(2); + + MockEndpoint mockFinish = getMockEndpoint(format(MOCK_FINISH, ROUTE_ID_SESSION_TX)); + mockFinish.expectedMessageCount(1); + + for (int i = 1; i <= 5; i++) { + template.sendBody("sjms2:queue:" + format(QUEUE_NAME_TEMPLATE, ROUTE_ID_SESSION_TX), + format(MESSAGE_TEXT, i)); + } + + MockEndpoint.assertIsSatisfied(context); + + List<Exchange> batch = mockFinish.getExchanges().get(0).getMessage().getBody(List.class); + assertNotNull(batch); + assertEquals(5, batch.size()); + + int messageNumber = 1; + for (Exchange batchExchange : batch) { + assertEquals( + String.format(MESSAGE_TEXT, messageNumber++), + batchExchange.getMessage().getBody(String.class)); + } + } + + @Test + public void testAutoAcknowledgedNotTransacted() throws Exception { + MockEndpoint mockStart = getMockEndpoint(format(MOCK_START, ROUTE_ID_AUTO_ACK_NO_TX)); + mockStart.expectedMessageCount(1); + + MockEndpoint mockFinish = getMockEndpoint(format(MOCK_FINISH, ROUTE_ID_AUTO_ACK_NO_TX)); + mockFinish.expectedMessageCount(0); + + for (int i = 1; i <= 5; i++) { + template.sendBody("sjms2:queue:" + format(QUEUE_NAME_TEMPLATE, ROUTE_ID_AUTO_ACK_NO_TX), + format(MESSAGE_TEXT, i)); + } + + MockEndpoint.assertIsSatisfied(context); + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + fromF("sjms2:%s?batching=true&batchSize=5&batchInterval=10000&transacted=%s&acknowledgementMode=%s", + format(QUEUE_NAME_TEMPLATE, ROUTE_ID_AUTO_ACK_NO_TX), + false, + "AUTO_ACKNOWLEDGE") + .routeId(ROUTE_ID_AUTO_ACK_NO_TX) + .toF(MOCK_START, ROUTE_ID_AUTO_ACK_NO_TX) + .process(new ThrowExceptionProcessor()) + .toF(MOCK_FINISH, ROUTE_ID_AUTO_ACK_NO_TX); + + fromF("sjms2:%s?batching=true&batchSize=5&batchInterval=10000&transacted=%s&acknowledgementMode=%s", + format(QUEUE_NAME_TEMPLATE, ROUTE_ID_SESSION_TX), + true, + "SESSION_TRANSACTED") + .routeId(ROUTE_ID_SESSION_TX) + .toF(MOCK_START, ROUTE_ID_SESSION_TX) + .process(new ThrowExceptionProcessor()) + .toF(MOCK_FINISH, ROUTE_ID_SESSION_TX); + + fromF("sjms2:%s?batching=true&batchSize=5&batchInterval=10000&transacted=%s&acknowledgementMode=%s", + format(QUEUE_NAME_TEMPLATE, ROUTE_ID_CLIENT_ACK_NO_TX), + false, + "CLIENT_ACKNOWLEDGE") + .routeId(ROUTE_ID_CLIENT_ACK_NO_TX) + .toF(MOCK_START, ROUTE_ID_CLIENT_ACK_NO_TX) + .process(new ThrowExceptionProcessor()) + .toF(MOCK_FINISH, ROUTE_ID_CLIENT_ACK_NO_TX); + } + }; + } + + protected ConnectionFactory getConnectionFactory() throws Exception { + return getConnectionFactory(service.serviceAddress()); + } + + private static class ThrowExceptionProcessor implements Processor { + private final AtomicInteger counter = new AtomicInteger(); + + @Override + public void process(org.apache.camel.Exchange exchange) { Review Comment: 💬 **FQN redundant — `Exchange` is already imported at line 24.** ```suggestion public void process(Exchange exchange) { ``` -- 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]
