KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > benchmark > Producer


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.benchmark;
19
20 import javax.jms.DeliveryMode JavaDoc;
21 import javax.jms.Destination JavaDoc;
22 import javax.jms.JMSException JavaDoc;
23 import javax.jms.Message JavaDoc;
24 import javax.jms.MessageProducer JavaDoc;
25 import javax.jms.Session JavaDoc;
26 import java.io.BufferedReader JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * @author James Strachan
33  * @version $Revision$
34  */

35 public class Producer extends BenchmarkSupport {
36
37     int loops = -1;
38     int loopSize = 1000;
39     private int messageSize = 1000;
40
41     public static void main(String JavaDoc[] args) {
42         Producer tool = new Producer();
43         if (args.length > 0) {
44             tool.setUrl(args[0]);
45         }
46         if (args.length > 1) {
47             tool.setTopic(parseBoolean(args[1]));
48         }
49         if (args.length > 2) {
50             tool.setSubject(args[2]);
51         }
52         if (args.length > 3) {
53             tool.setDurable(parseBoolean(args[3]));
54         }
55         if (args.length > 4) {
56             tool.setMessageSize(Integer.parseInt(args[4]));
57         }
58         if (args.length > 5) {
59             tool.setConnectionCount(Integer.parseInt(args[5]));
60         }
61         try {
62             tool.run();
63         }
64         catch (Exception JavaDoc e) {
65             System.out.println("Caught: " + e);
66             e.printStackTrace();
67         }
68     }
69
70     public Producer() {
71     }
72
73     public void run() throws Exception JavaDoc {
74         start();
75         publish();
76     }
77
78     // Properties
79
//-------------------------------------------------------------------------
80
public int getMessageSize() {
81         return messageSize;
82     }
83
84     public void setMessageSize(int messageSize) {
85         this.messageSize = messageSize;
86     }
87
88     public int getLoopSize() {
89         return loopSize;
90     }
91
92     public void setLoopSize(int loopSize) {
93         this.loopSize = loopSize;
94     }
95
96     // Implementation methods
97
//-------------------------------------------------------------------------
98

99     protected void publish() throws Exception JavaDoc {
100         final String JavaDoc text = getMessage();
101
102         System.out.println("Publishing to: " + subjects.length + " subject(s)");
103
104         for (int i = 0; i < subjects.length; i++) {
105             final String JavaDoc subject = subjects[i];
106             Thread JavaDoc thread = new Thread JavaDoc() {
107                 public void run() {
108                     try {
109                         publish(text, subject);
110                     }
111                     catch (JMSException JavaDoc e) {
112                         System.out.println("Caught: " + e);
113                         e.printStackTrace();
114                     }
115                 }
116             };
117             thread.start();
118         }
119
120     }
121
122     protected String JavaDoc getMessage() {
123         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
124         for (int i = 0; i < messageSize; i++) {
125             char ch = 'X';
126             buffer.append(ch);
127         }
128         return buffer.toString();
129     }
130
131     protected void publish(String JavaDoc text, String JavaDoc subject) throws JMSException JavaDoc {
132         Session JavaDoc session = createSession();
133
134         Destination JavaDoc destination = createDestination(session, subject);
135
136         MessageProducer JavaDoc publisher = session.createProducer(destination);
137         if (isDurable()) {
138             publisher.setDeliveryMode(DeliveryMode.PERSISTENT);
139         }
140         else {
141             publisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
142         }
143
144         System.out.println("Starting publisher on : " + destination + " of type: " + destination.getClass().getName());
145         System.out.println("Message length: " + text.length());
146
147         if (loops <= 0) {
148             while (true) {
149                 publishLoop(session, publisher, text);
150             }
151         }
152         else {
153             for (int i = 0; i < loops; i++) {
154                 publishLoop(session, publisher, text);
155             }
156         }
157     }
158
159     protected void publishLoop(Session JavaDoc session, MessageProducer JavaDoc publisher, String JavaDoc text) throws JMSException JavaDoc {
160         for (int i = 0; i < loopSize; i++) {
161             Message JavaDoc message = session.createTextMessage(text);
162
163             publisher.send(message);
164             count(1);
165         }
166     }
167
168     protected String JavaDoc loadFile(String JavaDoc file) throws IOException JavaDoc {
169         System.out.println("Loading file: " + file);
170
171         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
172         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
173         while (true) {
174             String JavaDoc line = in.readLine();
175             if (line == null) {
176                 break;
177             }
178             buffer.append(line);
179             buffer.append(File.separator);
180         }
181         return buffer.toString();
182     }
183 }
184
Popular Tags