KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > usecases > AMQStackOverFlowTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.usecases;
20
21 import java.net.URI JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import javax.jms.JMSException JavaDoc;
24 import javax.jms.Message JavaDoc;
25 import javax.jms.Session JavaDoc;
26 import javax.jms.TextMessage JavaDoc;
27 import junit.framework.Assert;
28 import junit.framework.TestCase;
29 import org.apache.activemq.ActiveMQConnectionFactory;
30 import org.apache.activemq.broker.BrokerService;
31 import org.apache.activemq.broker.TransportConnector;
32 import org.apache.activemq.broker.region.policy.PolicyEntry;
33 import org.apache.activemq.broker.region.policy.PolicyMap;
34 import org.apache.activemq.memory.UsageManager;
35 import org.apache.activemq.network.DiscoveryNetworkConnector;
36 import org.apache.activemq.network.NetworkConnector;
37 import org.springframework.jms.core.JmsTemplate;
38 import org.springframework.jms.core.MessageCreator;
39
40 public class AMQStackOverFlowTest extends TestCase {
41
42     private static final String JavaDoc URL1 = "tcp://localhost:61616";
43
44     private static final String JavaDoc URL2 = "tcp://localhost:61617";
45
46     public void testStackOverflow() throws Exception JavaDoc {
47         BrokerService brokerService1 = null;
48         BrokerService brokerService2 = null;
49
50         try {
51             brokerService1 = createBrokerService("broker1", URL1, URL2);
52             brokerService1.start();
53             brokerService2 = createBrokerService("broker2", URL2, URL1);
54             brokerService2.start();
55
56             final ActiveMQConnectionFactory cf1 = new ActiveMQConnectionFactory(
57                     URL1);
58             cf1.setUseAsyncSend(false);
59
60             final ActiveMQConnectionFactory cf2 = new ActiveMQConnectionFactory(
61                     URL2);
62             cf2.setUseAsyncSend(false);
63
64             final JmsTemplate template1 = new JmsTemplate(cf1);
65             template1.setReceiveTimeout(10000);
66
67             template1.send("test.q", new MessageCreator() {
68
69                 public Message JavaDoc createMessage(Session JavaDoc session)
70                         throws JMSException JavaDoc {
71                     return session.createTextMessage("test");
72                 }
73
74             });
75
76             final JmsTemplate template2 = new JmsTemplate(cf2);
77             template2.setReceiveTimeout(10000);
78
79             final Message JavaDoc m = template2.receive("test.q");
80             assertTrue(m instanceof TextMessage JavaDoc);
81         
82             final TextMessage JavaDoc tm = (TextMessage JavaDoc) m;
83
84             Assert.assertEquals("test", tm.getText());
85
86             template2.send("test2.q", new MessageCreator() {
87
88                 public Message JavaDoc createMessage(Session JavaDoc session)
89                         throws JMSException JavaDoc {
90                     return session.createTextMessage("test2");
91                 }
92
93             });
94
95             final Message JavaDoc m2 = template1.receive("test2.q");
96
97             assertTrue(m2 instanceof TextMessage JavaDoc);
98
99             final TextMessage JavaDoc tm2 = (TextMessage JavaDoc) m2;
100
101             Assert.assertEquals("test2", tm2.getText());
102
103         } finally {
104
105             brokerService1.stop();
106             brokerService1 = null;
107             brokerService2.stop();
108             brokerService2 = null;
109
110         }
111
112     }
113
114     private BrokerService createBrokerService(final String JavaDoc brokerName,
115             final String JavaDoc uri1, final String JavaDoc uri2) throws Exception JavaDoc {
116         final BrokerService brokerService = new BrokerService();
117
118         brokerService.setBrokerName(brokerName);
119         brokerService.setPersistent(false);
120         brokerService.setUseJmx(true);
121
122         final UsageManager memoryManager = new UsageManager();
123         memoryManager.setLimit(10);
124         brokerService.setMemoryManager(memoryManager);
125
126         final ArrayList JavaDoc<PolicyEntry> policyEntries = new ArrayList JavaDoc<PolicyEntry>();
127
128         final PolicyEntry entry = new PolicyEntry();
129         entry.setQueue(">");
130         entry.setMemoryLimit(1);
131         policyEntries.add(entry);
132
133         final PolicyMap policyMap = new PolicyMap();
134         policyMap.setPolicyEntries(policyEntries);
135         brokerService.setDestinationPolicy(policyMap);
136
137         final TransportConnector tConnector = new TransportConnector();
138         tConnector.setUri(new URI JavaDoc(uri1));
139         tConnector.setBrokerName(brokerName);
140         tConnector.setName(brokerName + ".transportConnector");
141         brokerService.addConnector(tConnector);
142
143         if (uri2 != null) {
144             final NetworkConnector nc = new DiscoveryNetworkConnector(new URI JavaDoc(
145                     "static:" + uri2));
146             nc.setBridgeTempDestinations(true);
147             nc.setBrokerName(brokerName);
148             nc.setPrefetchSize(1);
149             brokerService.addNetworkConnector(nc);
150         }
151
152         return brokerService;
153
154     }
155 }
156
Popular Tags