KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > systest > impl > ProducerAgentImpl


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.systest.impl;
19
20 import org.apache.activemq.systest.AgentStopper;
21 import org.apache.activemq.systest.MessageList;
22 import org.apache.activemq.systest.ProducerAgent;
23
24 import javax.jms.DeliveryMode JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.MessageProducer JavaDoc;
27
28 /**
29  * A simple in JVM implementation of a {@link ProducerAgent}
30  *
31  * @version $Revision: 1.1 $
32  */

33 public class ProducerAgentImpl extends JmsClientSupport implements ProducerAgent {
34
35     private MessageProducer JavaDoc producer;
36     private boolean persistent = true;
37
38     public void start() throws Exception JavaDoc {
39         super.start();
40         producer = createProducer();
41     }
42
43     public void sendMessages(MessageList messageList) throws JMSException JavaDoc {
44         System.out.println("About to send: " + messageList.getSize() + " message(s) to destination: " + getDestination());
45         messageList.sendMessages(getSession(), producer);
46         if (isTransacted()) {
47             getSession().commit();
48         }
49         System.out.println("Sent: " + messageList.getSize() + " message(s) to destination: " + getDestination());
50     }
51     
52     public void sendMessages(MessageList messageList, int percent) throws JMSException JavaDoc {
53         System.out.println("About to send: " + percent + " % of the message(s) to destination: " + getDestination());
54         messageList.sendMessages(getSession(), producer, percent);
55         if (isTransacted()) {
56             getSession().commit();
57         }
58         System.out.println("Sent: " + percent + " % of the message(s) to destination: " + getDestination());
59     }
60
61     public boolean isPersistent() {
62         return persistent;
63     }
64
65     public void setPersistent(boolean persistent) {
66         this.persistent = persistent;
67     }
68
69     public void stop(AgentStopper stopper) {
70         if (producer != null) {
71             try {
72                 producer.close();
73             }
74             catch (JMSException JavaDoc e) {
75                 stopper.onException(this, e);
76             }
77             finally {
78                 producer = null;
79             }
80         }
81         super.stop(stopper);
82     }
83
84     // Implementation methods
85
// -------------------------------------------------------------------------
86
protected MessageProducer JavaDoc createProducer() throws JMSException JavaDoc {
87         MessageProducer JavaDoc answer = getSession().createProducer(getDestination());
88         answer.setDeliveryMode(isPersistent() ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
89         return answer;
90     }
91
92 }
93
Popular Tags