1 18 package org.apache.activemq; 19 20 import javax.jms.Connection ; 21 import javax.jms.JMSException ; 22 import javax.jms.Message ; 23 import javax.jms.MessageConsumer ; 24 import javax.jms.MessageProducer ; 25 import javax.jms.Queue ; 26 import javax.jms.Session ; 27 28 29 32 public class ConsumerReceiveWithTimeoutTest extends TestSupport { 33 34 private Connection connection; 35 36 protected void setUp() throws Exception { 37 super.setUp(); 38 connection = createConnection(); 39 } 40 41 44 protected void tearDown() throws Exception { 45 if (connection != null) { 46 connection.close(); 47 connection = null; 48 } 49 super.tearDown(); 50 } 51 52 57 public void testConsumerReceiveBeforeMessageDispatched() throws JMSException { 58 59 connection.start(); 60 61 final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 62 final Queue queue = session.createQueue("test"); 63 64 Thread t = new Thread () { 65 public void run(){ 66 try { 67 Thread.sleep(10000); 69 MessageProducer producer = session.createProducer(queue); 70 producer.send(session.createTextMessage("Hello")); 71 }catch(Exception e){e.printStackTrace();} 72 } 73 }; 74 75 t.start(); 76 77 MessageConsumer consumer = session.createConsumer(queue); 79 Message msg = consumer.receive(60000); 80 assertNotNull(msg); 81 session.close(); 82 83 } 84 85 86 87 88 89 } 90 | Popular Tags |