1 18 19 package org.apache.activemq.usecases; 20 21 import java.net.URI ; 22 import java.util.ArrayList ; 23 import javax.jms.JMSException ; 24 import javax.jms.Message ; 25 import javax.jms.Session ; 26 import javax.jms.TextMessage ; 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 URL1 = "tcp://localhost:61616"; 43 44 private static final String URL2 = "tcp://localhost:61617"; 45 46 public void testStackOverflow() throws Exception { 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 createMessage(Session session) 70 throws JMSException { 71 return session.createTextMessage("test"); 72 } 73 74 }); 75 76 final JmsTemplate template2 = new JmsTemplate(cf2); 77 template2.setReceiveTimeout(10000); 78 79 final Message m = template2.receive("test.q"); 80 assertTrue(m instanceof TextMessage ); 81 82 final TextMessage tm = (TextMessage ) m; 83 84 Assert.assertEquals("test", tm.getText()); 85 86 template2.send("test2.q", new MessageCreator() { 87 88 public Message createMessage(Session session) 89 throws JMSException { 90 return session.createTextMessage("test2"); 91 } 92 93 }); 94 95 final Message m2 = template1.receive("test2.q"); 96 97 assertTrue(m2 instanceof TextMessage ); 98 99 final TextMessage tm2 = (TextMessage ) 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 brokerName, 115 final String uri1, final String uri2) throws Exception { 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 <PolicyEntry> policyEntries = new ArrayList <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 (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 ( 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 |