KickJava   Java API By Example, From Geeks To Geeks.

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


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.tool.properties.JmsProducerProperties;
21 import org.apache.activemq.tool.properties.JmsClientProperties;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import javax.jms.ConnectionFactory JavaDoc;
26 import javax.jms.MessageProducer JavaDoc;
27 import javax.jms.TextMessage JavaDoc;
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.Destination JavaDoc;
30 import javax.jms.DeliveryMode JavaDoc;
31 import java.util.Arrays JavaDoc;
32
33 public class JmsProducerClient extends AbstractJmsMeasurableClient {
34     private static final Log log = LogFactory.getLog(JmsProducerClient.class);
35
36     protected JmsProducerProperties client;
37     protected MessageProducer JavaDoc jmsProducer;
38     protected TextMessage JavaDoc jmsTextMessage;
39
40     public JmsProducerClient(ConnectionFactory JavaDoc factory) {
41         this(new JmsProducerProperties(), factory);
42     }
43
44     public JmsProducerClient(JmsProducerProperties clientProps, ConnectionFactory JavaDoc factory) {
45         super(factory);
46         this.client = clientProps;
47     }
48
49     public void sendMessages() throws JMSException JavaDoc {
50         // Send a specific number of messages
51
if (client.getSendType().equalsIgnoreCase(JmsProducerProperties.COUNT_BASED_SENDING)) {
52             sendCountBasedMessages(client.getSendCount());
53
54         // Send messages for a specific duration
55
} else {
56             sendTimeBasedMessages(client.getSendDuration());
57         }
58     }
59
60     public void sendMessages(int destCount) throws JMSException JavaDoc {
61         this.destCount = destCount;
62         sendMessages();
63     }
64
65     public void sendMessages(int destIndex, int destCount) throws JMSException JavaDoc {
66         this.destIndex = destIndex;
67         sendMessages(destCount);
68     }
69
70     public void sendCountBasedMessages(long messageCount) throws JMSException JavaDoc {
71         // Parse through different ways to send messages
72
// Avoided putting the condition inside the loop to prevent effect on performance
73
Destination JavaDoc[] dest = createDestination(destIndex, destCount);
74
75         // Create a producer, if none is created.
76
if (getJmsProducer() == null) {
77             if (dest.length == 1) {
78                 createJmsProducer(dest[0]);
79             } else {
80                 createJmsProducer();
81             }
82         }
83         try {
84             getConnection().start();
85             log.info("Starting to publish " + client.getMessageSize() + " byte(s) of " + messageCount + " messages...");
86
87             // Send one type of message only, avoiding the creation of different messages on sending
88
if (!client.isCreateNewMsg()) {
89                 // Create only a single message
90
createJmsTextMessage();
91
92                 // Send to more than one actual destination
93
if (dest.length > 1) {
94                     for (int i = 0; i < messageCount; i++) {
95                         for (int j = 0; j < dest.length; j++) {
96                             getJmsProducer().send(dest[j], getJmsTextMessage());
97                             incThroughput();
98                         }
99                     }
100                     // Send to only one actual destination
101
} else {
102                     for (int i = 0; i < messageCount; i++) {
103                         getJmsProducer().send(getJmsTextMessage());
104                         incThroughput();
105                     }
106                 }
107
108                 // Send different type of messages using indexing to identify each one.
109
// Message size will vary. Definitely slower, since messages properties
110
// will be set individually each send.
111
} else {
112                 // Send to more than one actual destination
113
if (dest.length > 1) {
114                     for (int i = 0; i < messageCount; i++) {
115                         for (int j = 0; j < dest.length; j++) {
116                             getJmsProducer().send(dest[j], createJmsTextMessage("Text Message [" + i + "]"));
117                             incThroughput();
118                         }
119                     }
120
121                     // Send to only one actual destination
122
} else {
123                     for (int i = 0; i < messageCount; i++) {
124                         getJmsProducer().send(createJmsTextMessage("Text Message [" + i + "]"));
125                         incThroughput();
126                     }
127                 }
128             }
129         } finally {
130             getConnection().close();
131         }
132     }
133
134     public void sendTimeBasedMessages(long duration) throws JMSException JavaDoc {
135         long endTime = System.currentTimeMillis() + duration;
136         // Parse through different ways to send messages
137
// Avoided putting the condition inside the loop to prevent effect on performance
138

139         Destination JavaDoc[] dest = createDestination(destIndex, destCount);
140
141         // Create a producer, if none is created.
142
if (getJmsProducer() == null) {
143             if (dest.length == 1) {
144                 createJmsProducer(dest[0]);
145             } else {
146                 createJmsProducer();
147             }
148         }
149
150         try {
151             getConnection().start();
152             log.info("Starting to publish " + client.getMessageSize() + " byte(s) messages for " + duration + " ms");
153
154             // Send one type of message only, avoiding the creation of different messages on sending
155
if (!client.isCreateNewMsg()) {
156                 // Create only a single message
157
createJmsTextMessage();
158
159                 // Send to more than one actual destination
160
if (dest.length > 1) {
161                     while (System.currentTimeMillis() < endTime) {
162                         for (int j = 0; j < dest.length; j++) {
163                             getJmsProducer().send(dest[j], getJmsTextMessage());
164                             incThroughput();
165                         }
166                     }
167                     // Send to only one actual destination
168
} else {
169                     while (System.currentTimeMillis() < endTime) {
170                         getJmsProducer().send(getJmsTextMessage());
171                         incThroughput();
172                     }
173                 }
174
175                 // Send different type of messages using indexing to identify each one.
176
// Message size will vary. Definitely slower, since messages properties
177
// will be set individually each send.
178
} else {
179                 // Send to more than one actual destination
180
long count = 1;
181                 if (dest.length > 1) {
182                     while (System.currentTimeMillis() < endTime) {
183                         for (int j = 0; j < dest.length; j++) {
184                             getJmsProducer().send(dest[j], createJmsTextMessage("Text Message [" + count++ + "]"));
185                             incThroughput();
186                         }
187                     }
188
189                     // Send to only one actual destination
190
} else {
191                     while (System.currentTimeMillis() < endTime) {
192
193                         getJmsProducer().send(createJmsTextMessage("Text Message [" + count++ + "]"));
194                         incThroughput();
195                     }
196                 }
197             }
198         } finally {
199             getConnection().close();
200         }
201     }
202
203     public MessageProducer JavaDoc createJmsProducer() throws JMSException JavaDoc {
204         jmsProducer = getSession().createProducer(null);
205         if (client.getDeliveryMode().equalsIgnoreCase(JmsProducerProperties.DELIVERY_MODE_PERSISTENT)) {
206             log.info("Creating producer to possible multiple destinations with persistent delivery.");
207             jmsProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
208         } else if (client.getDeliveryMode().equalsIgnoreCase(JmsProducerProperties.DELIVERY_MODE_NON_PERSISTENT)) {
209             log.info("Creating producer to possible multiple destinations with non-persistent delivery.");
210             jmsProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
211         } else {
212             log.warn("Unknown deliveryMode value. Defaulting to non-persistent.");
213             jmsProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
214         }
215         return jmsProducer;
216     }
217
218     public MessageProducer JavaDoc createJmsProducer(Destination JavaDoc dest) throws JMSException JavaDoc {
219         jmsProducer = getSession().createProducer(dest);
220         if (client.getDeliveryMode().equalsIgnoreCase(JmsProducerProperties.DELIVERY_MODE_PERSISTENT)) {
221             log.info("Creating producer to: " + dest.toString() + " with persistent delivery.");
222             jmsProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
223         } else if (client.getDeliveryMode().equalsIgnoreCase(JmsProducerProperties.DELIVERY_MODE_NON_PERSISTENT)) {
224             log.info("Creating producer to: " + dest.toString() + " with non-persistent delivery.");
225             jmsProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
226         } else {
227             log.warn("Unknown deliveryMode value. Defaulting to non-persistent.");
228             jmsProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
229         }
230         return jmsProducer;
231     }
232
233     public MessageProducer JavaDoc getJmsProducer() {
234         return jmsProducer;
235     }
236
237     public TextMessage JavaDoc createJmsTextMessage() throws JMSException JavaDoc {
238         return createJmsTextMessage(client.getMessageSize());
239     }
240
241     public TextMessage JavaDoc createJmsTextMessage(int size) throws JMSException JavaDoc {
242         jmsTextMessage = getSession().createTextMessage(buildText("", size));
243         return jmsTextMessage;
244     }
245
246     public TextMessage JavaDoc createJmsTextMessage(String JavaDoc text) throws JMSException JavaDoc {
247         jmsTextMessage = getSession().createTextMessage(buildText(text, client.getMessageSize()));
248         return jmsTextMessage;
249     }
250
251     public TextMessage JavaDoc getJmsTextMessage() {
252         return jmsTextMessage;
253     }
254
255     public JmsClientProperties getClient() {
256         return client;
257     }
258
259     public void setClient(JmsClientProperties clientProps) {
260         client = (JmsProducerProperties)clientProps;
261     }
262
263     protected String JavaDoc buildText(String JavaDoc text, int size) {
264         byte[] data = new byte[size - text.length()];
265         Arrays.fill(data, (byte) 0);
266         return text + new String JavaDoc(data);
267     }
268 }
269
Popular Tags