Debian Codegraph @

activemq-spring/src/test/java/org/apache/bugs/AMQ2754Test.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 package org.apache.bugs;
18
19 import java.util.concurrent.Callable;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.ExecutorService;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.TimeUnit;
24
25 import javax.jms.JMSException;
26 import javax.jms.Message;
27 import javax.jms.MessageListener;
28 import javax.jms.Session;
29 import javax.jms.TextMessage;
30
31 import junit.framework.TestCase;
32
33 import org.apache.activemq.ActiveMQConnectionFactory;
34 import org.apache.activemq.broker.BrokerService;
35 import org.apache.activemq.command.ActiveMQQueue;
36 import org.apache.activemq.network.NetworkConnector;
37 import org.apache.activemq.pool.PooledConnectionFactory;
38 import org.apache.activemq.store.memory.MemoryPersistenceAdapter;
39 import org.springframework.jms.core.JmsTemplate;
40 import org.springframework.jms.core.MessageCreator;
41 import org.springframework.jms.listener.DefaultMessageListenerContainer;
42
43 public class AMQ2754Test extends TestCase {
44
45 public void testNetworkOfBrokers() throws Exception {
46 BrokerService brokerService1 = null;
47 BrokerService brokerService2 = null;
48
49 String broker1Uri;
50 String broker2Uri;
51
52 final int total = 100;
53 final CountDownLatch latch = new CountDownLatch(total);
54 final boolean conduitSubscriptions = true;
55 try {
56
57 {
58 brokerService1 = new BrokerService();
59 brokerService1.setBrokerName("consumer");
60 brokerService1.setUseJmx(false);
61 brokerService1.setPersistenceAdapter(new MemoryPersistenceAdapter());
62 broker1Uri = brokerService1.addConnector("tcp://0.0.0.0:0").getPublishableConnectString();
63 brokerService1.start();
64 }
65
66 {
67 brokerService2 = new BrokerService();
68 brokerService2.setBrokerName("producer");
69 brokerService2.setUseJmx(false);
70 brokerService2.setPersistenceAdapter(new MemoryPersistenceAdapter());
71 broker2Uri = brokerService2.addConnector("tcp://0.0.0.0:0").getPublishableConnectString();
72 NetworkConnector network2 = brokerService2.addNetworkConnector("static:("+broker1Uri+")");
73 network2.setName("network1");
74 network2.setDynamicOnly(true);
75 network2.setConduitSubscriptions(conduitSubscriptions);
76 network2.setNetworkTTL(3);
77 network2.setPrefetchSize(1);
78 brokerService2.start();
79 }
80
81 ExecutorService pool = Executors.newSingleThreadExecutor();
82
83 ActiveMQConnectionFactory connectionFactory1 =
84 new ActiveMQConnectionFactory("failover:("+broker1Uri+")");
85
86 connectionFactory1.setWatchTopicAdvisories(false);
87 final DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
88 container.setConnectionFactory(connectionFactory1);
89 container.setMaxConcurrentConsumers(10);
90 container.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
91 container.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER);
92 container.setDestination(new ActiveMQQueue("testingqueue"));
93 container.setMessageListener(new MessageListener() {
94 @Override
95 public void onMessage(Message message) {
96 latch.countDown();
97 }
98 });
99 container.setMaxMessagesPerTask(1);
100 container.afterPropertiesSet();
101 container.start();
102
103 final String finalBroker2Uri = broker2Uri;
104
105 pool.submit(new Callable<Object>() {
106 @Override
107 public Object call() throws Exception {
108 try {
109 final int batch = 10;
110 ActiveMQConnectionFactory connectionFactory2 =
111 new ActiveMQConnectionFactory("failover:("+finalBroker2Uri+")");
112 PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(connectionFactory2);
113 connectionFactory2.setWatchTopicAdvisories(false);
114 JmsTemplate template = new JmsTemplate(pooledConnectionFactory);
115 ActiveMQQueue queue = new ActiveMQQueue("testingqueue");
116 for(int b = 0; b < batch; b++) {
117 for(int i = 0; i < (total / batch); i++) {
118 final String id = ":batch=" + b + "i=" + i;
119 template.send(queue, new MessageCreator() {
120 @Override
121 public Message createMessage(Session session) throws JMSException {
122 TextMessage message = session.createTextMessage();
123 message.setText("Hello World!" + id);
124 return message;
125 }
126 });
127 }
128 // give spring time to scale back again
129 while(container.getActiveConsumerCount() > 1) {
130 System.out.println("active consumer count:" + container.getActiveConsumerCount());
131 System.out.println("concurrent consumer count: " + container.getConcurrentConsumers());
132 Thread.sleep(1000);
133 }
134 }
135 //pooledConnectionFactory.stop();
136 } catch(Throwable t) {
137 t.printStackTrace();
138 }
139 return null;
140 }
141 });
142
143 pool.shutdown();
144 pool.awaitTermination(10, TimeUnit.SECONDS);
145
146 int count = 0;
147
148 // give it 20 seconds
149 while(!latch.await(1, TimeUnit.SECONDS) && count++ < 20) {
150 System.out.println("count " + latch.getCount());
151 }
152
153 container.destroy();
154
155 } finally {
156 try { if(brokerService1 != null) {
157 brokerService1.stop();
158 }} catch(Throwable t) { t.printStackTrace(); }
159 try { if(brokerService2 != null) {
160 brokerService2.stop();
161 }} catch(Throwable t) { t.printStackTrace(); }
162 }
163
164 if(latch.getCount() > 0) {
165 fail("latch should have gone down to 0 but was " + latch.getCount());
166 }
167 }
168 }