KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > test > Demo


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  * Copyright 2001 Dan Greff
20  */

21 package com.presumo.jms.test;
22
23 import com.presumo.jms.client.JmsTopicConnectionFactory;
24
25 import javax.jms.Topic JavaDoc;
26 import javax.jms.TopicConnection JavaDoc;
27 import javax.jms.TopicSession JavaDoc;
28 import javax.jms.TopicSubscriber JavaDoc;
29 import javax.jms.TopicPublisher JavaDoc;
30 import javax.jms.Message JavaDoc;
31 import javax.jms.JMSException JavaDoc;
32 import javax.jms.MessageListener JavaDoc;
33 import javax.jms.Session JavaDoc;
34
35 import java.util.*;
36 import java.io.*;
37
38 /**
39  * Thrown together to test/demonstrate the performance that can be expected
40  * from presumo once completed.
41  *
42  * @author Dan Greff
43  */

44 public class Demo
45 {
46   public final static String JavaDoc FILTER_KEY = "subfilter";
47   public final static String JavaDoc EXPECTED_MSGS_KEY = "expectedMsgs";
48   public final static String JavaDoc NUM_SUB_KEY = "numOfSubscribers";
49   public final static String JavaDoc NUM_MSGS_KEY = "numOfMsgs";
50   public final static String JavaDoc INTERATIONS_KEY = "iterations";
51   public final static String JavaDoc SLEEP_KEY = "sleep";
52   public final static String JavaDoc SERVER_KEY = "server";
53   public final static String JavaDoc PORT_KEY = "port";
54
55
56   
57   private Vector subscribers;
58   private Vector finishedSubscribers;
59   
60   private String JavaDoc server;
61   private int port;
62   
63   private String JavaDoc subfilter;
64   private int expectedMsgs;
65   private int numOfSubscribers;
66   private int numOfMsgs;
67   private int iterations;
68   private int sleepInterval;
69   
70   private TopicConnection JavaDoc connection;
71   private TopicPublisher JavaDoc publisher;
72   private TopicSession JavaDoc session;
73   
74   private String JavaDoc topicName = "demotopic";
75
76   Demo(Properties testProp) throws Exception JavaDoc
77   {
78     readProperties(testProp);
79     JmsTopicConnectionFactory factory = new JmsTopicConnectionFactory();
80     if (server != null) {
81       factory.setHost(server);
82       factory.setPort(port);
83     }
84     connection = factory.createTopicConnection();
85     
86     session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
87     Topic JavaDoc topic = session.createTopic(topicName);
88     publisher = session.createPublisher(topic);
89     
90     TopicSession JavaDoc subSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
91     subscribers = new Vector();
92     finishedSubscribers = new Vector();
93     for (int i=0; i < numOfSubscribers; i++) {
94       SubscriberHelper helper = new SubscriberHelper("Subscriber " + i, expectedMsgs, this);
95       TopicSubscriber JavaDoc sub = subSession.createSubscriber(topic, subfilter, false);
96       sub.setMessageListener(helper);
97       subscribers.add(helper);
98       System.out.println("Subscriber " + i + " created.");
99     }
100   }
101
102   private void readProperties(Properties props) throws Exception JavaDoc
103   {
104     subfilter = props.getProperty(FILTER_KEY, null);
105     expectedMsgs = Integer.parseInt(props.getProperty(EXPECTED_MSGS_KEY));
106     numOfSubscribers = Integer.parseInt(props.getProperty(NUM_SUB_KEY));
107     numOfMsgs = Integer.parseInt(props.getProperty(NUM_MSGS_KEY));
108     iterations = Integer.parseInt(props.getProperty(INTERATIONS_KEY));
109     sleepInterval = Integer.parseInt(props.getProperty(SLEEP_KEY));
110     server = props.getProperty(SERVER_KEY);
111     if (server != null) {
112       port = Integer.parseInt(props.getProperty(PORT_KEY));
113     }
114   }
115   
116
117   /**
118    *
119    */

120   void start() throws Exception JavaDoc
121   {
122     connection.start();
123     int totalcount=0;
124     for (int i=0; i < iterations; i++) {
125       long startTime = System.currentTimeMillis();
126   
127       for (int j=0; j < numOfMsgs; j++) {
128         totalcount++;
129         Message JavaDoc msg = session.createMessage();
130
131         if (totalcount % 2 == 0)
132           msg.setBooleanProperty("EVEN", true);
133         else
134           msg.setBooleanProperty("EVEN", false);
135         msg.setIntProperty("MsgNumber", totalcount);
136
137         publisher.setDeliveryMode(javax.jms.DeliveryMode.PERSISTENT);
138         publisher.publish(msg);
139       }
140       long finishTime = System.currentTimeMillis();
141       long time = finishTime - startTime;
142       System.out.println("Published " + numOfMsgs +" messages in " + time +" millseconds.");
143       try {
144           Thread.sleep(sleepInterval);
145       } catch (InterruptedException JavaDoc ie) {}
146     }
147     
148     try {
149       Thread.sleep(5000);
150     } catch (InterruptedException JavaDoc ie) {}
151   }
152
153   /**
154    *
155    */

156   synchronized boolean isComplete()
157   {
158     return subscribers.size() == 0;
159   }
160   
161   /**
162    *
163    */

164   void close() throws Exception JavaDoc
165   {
166     connection.close();
167   }
168   
169   /**
170    *
171    */

172   synchronized void subscriberFinished(SubscriberHelper helper)
173   {
174     boolean removed = subscribers.removeElement(helper);
175     
176     if (removed) {
177       finishedSubscribers.add(helper);
178     }
179    
180   }
181   
182   /**
183    *
184    */

185   synchronized void printStats()
186   {
187     for(int i=0; i < finishedSubscribers.size(); i++)
188     {
189       SubscriberHelper helper = (SubscriberHelper)finishedSubscribers.get(i);
190       System.out.println(helper.report);
191     }
192   }
193   
194     /////////////////////////////////////////////////////////////////////////
195
// Inner class to keep track of each subscribers incoming messages. //
196
/////////////////////////////////////////////////////////////////////////
197
private class SubscriberHelper implements MessageListener JavaDoc
198   {
199     private String JavaDoc id;
200     private int expectedMsgs;
201     private long startTime;
202     private int messagesReceived = 0;
203     private Demo callback;
204     String JavaDoc report;
205      
206     SubscriberHelper(String JavaDoc id, int expectedMsgs, Demo callback)
207     {
208       this.id = id;
209       this.expectedMsgs = expectedMsgs;
210       this.callback = callback;
211     }
212     
213     
214     public void onMessage(Message JavaDoc message)
215     {
216       ++messagesReceived;
217       if (messagesReceived == 1) {
218         startTime = System.currentTimeMillis();
219       }
220       if (messagesReceived == expectedMsgs) {
221         long time = System.currentTimeMillis() - startTime;
222         
223         report = "Received "+ expectedMsgs+" messages in " + time +" milliseconds.";
224         report = id + ": " + report;
225         
226         callback.subscriberFinished(this);
227         //System.out.println(report);
228
}
229       
230       if (messagesReceived % 500 == 0)
231         System.out.println(">>>>>>>>>>>>Received Message "+ messagesReceived +":" + message);
232     }
233   }
234   
235   public static void main(String JavaDoc [] args) throws Exception JavaDoc
236   {
237     if (args.length != 1) {
238       System.err.println("Usage: java Demo <demo.properties file>");
239       System.exit(-1);
240     }
241     
242     
243     try {
244       //
245
// Read in the test properties
246
//
247
Properties testProp = new Properties();
248       FileInputStream fis = null;
249       try {
250         fis = new FileInputStream(args[0]);
251         testProp.load(fis);
252       } finally {
253         if (fis != null) fis.close();
254       }
255       
256       Demo demo = new Demo(testProp);
257       
258       System.out.println("Hit enter to start the demo.");
259       BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
260       input.readLine();
261       
262       System.out.println("Starting publishes (if any) in 3 seconds...");
263       try { Thread.sleep(3000); } catch (InterruptedException JavaDoc ie) {}
264       
265       demo.start();
266       
267       while (!demo.isComplete() ) {
268         try { Thread.sleep(1000); } catch (InterruptedException JavaDoc ie) {}
269       }
270       
271       demo.printStats();
272       demo.close();
273       
274     } catch (Throwable JavaDoc t) {
275       System.err.println("The following error occured while executing the demo:");
276       t.printStackTrace();
277     }
278   }
279
280 }
281
Popular Tags