|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.camel.component.sjms.tx; |
| 18 | + |
| 19 | +import java.lang.management.ManagementFactory; |
| 20 | +import java.lang.management.ThreadInfo; |
| 21 | +import java.lang.management.ThreadMXBean; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +import org.apache.activemq.ActiveMQConnectionFactory; |
| 28 | +import org.apache.camel.CamelContext; |
| 29 | +import org.apache.camel.builder.RouteBuilder; |
| 30 | +import org.apache.camel.component.mock.MockEndpoint; |
| 31 | +import org.apache.camel.component.sjms.SjmsComponent; |
| 32 | +import org.apache.camel.test.junit4.CamelTestSupport; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +public class TransactedAsyncExceptionTest extends CamelTestSupport { |
| 36 | + |
| 37 | + private static final String BROKER_URI = "vm://tqc_test_broker?broker.persistent=false&broker.useJmx=false"; |
| 38 | + |
| 39 | + private static final int TRANSACTION_REDELIVERY_COUNT = 10; |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testRouteWithThread() throws Exception { |
| 43 | + String destination = "sjms:queue:async.exception"; |
| 44 | + |
| 45 | + context.addRoutes(new RouteBuilder() { |
| 46 | + @Override |
| 47 | + public void configure() throws Exception { |
| 48 | + AtomicInteger counter = new AtomicInteger(); |
| 49 | + |
| 50 | + from(destination + "?acknowledgementMode=SESSION_TRANSACTED&transacted=true") |
| 51 | + .threads() |
| 52 | + .process(exchange -> { |
| 53 | + if (counter.incrementAndGet() < TRANSACTION_REDELIVERY_COUNT) { |
| 54 | + throw new IllegalArgumentException(); |
| 55 | + } |
| 56 | + }) |
| 57 | + .to("mock:async.exception"); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + template.sendBody(destination, "begin"); |
| 62 | + |
| 63 | + MockEndpoint mockEndpoint = context.getEndpoint("mock:async.exception", MockEndpoint.class); |
| 64 | + |
| 65 | + mockEndpoint.expectedMessageCount(1); |
| 66 | + if (!mockEndpoint.await(getShutdownTimeout(), TimeUnit.SECONDS)) { |
| 67 | + dumpThreads(); |
| 68 | + } |
| 69 | + assertMockEndpointsSatisfied(getShutdownTimeout(), TimeUnit.SECONDS); |
| 70 | + } |
| 71 | + |
| 72 | + private void dumpThreads() { |
| 73 | + ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
| 74 | + for (ThreadInfo threadInfo : threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), Integer.MAX_VALUE)) { |
| 75 | + if (Thread.State.BLOCKED.equals(threadInfo.getThreadState())) { |
| 76 | + log.error("blocked thread: {}", threadInfo); |
| 77 | + } else { |
| 78 | + log.info("normal thread: {}", threadInfo); |
| 79 | + } |
| 80 | + log.info("full stack: {}", Arrays.stream(threadInfo.getStackTrace()).map(Object::toString).collect(Collectors.joining("\n\t"))); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected CamelContext createCamelContext() throws Exception { |
| 86 | + CamelContext camelContext = super.createCamelContext(); |
| 87 | + |
| 88 | + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URI); |
| 89 | + |
| 90 | + connectionFactory.getRedeliveryPolicy().setInitialRedeliveryDelay(0); |
| 91 | + connectionFactory.getRedeliveryPolicy().setRedeliveryDelay(0); |
| 92 | + connectionFactory.getRedeliveryPolicy().setUseCollisionAvoidance(false); |
| 93 | + connectionFactory.getRedeliveryPolicy().setUseExponentialBackOff(false); |
| 94 | + connectionFactory.getRedeliveryPolicy().setMaximumRedeliveries(TRANSACTION_REDELIVERY_COUNT); |
| 95 | + |
| 96 | + SjmsComponent component = new SjmsComponent(); |
| 97 | + component.setConnectionFactory(connectionFactory); |
| 98 | + camelContext.addComponent("sjms", component); |
| 99 | + |
| 100 | + return camelContext; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected int getShutdownTimeout() { |
| 105 | + return 2; |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments