1 17 package org.apache.activemq; 18 19 20 import junit.framework.TestCase; 21 import org.apache.activemq.broker.BrokerPlugin; 22 import org.apache.activemq.broker.BrokerService; 23 import org.apache.activemq.broker.util.UDPTraceBrokerPlugin; 24 import org.apache.activemq.broker.view.ConnectionDotFilePlugin; 25 26 import javax.jms.Connection ; 27 import javax.jms.DeliveryMode ; 28 import javax.jms.Destination ; 29 import javax.jms.Message ; 30 import javax.jms.MessageConsumer ; 31 import javax.jms.MessageProducer ; 32 import javax.jms.Session ; 33 34 public class TimeStampTest extends TestCase { 35 public void test() throws Exception { 36 BrokerService broker = new BrokerService(); 37 broker.setPersistent(false); 38 broker.setUseJmx(true); 39 broker.setPlugins(new BrokerPlugin[] { new ConnectionDotFilePlugin(), new UDPTraceBrokerPlugin() }); 40 broker.addConnector("tcp://localhost:61616"); 41 broker.addConnector("stomp://localhost:61613"); 42 broker.start(); 43 44 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); 46 47 Connection connection = connectionFactory.createConnection(); 49 connection.start(); 50 51 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 53 54 Destination destination = session.createQueue("TEST.FOO"); 56 57 MessageProducer producer = session.createProducer(destination); 59 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 60 61 Message sentMessage = session.createMessage(); 63 64 long beforeSend = System.currentTimeMillis(); 66 producer.send(sentMessage); 67 long afterSend = System.currentTimeMillis(); 68 69 assertTrue(beforeSend <= sentMessage.getJMSTimestamp() && sentMessage.getJMSTimestamp() <= afterSend); 71 72 MessageConsumer consumer = session.createConsumer(destination); 74 75 Message receivedMessage = consumer.receive(1000); 77 78 assertEquals(sentMessage.getJMSMessageID(), receivedMessage.getJMSMessageID()); 80 81 assertTrue("JMS Message Timestamp should be set during the send method: \n" + 83 " beforeSend = " + beforeSend + "\n" + 84 " getJMSTimestamp = " + receivedMessage.getJMSTimestamp() + "\n" + 85 " afterSend = " + afterSend + "\n", 86 beforeSend <= receivedMessage.getJMSTimestamp() && receivedMessage.getJMSTimestamp() <= afterSend); 87 88 assertEquals("JMS Message Timestamp of recieved message should be the same as the sent message\n ", 90 sentMessage.getJMSTimestamp(), receivedMessage.getJMSTimestamp()); 91 92 producer.close(); 94 consumer.close(); 95 session.close(); 96 connection.close(); 97 broker.stop(); 98 } 99 } 100 | Popular Tags |