Debian Codegraph @

activemq-spring/src/test/java/org/apache/bugs/AMQ1730Test.java

raw symbols SCIP
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
18 package org.apache.bugs;
19
20 import java.util.concurrent.CountDownLatch;
21
22 import javax.jms.Connection;
23 import javax.jms.ConnectionFactory;
24 import javax.jms.JMSException;
25 import javax.jms.Message;
26 import javax.jms.MessageListener;
27 import javax.jms.MessageProducer;
28 import javax.jms.Queue;
29 import javax.jms.Session;
30 import javax.jms.TextMessage;
31
32 import junit.framework.TestCase;
33
34 import org.apache.activemq.ActiveMQConnectionFactory;
35 import org.apache.activemq.broker.BrokerService;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.jms.listener.DefaultMessageListenerContainer;
39
40
41 public class AMQ1730Test extends TestCase {
42
43 private static final Logger log = LoggerFactory.getLogger(AMQ1730Test.class);
44 private static final String JMSX_DELIVERY_COUNT = "JMSXDeliveryCount";
45 BrokerService brokerService;
46
47 private static final int MESSAGE_COUNT = 250;
48
49 public AMQ1730Test() {
50 super();
51 }
52
53 @Override
54 protected void setUp() throws Exception {
55 super.setUp();
56
57 brokerService = new BrokerService();
58 brokerService.addConnector("tcp://localhost:0");
59 brokerService.setUseJmx(false);
60 brokerService.start();
61 }
62
63 @Override
64 protected void tearDown() throws Exception {
65 super.tearDown();
66 brokerService.stop();
67 brokerService.waitUntilStopped();
68 }
69
70 public void testRedelivery() throws Exception {
71
72 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
73 brokerService.getTransportConnectors().get(0).getConnectUri().toString() + "?jms.prefetchPolicy.queuePrefetch=100");
74
75 Connection connection = connectionFactory.createConnection();
76 connection.start();
77
78 Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
79 Queue queue = session.createQueue("queue.test");
80
81 MessageProducer producer = session.createProducer(queue);
82
83 for (int i = 0; i < MESSAGE_COUNT; i++) {
84 log.info("Sending message " + i);
85 TextMessage message = session.createTextMessage("Message " + i);
86 producer.send(message);
87 }
88
89 producer.close();
90 session.close();
91 connection.stop();
92 connection.close();
93
94 final CountDownLatch countDownLatch = new CountDownLatch(MESSAGE_COUNT);
95
96 final ValueHolder<Boolean> messageRedelivered = new ValueHolder<Boolean>(false);
97
98 DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
99 messageListenerContainer.setConnectionFactory(connectionFactory);
100 messageListenerContainer.setDestination(queue);
101 messageListenerContainer.setAutoStartup(false);
102 messageListenerContainer.setConcurrentConsumers(1);
103 messageListenerContainer.setMaxConcurrentConsumers(16);
104 messageListenerContainer.setMaxMessagesPerTask(10);
105 messageListenerContainer.setReceiveTimeout(10000);
106 messageListenerContainer.setRecoveryInterval(5000);
107 messageListenerContainer.setAcceptMessagesWhileStopping(false);
108 messageListenerContainer.setCacheLevel(DefaultMessageListenerContainer.CACHE_NONE);
109 messageListenerContainer.setSessionTransacted(false);
110 messageListenerContainer.setMessageListener(new MessageListener() {
111
112 @Override
113 public void onMessage(Message message) {
114 if (!(message instanceof TextMessage)) {
115 throw new RuntimeException();
116 }
117 try {
118 TextMessage textMessage = (TextMessage) message;
119 String text = textMessage.getText();
120 int messageDeliveryCount = message.getIntProperty(JMSX_DELIVERY_COUNT);
121 if (messageDeliveryCount > 1) {
122 messageRedelivered.set(true);
123 }
124 log.info("[Count down latch: " + countDownLatch.getCount() + "][delivery count: " + messageDeliveryCount + "] - " + "Received message with id: " + message.getJMSMessageID() + " with text: " + text);
125
126 } catch (JMSException e) {
127 e.printStackTrace();
128 }
129 finally {
130 countDownLatch.countDown();
131 }
132 }
133
134 });
135 messageListenerContainer.afterPropertiesSet();
136
137 messageListenerContainer.start();
138
139 countDownLatch.await();
140 messageListenerContainer.stop();
141 messageListenerContainer.destroy();
142
143 assertFalse("no message has redelivery > 1", messageRedelivered.get());
144 }
145
146 private class ValueHolder<T> {
147
148 private T value;
149
150 public ValueHolder(T value) {
151 super();
152 this.value = value;
153 }
154
155 void set(T value) {
156 this.value = value;
157 }
158
159 T get() {
160 return value;
161 }
162 }
163 }