KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > usecases > QueueDuplicatesTest


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

18
19 package org.apache.activemq.usecases;
20 import java.text.DateFormat JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.jms.Connection JavaDoc;
27 import javax.jms.DeliveryMode JavaDoc;
28 import javax.jms.Destination JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.Message JavaDoc;
31 import javax.jms.MessageConsumer JavaDoc;
32 import javax.jms.MessageListener JavaDoc;
33 import javax.jms.MessageProducer JavaDoc;
34 import javax.jms.Session JavaDoc;
35 import javax.jms.TextMessage JavaDoc;
36
37 import junit.framework.TestCase;
38
39 import org.apache.activemq.ActiveMQConnectionFactory;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 public class QueueDuplicatesTest extends TestCase {
44     
45     private static final Log log = LogFactory.getLog(QueueDuplicatesTest.class);
46
47     private static DateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("HH:mm:ss SSS");
48     private String JavaDoc brokerUrl;
49     private String JavaDoc subject;
50     private Connection JavaDoc brokerConnection;
51
52     public QueueDuplicatesTest(String JavaDoc name) {
53         super(name);
54     }
55
56     protected void setUp() throws Exception JavaDoc {
57         String JavaDoc peerUrl = "peer://localhost:6099";
58
59         subject = this.getClass().getName();
60
61         ActiveMQConnectionFactory fac = createFactory(peerUrl);
62         brokerConnection = fac.createConnection();
63         brokerConnection.start();
64     }
65
66     protected void tearDown() throws Exception JavaDoc {
67         if (brokerConnection != null) {
68             brokerConnection.close();
69         }
70     }
71
72     public void testDuplicates() {
73         try {
74             // Get Session
75
Session JavaDoc session = createSession(brokerConnection);
76             // create consumer
77
Destination JavaDoc dest = session.createQueue(subject);
78             MessageConsumer JavaDoc consumer = session.createConsumer(dest);
79             // subscribe to queue
80
consumer.setMessageListener(new SimpleConsumer());
81             // create producer
82
Thread JavaDoc sendingThread = new SendingThread(brokerUrl, subject);
83             // start producer
84
sendingThread.start();
85             // wait about 5 seconds
86
Thread.sleep(5000);
87             // unsubscribe consumer
88
consumer.close();
89             // wait another 5 seconds
90
Thread.sleep(5000);
91             // create new consumer
92
consumer = session.createConsumer(dest);
93             // subscribe to queue
94
consumer.setMessageListener(new SimpleConsumer());
95             // sleep a little while longer
96
Thread.sleep(15000);
97             session.close();
98         }
99         catch (Exception JavaDoc e) {
100             e.printStackTrace();
101         }
102     }
103
104     Session JavaDoc createSession(Connection JavaDoc peerConnection) throws JMSException JavaDoc {
105         // Connect using peer to peer connection
106
Session JavaDoc session = peerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
107         return session;
108     }
109
110     private ActiveMQConnectionFactory createFactory(String JavaDoc brokerUrl) {
111         ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
112         cf.setBrokerURL(brokerUrl);
113
114         return cf;
115     }
116     private class SendingThread extends Thread JavaDoc {
117         private String JavaDoc brokerUrl;
118         private String JavaDoc subject;
119
120         SendingThread(String JavaDoc brokerUrl, String JavaDoc subject) {
121             this.brokerUrl = brokerUrl;
122             this.subject = subject;
123             setDaemon(false);
124         }
125
126         public void run() {
127             try {
128                 Session JavaDoc session = createSession(brokerConnection);
129                 Destination JavaDoc dest = session.createQueue(subject);
130                 MessageProducer JavaDoc producer = session.createProducer(dest);
131                 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
132                 for (int i = 0;i < 20;i++) {
133                     String JavaDoc txt = "Text Message: " + i;
134                     TextMessage JavaDoc msg = session.createTextMessage(txt);
135                     producer.send(msg);
136                     log.info(formatter.format(new Date JavaDoc()) + " Sent ==> " + msg + " to " + subject);
137                     Thread.sleep(1000);
138                 }
139                 session.close();
140             }
141             catch (Exception JavaDoc e) {
142                 e.printStackTrace();
143             }
144         }
145     }
146     private static class SimpleConsumer implements MessageListener JavaDoc {
147         private Map JavaDoc msgs = new HashMap JavaDoc();
148
149         public void onMessage(Message JavaDoc message) {
150             log.info(formatter.format(new Date JavaDoc()) + " SimpleConsumer Message Received: " + message);
151             try {
152                 String JavaDoc id = message.getJMSMessageID();
153                 assertNull("Message is duplicate: " + id, msgs.get(id));
154                 msgs.put(id, message);
155             }
156             catch (Exception JavaDoc e) {
157                 e.printStackTrace();
158             }
159         }
160     }
161 }
162
Popular Tags