This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch 2.13.x in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit 873bd9448f52d674f298ee1e19575bd453b7f711 Author: Zheng Feng <[email protected]> AuthorDate: Sat Nov 5 08:41:31 2022 +0800 Fix #4250 to add JmsArtemisXATest (#4251) --- .../jms/artemis/it/JmsArtemisResource.java | 39 +++++++++++ .../jms/artemis/it/JmsArtemisXAEnabled.java} | 44 ++++-------- .../component/jms/artemis/it/JmsArtemisXAIT.java} | 37 ++-------- .../jms/artemis/it/JmsArtemisXATest.java} | 53 +++++++------- .../quarkus/messaging/jms/DummyXAResource.java | 81 ++++++++++++++++++++++ .../camel/quarkus/messaging/jms/JmsRoutes.java | 24 +++++++ 6 files changed, 185 insertions(+), 93 deletions(-) diff --git a/integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java b/integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java index 3503e828c2..186ef8f8bc 100644 --- a/integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java +++ b/integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java @@ -25,8 +25,11 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; +import org.apache.camel.CamelContext; +import org.apache.camel.CamelExecutionException; import org.apache.camel.Produce; import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.mock.MockEndpoint; @ApplicationScoped @Path("/messaging/jms/artemis") @@ -38,6 +41,12 @@ public class JmsArtemisResource { @Produce("jms:queue:pojoProduce") ProducerTemplate pojoProducer; + @Inject + CamelContext context; + + @Inject + ProducerTemplate producerTemplate; + @GET @Path("/connection/factory") @Produces(MediaType.TEXT_PLAIN) @@ -50,4 +59,34 @@ public class JmsArtemisResource { public void pojoProducer(String message) { pojoProducer.sendBody(message); } + + @POST + @Path("/xa") + public String testXA(String message) throws Exception { + MockEndpoint mockEndpoint = context.getEndpoint("mock:xaResult", MockEndpoint.class); + + mockEndpoint.reset(); + if (isValid(message)) { + mockEndpoint.expectedMessageCount(1); + } else { + mockEndpoint.expectedMessageCount(0); + } + + try { + producerTemplate.sendBody("direct:xa", message); + } catch (CamelExecutionException e) { + // ignore the exception and we will check the mock:xaResult + } + mockEndpoint.assertIsSatisfied(5000); + + if (isValid(message)) { + return mockEndpoint.getExchanges().get(0).getIn().getBody(String.class); + } else { + return "rollback"; + } + } + + private boolean isValid(String message) { + return !message.startsWith("fail"); + } } diff --git a/integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java b/integration-tests/jms-artemis-client/src/test/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisXAEnabled.java similarity index 50% copy from integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java copy to integration-tests/jms-artemis-client/src/test/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisXAEnabled.java index 3503e828c2..de4f6f494d 100644 --- a/integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java +++ b/integration-tests/jms-artemis-client/src/test/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisXAEnabled.java @@ -14,40 +14,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.quarkus.component.jms.artemis.it; - -import javax.enterprise.context.ApplicationScoped; -import javax.inject.Inject; -import javax.jms.ConnectionFactory; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; - -import org.apache.camel.Produce; -import org.apache.camel.ProducerTemplate; -@ApplicationScoped -@Path("/messaging/jms/artemis") -public class JmsArtemisResource { - - @Inject - ConnectionFactory connectionFactory; +package org.apache.camel.quarkus.component.jms.artemis.it; - @Produce("jms:queue:pojoProduce") - ProducerTemplate pojoProducer; +import java.util.HashMap; +import java.util.Map; - @GET - @Path("/connection/factory") - @Produces(MediaType.TEXT_PLAIN) - public String connectionFactoryImplementation() { - return connectionFactory.getClass().getName(); - } +import io.quarkus.test.junit.QuarkusTestProfile; - @POST - @Path("/pojo/producer") - public void pojoProducer(String message) { - pojoProducer.sendBody(message); +public class JmsArtemisXAEnabled implements QuarkusTestProfile { + @Override + public Map<String, String> getConfigOverrides() { + Map<String, String> props = new HashMap<>(); + props.put("quarkus.pooled-jms.pooling.enabled", "true"); + props.put("quarkus.pooled-jms.xa.enabled", "true"); + props.put("quarkus.transaction-manager.enable-recovery", "true"); + props.put("quarkus.pooled-jms.max-connections", "8"); + return props; } } diff --git a/integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java b/integration-tests/jms-artemis-client/src/test/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisXAIT.java similarity index 50% copy from integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java copy to integration-tests/jms-artemis-client/src/test/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisXAIT.java index 3503e828c2..0e544e3271 100644 --- a/integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java +++ b/integration-tests/jms-artemis-client/src/test/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisXAIT.java @@ -14,40 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.quarkus.component.jms.artemis.it; - -import javax.enterprise.context.ApplicationScoped; -import javax.inject.Inject; -import javax.jms.ConnectionFactory; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; - -import org.apache.camel.Produce; -import org.apache.camel.ProducerTemplate; -@ApplicationScoped -@Path("/messaging/jms/artemis") -public class JmsArtemisResource { - - @Inject - ConnectionFactory connectionFactory; - - @Produce("jms:queue:pojoProduce") - ProducerTemplate pojoProducer; +package org.apache.camel.quarkus.component.jms.artemis.it; - @GET - @Path("/connection/factory") - @Produces(MediaType.TEXT_PLAIN) - public String connectionFactoryImplementation() { - return connectionFactory.getClass().getName(); - } +import io.quarkus.test.junit.QuarkusIntegrationTest; - @POST - @Path("/pojo/producer") - public void pojoProducer(String message) { - pojoProducer.sendBody(message); - } +@QuarkusIntegrationTest +public class JmsArtemisXAIT extends JmsArtemisXATest { } diff --git a/integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java b/integration-tests/jms-artemis-client/src/test/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisXATest.java similarity index 50% copy from integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java copy to integration-tests/jms-artemis-client/src/test/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisXATest.java index 3503e828c2..03391d9f70 100644 --- a/integration-tests/jms-artemis-client/src/main/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisResource.java +++ b/integration-tests/jms-artemis-client/src/test/java/org/apache/camel/quarkus/component/jms/artemis/it/JmsArtemisXATest.java @@ -16,38 +16,33 @@ */ package org.apache.camel.quarkus.component.jms.artemis.it; -import javax.enterprise.context.ApplicationScoped; -import javax.inject.Inject; -import javax.jms.ConnectionFactory; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.RestAssured; +import org.junit.jupiter.api.Test; -import org.apache.camel.Produce; -import org.apache.camel.ProducerTemplate; +import static org.hamcrest.core.Is.is; -@ApplicationScoped -@Path("/messaging/jms/artemis") -public class JmsArtemisResource { - - @Inject - ConnectionFactory connectionFactory; - - @Produce("jms:queue:pojoProduce") - ProducerTemplate pojoProducer; - - @GET - @Path("/connection/factory") - @Produces(MediaType.TEXT_PLAIN) - public String connectionFactoryImplementation() { - return connectionFactory.getClass().getName(); +@QuarkusTest +@TestProfile(JmsArtemisXAEnabled.class) +public class JmsArtemisXATest { + @Test + public void testJmsXACommit() { + RestAssured.given() + .body("commit") + .post("/messaging/jms/artemis/xa") + .then() + .statusCode(200) + .body(is("commit")); } - @POST - @Path("/pojo/producer") - public void pojoProducer(String message) { - pojoProducer.sendBody(message); + @Test + public void testJmsXARollback() { + RestAssured.given() + .body("fail") + .post("/messaging/jms/artemis/xa") + .then() + .statusCode(200) + .body(is("rollback")); } } diff --git a/integration-tests/messaging/jms/src/main/java/org/apache/camel/quarkus/messaging/jms/DummyXAResource.java b/integration-tests/messaging/jms/src/main/java/org/apache/camel/quarkus/messaging/jms/DummyXAResource.java new file mode 100644 index 0000000000..c1a98e8c10 --- /dev/null +++ b/integration-tests/messaging/jms/src/main/java/org/apache/camel/quarkus/messaging/jms/DummyXAResource.java @@ -0,0 +1,81 @@ +/* + * 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.quarkus.messaging.jms; + +import javax.transaction.xa.XAException; +import javax.transaction.xa.XAResource; +import javax.transaction.xa.Xid; + +import org.jboss.logging.Logger; + +public class DummyXAResource implements XAResource { + private static final Logger LOG = Logger.getLogger(DummyXAResource.class); + + @Override + public void commit(Xid xid, boolean b) throws XAException { + LOG.info("DummyXAResource commit " + xid); + } + + @Override + public void end(Xid xid, int i) throws XAException { + } + + @Override + public void forget(Xid xid) throws XAException { + + } + + @Override + public int getTransactionTimeout() throws XAException { + return 0; + } + + @Override + public boolean isSameRM(XAResource xaResource) throws XAException { + if (!(xaResource instanceof DummyXAResource)) { + return false; + } else { + return this.equals(xaResource); + } + } + + @Override + public int prepare(Xid xid) throws XAException { + return XA_OK; + } + + @Override + public Xid[] recover(int i) throws XAException { + return new Xid[0]; + } + + @Override + public void rollback(Xid xid) throws XAException { + LOG.info("DummyXAResource rollback " + xid); + } + + @Override + public boolean setTransactionTimeout(int i) throws XAException { + return true; + } + + @Override + public void start(Xid xid, int i) throws XAException { + + } +} diff --git a/integration-tests/messaging/jms/src/main/java/org/apache/camel/quarkus/messaging/jms/JmsRoutes.java b/integration-tests/messaging/jms/src/main/java/org/apache/camel/quarkus/messaging/jms/JmsRoutes.java index 7c2d2d2d23..9c62ecb784 100644 --- a/integration-tests/messaging/jms/src/main/java/org/apache/camel/quarkus/messaging/jms/JmsRoutes.java +++ b/integration-tests/messaging/jms/src/main/java/org/apache/camel/quarkus/messaging/jms/JmsRoutes.java @@ -18,6 +18,7 @@ package org.apache.camel.quarkus.messaging.jms; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; +import javax.transaction.TransactionManager; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.quarkus.component.messaging.it.util.scheme.ComponentScheme; @@ -28,6 +29,9 @@ public class JmsRoutes extends RouteBuilder { @Inject ComponentScheme componentScheme; + @Inject + TransactionManager transactionManager; + @Override public void configure() throws Exception { @@ -40,5 +44,25 @@ public class JmsRoutes extends RouteBuilder { from("direct:computedDestination") .bean("destinationHeaderSetter") .toF("%s:queue:override", componentScheme); + + fromF("%s:queue:xa", componentScheme) + .log("Received message ${body}") + .to("mock:xaResult"); + + from("direct:xa") + .transacted() + .process(x -> { + transactionManager.getTransaction().enlistResource(new DummyXAResource()); + }) + .toF("%s:queue:xa?disableReplyTo=true", componentScheme) + .choice() + .when(body().startsWith("fail")) + .log("Forced to rollback") + .process(x -> { + transactionManager.setRollbackOnly(); + }) + .otherwise() + .log("Message added: ${body}") + .endChoice(); } }
