1 18 package org.apache.activemq; 19 20 import junit.framework.TestCase; 21 22 import javax.jms.BytesMessage ; 23 import javax.jms.Connection ; 24 import javax.jms.DeliveryMode ; 25 import javax.jms.InvalidDestinationException ; 26 import javax.jms.JMSException ; 27 import javax.jms.Message ; 28 import javax.jms.MessageConsumer ; 29 import javax.jms.MessageProducer ; 30 import javax.jms.Queue ; 31 import javax.jms.Session ; 32 import javax.jms.TemporaryQueue ; 33 import javax.jms.TextMessage ; 34 import java.util.ArrayList ; 35 36 39 public class JmsTempDestinationTest extends TestCase { 40 41 private Connection connection; 42 private ActiveMQConnectionFactory factory; 43 44 protected void setUp() throws Exception { 45 factory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); 46 factory.setUseAsyncSend(false); 47 connection = factory.createConnection(); 48 } 49 50 53 protected void tearDown() throws Exception { 54 if (connection != null) { 55 connection.close(); 56 connection = null; 57 } 58 } 59 60 61 66 public void testTempDestOnlyConsumedByLocalConn() throws JMSException { 67 connection.start(); 68 69 Session tempSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 70 TemporaryQueue queue = tempSession.createTemporaryQueue(); 71 MessageProducer producer = tempSession.createProducer(queue); 72 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 73 TextMessage message = tempSession.createTextMessage("First"); 74 producer.send(message); 75 76 Connection otherConnection = factory.createConnection(); 78 Session otherSession = otherConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); 79 TemporaryQueue otherQueue = otherSession.createTemporaryQueue(); 80 MessageConsumer consumer = otherSession.createConsumer(otherQueue); 81 Message msg = consumer.receive(3000); 82 assertNull(msg); 83 84 try{ 86 consumer = otherSession.createConsumer(queue); 87 fail("Send should fail since temp destination should be used from another connection"); 88 }catch(InvalidDestinationException e){ 89 assertTrue("failed to throw an exception",true); 90 } 91 92 93 consumer = tempSession.createConsumer(queue); 95 msg = consumer.receive(3000); 96 assertNotNull(msg); 97 98 99 } 100 101 107 public void testTempQueueHoldsMessagesWithConsumers() throws JMSException { 108 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 109 Queue queue = session.createTemporaryQueue(); 110 MessageConsumer consumer = session.createConsumer(queue); 111 connection.start(); 112 113 MessageProducer producer = session.createProducer(queue); 114 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 115 TextMessage message = session.createTextMessage("Hello"); 116 producer.send(message); 117 118 Message message2 = consumer.receive(1000); 119 assertNotNull(message2); 120 assertTrue("Expected message to be a TextMessage", message2 instanceof TextMessage ); 121 assertTrue("Expected message to be a '" + message.getText() + "'", 122 ((TextMessage ) message2).getText().equals(message.getText())); 123 } 124 125 131 public void testTempQueueHoldsMessagesWithoutConsumers() throws JMSException { 132 133 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 134 Queue queue = session.createTemporaryQueue(); 135 MessageProducer producer = session.createProducer(queue); 136 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 137 TextMessage message = session.createTextMessage("Hello"); 138 producer.send(message); 139 140 connection.start(); 141 MessageConsumer consumer = session.createConsumer(queue); 142 Message message2 = consumer.receive(3000); 143 assertNotNull(message2); 144 assertTrue("Expected message to be a TextMessage", message2 instanceof TextMessage ); 145 assertTrue("Expected message to be a '" + message.getText() + "'", 146 ((TextMessage ) message2).getText().equals(message.getText())); 147 148 } 149 150 154 public void testTmpQueueWorksUnderLoad() throws JMSException { 155 int count = 500; 156 int dataSize = 1024; 157 158 ArrayList list = new ArrayList (count); 159 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 160 Queue queue = session.createTemporaryQueue(); 161 MessageProducer producer = session.createProducer(queue); 162 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 163 164 byte[] data = new byte[dataSize]; 165 for (int i =0; i < count; i++){ 166 BytesMessage message = session.createBytesMessage(); 167 message.writeBytes(data); 168 producer.send(message); 169 list.add(message); 170 } 171 172 173 connection.start(); 174 MessageConsumer consumer = session.createConsumer(queue); 175 for (int i =0; i < count; i++){ 176 Message message2 = consumer.receive(2000); 177 178 assertTrue(message2 != null); 179 assertTrue(message2.equals(list.get(i))); 180 } 181 } 182 183 188 public void testPublishFailsForClosedConnection() throws JMSException { 189 190 Connection tempConnection = factory.createConnection(); 191 Session tempSession = tempConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); 192 TemporaryQueue queue = tempSession.createTemporaryQueue(); 193 194 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 195 connection.start(); 196 197 MessageProducer producer = session.createProducer(queue); 199 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 200 TextMessage message = session.createTextMessage("First"); 201 producer.send(message); 202 203 tempConnection.close(); 205 206 try { 208 message = session.createTextMessage("Hello"); 209 producer.send(message); 210 fail("Send should fail since temp destination should not exist anymore."); 211 } catch ( JMSException e ) { 212 assertTrue("failed to throw an exception",true); 213 } 214 } 215 216 221 public void testPublishFailsForDestoryedTempDestination() throws JMSException { 222 223 Connection tempConnection = factory.createConnection(); 224 Session tempSession = tempConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); 225 TemporaryQueue queue = tempSession.createTemporaryQueue(); 226 227 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 228 connection.start(); 229 230 MessageProducer producer = session.createProducer(queue); 232 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 233 TextMessage message = session.createTextMessage("First"); 234 producer.send(message); 235 236 queue.delete(); 238 239 try { 241 message = session.createTextMessage("Hello"); 242 producer.send(message); 243 fail("Send should fail since temp destination should not exist anymore."); 244 } catch ( JMSException e ) { 245 assertTrue("failed to throw an exception",true); 246 } 247 } 248 249 253 public void testDeleteDestinationWithSubscribersFails() throws JMSException { 254 Connection connection = factory.createConnection(); 255 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 256 TemporaryQueue queue = session.createTemporaryQueue(); 257 258 connection.start(); 259 260 session.createConsumer(queue); 261 262 try { 264 queue.delete(); 265 fail("Should fail as Subscribers are active"); 266 } catch ( JMSException e ) { 267 assertTrue("failed to throw an exception",true); 268 } 269 } 270 } 271 | Popular Tags |