KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > TimeStampTest


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

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 JavaDoc;
27 import javax.jms.DeliveryMode JavaDoc;
28 import javax.jms.Destination JavaDoc;
29 import javax.jms.Message JavaDoc;
30 import javax.jms.MessageConsumer JavaDoc;
31 import javax.jms.MessageProducer JavaDoc;
32 import javax.jms.Session JavaDoc;
33
34 public class TimeStampTest extends TestCase {
35     public void test() throws Exception JavaDoc {
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         // Create a ConnectionFactory
45
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
46
47         // Create a Connection
48
Connection JavaDoc connection = connectionFactory.createConnection();
49         connection.start();
50
51         // Create a Session
52
Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
53
54         // Create the destination Queue
55
Destination JavaDoc destination = session.createQueue("TEST.FOO");
56
57         // Create a MessageProducer from the Session to the Topic or Queue
58
MessageProducer JavaDoc producer = session.createProducer(destination);
59         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
60
61         // Create a messages
62
Message sentMessage = session.createMessage();
63
64         // Tell the producer to send the message
65
long beforeSend = System.currentTimeMillis();
66         producer.send(sentMessage);
67         long afterSend = System.currentTimeMillis();
68
69         // assert message timestamp is in window
70
assertTrue(beforeSend <= sentMessage.getJMSTimestamp() && sentMessage.getJMSTimestamp() <= afterSend);
71
72         // Create a MessageConsumer from the Session to the Topic or Queue
73
MessageConsumer JavaDoc consumer = session.createConsumer(destination);
74
75         // Wait for a message
76
Message receivedMessage = consumer.receive(1000);
77
78         // assert we got the same message ID we sent
79
assertEquals(sentMessage.getJMSMessageID(), receivedMessage.getJMSMessageID());
80
81         // assert message timestamp is in window
82
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         // assert message timestamp is unchanged
89
assertEquals("JMS Message Timestamp of recieved message should be the same as the sent message\n ",
90                 sentMessage.getJMSTimestamp(), receivedMessage.getJMSTimestamp());
91
92         // Clean up
93
producer.close();
94         consumer.close();
95         session.close();
96         connection.close();
97         broker.stop();
98     }
99 }
100
Popular Tags