KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.concurrent.CountDownLatch JavaDoc;
21
22 import javax.jms.BytesMessage JavaDoc;
23 import javax.jms.Connection JavaDoc;
24 import javax.jms.ConnectionFactory JavaDoc;
25 import javax.jms.Destination JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.MessageProducer JavaDoc;
28 import javax.jms.Session JavaDoc;
29 /**
30  * @version $Revision: 1.3 $
31  */

32 public class PerfProducer implements Runnable JavaDoc {
33     protected Connection JavaDoc connection;
34     protected MessageProducer JavaDoc producer;
35     protected PerfRate rate=new PerfRate();
36     private byte[] payload;
37     private Session JavaDoc session;
38     private final CountDownLatch JavaDoc stopped = new CountDownLatch JavaDoc(1);
39     private boolean running;
40     
41     public PerfProducer(ConnectionFactory JavaDoc fac,Destination JavaDoc dest, byte[] palyload) throws JMSException JavaDoc{
42         connection=fac.createConnection();
43         session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
44         producer=session.createProducer(dest);
45         this.payload = palyload;
46     }
47     
48     public void setDeliveryMode(int mode) throws JMSException JavaDoc{
49         producer.setDeliveryMode(mode);
50     }
51     
52     public void shutDown() throws JMSException JavaDoc{
53         connection.close();
54     }
55
56     public PerfRate getRate(){
57         return rate;
58     }
59         
60     synchronized public void start() throws JMSException JavaDoc{
61         if( !running ) {
62             rate.reset();
63             running = true;
64             connection.start();
65             new Thread JavaDoc(this).start();
66         }
67     }
68     public void stop() throws JMSException JavaDoc, InterruptedException JavaDoc{
69         synchronized(this) {
70             running=false;
71         }
72         stopped.await();
73         connection.stop();
74     }
75     synchronized public boolean isRunning() {
76         return running;
77     }
78     
79     public void run() {
80         try {
81             while(isRunning()){
82                 BytesMessage JavaDoc msg;
83                 msg=session.createBytesMessage();
84                 msg.writeBytes(payload);
85                 producer.send(msg);
86                 rate.increment();
87             }
88         } catch (Throwable JavaDoc e) {
89             e.printStackTrace();
90         } finally {
91             stopped.countDown();
92         }
93     }
94     
95 }
96
Popular Tags