KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > Consumer


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 package org.apache.activemq.tool;
19
20 import org.apache.activemq.util.MessageIdList;
21
22 import javax.jms.Connection JavaDoc;
23 import javax.jms.ConnectionFactory JavaDoc;
24 import javax.jms.Destination JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.Message JavaDoc;
27 import javax.jms.MessageConsumer JavaDoc;
28 import javax.jms.MessageListener JavaDoc;
29 import javax.jms.Session JavaDoc;
30 import javax.jms.Topic JavaDoc;
31 /**
32  * @version $Revision: 1.3 $
33  */

34 public class Consumer extends MessageIdList implements MessageListener JavaDoc{
35     protected Connection JavaDoc connection;
36     protected MessageConsumer JavaDoc consumer;
37     protected long counter = 0;
38     protected boolean isParent = false;
39     protected boolean inOrder = true;
40
41
42     public Consumer() {
43         super();
44     }
45     public Consumer(ConnectionFactory JavaDoc fac,Destination JavaDoc dest,String JavaDoc consumerName) throws JMSException JavaDoc{
46         connection=fac.createConnection();
47         Session JavaDoc s=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
48         if(dest instanceof Topic JavaDoc&&consumerName!=null&&consumerName.length()>0){
49             consumer=s.createDurableSubscriber((Topic JavaDoc) dest,consumerName);
50         }else{
51             consumer=s.createConsumer(dest);
52         }
53         consumer.setMessageListener(this);
54     }
55     public Consumer(ConnectionFactory JavaDoc fac,Destination JavaDoc dest) throws JMSException JavaDoc{
56         this(fac,dest,null);
57     }
58     public void start() throws JMSException JavaDoc{
59         connection.start();
60     }
61     public void stop() throws JMSException JavaDoc{
62         connection.stop();
63     }
64     public void shutDown() throws JMSException JavaDoc{
65         connection.close();
66     }
67
68
69     public Message JavaDoc receive() throws JMSException JavaDoc{
70         return consumer.receive();
71     }
72
73     public Message JavaDoc receive(long wait) throws JMSException JavaDoc{
74         return consumer.receive(wait);
75     }
76
77     public void onMessage(Message JavaDoc msg){
78         super.onMessage(msg);
79         if(isParent) {
80            try {
81               long ctr = msg.getLongProperty("counter");
82               if (counter != ctr){
83                    inOrder = false;
84               }
85               counter ++;
86            }catch(Exception JavaDoc e) {
87                e.printStackTrace();
88            }
89         }
90     }
91
92
93     public boolean isInOrder() {
94         return inOrder;
95     }
96
97
98     public void setAsParent(boolean isParent) {
99         this.isParent = isParent;
100     }
101
102     public boolean isParent() {
103         return this.isParent;
104     }
105
106
107     /**
108      * Performs a testing assertion that the correct order of messages have
109      * been received
110      *
111      * @param messageCount
112      */

113     public void assertMessagesReceivedAreInOrder(int messageCount) {
114         assertEquals("expected number of messages when received", messageCount, getMessageCount());
115     }
116
117 }
118
Popular Tags