KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > perf > PerfConsumer


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

32 public class PerfConsumer implements MessageListener JavaDoc{
33     protected Connection JavaDoc connection;
34     protected MessageConsumer JavaDoc consumer;
35     protected long sleepDuration;
36     
37     protected PerfRate rate=new PerfRate();
38     public PerfConsumer(ConnectionFactory JavaDoc fac,Destination JavaDoc dest,String JavaDoc consumerName) throws JMSException JavaDoc{
39         connection=fac.createConnection();
40         connection.setClientID(consumerName);
41         Session JavaDoc s=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
42         if(dest instanceof Topic JavaDoc&&consumerName!=null&&consumerName.length()>0){
43             consumer=s.createDurableSubscriber((Topic JavaDoc) dest,consumerName);
44         }else{
45             consumer=s.createConsumer(dest);
46         }
47         consumer.setMessageListener(this);
48     }
49     public PerfConsumer(ConnectionFactory JavaDoc fac,Destination JavaDoc dest) throws JMSException JavaDoc{
50         this(fac,dest,null);
51     }
52     public void start() throws JMSException JavaDoc{
53         connection.start();
54         rate.reset();
55     }
56     public void stop() throws JMSException JavaDoc{
57         connection.stop();
58     }
59     public void shutDown() throws JMSException JavaDoc{
60         connection.close();
61     }
62     public PerfRate getRate(){
63         return rate;
64     }
65     public void onMessage(Message JavaDoc msg){
66         rate.increment();
67         try {
68             if( sleepDuration!=0 ) {
69                 Thread.sleep(sleepDuration);
70             }
71         } catch (InterruptedException JavaDoc e) {
72         }
73     }
74     
75     public synchronized long getSleepDuration() {
76         return sleepDuration;
77     }
78     public synchronized void setSleepDuration(long sleepDuration) {
79         this.sleepDuration = sleepDuration;
80     }
81 }
82
Popular Tags