KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > adaptjms > Test


1 package org.jgroups.tests.adaptjms;
2
3 import org.apache.log4j.Logger;
4
5 import javax.jms.*;
6 import javax.naming.InitialContext JavaDoc;
7 import java.io.BufferedReader JavaDoc;
8 import java.io.FileNotFoundException JavaDoc;
9 import java.io.FileReader JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.net.InetAddress JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 /** You start the test by running this class.
17  * It only parses the initial params from the config.txt
18  * file (or any other file you wish to pass as the argument)
19  * and instantiates a new JGroupsTester object.<br>
20  * Use parameters -Xbatch -Xconcurrentio (Solaris specific)
21  * @author Milcan Prica (prica@deei.units.it)
22  * @author Bela Ban (belaban@yahoo.com)
23
24  */

25 public class Test {
26     public static int mcast_port=7777;
27     public static String JavaDoc mcast_addr="228.8.8.8";
28     public static int grpMembers=4;
29
30     public static void main(String JavaDoc[] args) {
31         String JavaDoc config="config.txt";
32         BufferedReader JavaDoc fileReader;
33         String JavaDoc line;
34
35         boolean sender=false;
36         int num_msgs=1000;
37         int msg_size=500;
38
39         int num_senders=1;
40         long log_interval=1000;
41         ConnectionFactory factory;
42         InitialContext JavaDoc ctx;
43         TopicConnection conn;
44         TopicSession session;
45         TopicPublisher pub;
46         Topic topic;
47         String JavaDoc topic_name="topic/testTopic";
48
49         for(int i=0; i < args.length; i++) {
50             if("-sender".equals(args[i])) {
51                 sender=true;
52                 continue;
53             }
54             if("-receiver".equals(args[i])) {
55                 sender=false;
56                 continue;
57             }
58             if("-config".equals(args[i])) {
59                 config=args[++i];
60                 continue;
61             }
62             help();
63             return;
64         }
65
66
67
68         try {
69             fileReader=new BufferedReader JavaDoc(new FileReader JavaDoc(config));
70             while((line=fileReader.readLine()) != null) {
71                 if(line.startsWith("#"))
72                     continue;
73                 else if(line.startsWith("NUM_MSGS=")) {
74                     num_msgs=Integer.parseInt(line.substring(line.indexOf('=') + 1,
75                             line.indexOf(';')));
76                 }
77                 else if(line.startsWith("MSG_SIZE=")) {
78                     msg_size=Integer.parseInt(line.substring(line.indexOf('=') + 1,
79                             line.indexOf(';')));
80                 }
81                 else if(line.startsWith("GRPMEMBERS=")) {
82                     grpMembers=Integer.parseInt(line.substring(line.indexOf('=') + 1,
83                             line.indexOf(';')));
84                 }
85                 else if(line.startsWith("NUM_SENDERS=")) {
86                     num_senders=Integer.parseInt(line.substring(line.indexOf('=') + 1,
87                             line.indexOf(';')));
88                 }
89                 else if(line.startsWith("LOG_INTERVAL=")) {
90                     log_interval=Long.parseLong(line.substring(line.indexOf('=') + 1,
91                             line.indexOf(';')));
92                 }
93                 else if(line.startsWith("TOPIC=")) {
94                    topic_name=line.substring(line.indexOf('=') + 1, line.indexOf(';'));
95                 }
96                 else if(line.startsWith("GNUPLOT_OUTPUT=")) {
97                     // only parse if not yet set by -Dgnuplot_output=true option (overrides file)
98
if(System.getProperty("gnuplot_output") == null) {
99                         String JavaDoc gnuplot_output=line.substring(line.indexOf('=') + 1,
100                                 line.indexOf(';')).trim();
101                         if(gnuplot_output != null)
102                             System.setProperty("gnuplot_output", gnuplot_output);
103                     }
104                 }
105             }
106             fileReader.close();
107
108             System.out.println("Javagroups test:");
109             String JavaDoc s="Initial params parsing completed. Starting test"
110                     + " with these values:\n"
111                     + "Sender:" + sender + " num_msgs:" + num_msgs
112                     + " Size(bytes):" + msg_size + " # Mbrs:" + grpMembers
113                     + " Senders: " + num_senders
114                     + "\nLog interval: " + log_interval + '\n';
115
116             System.out.println(s);
117             Logger.getLogger(Test.class).info("main(): " + s);
118
119             ctx=new InitialContext JavaDoc();
120             factory=(ConnectionFactory)ctx.lookup("ConnectionFactory");
121             conn=((TopicConnectionFactory)factory).createTopicConnection();
122             session=conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
123             topic=(Topic)ctx.lookup(topic_name);
124             pub=session.createPublisher(topic);
125             new JmsTester(conn, session, topic, pub, sender, num_msgs,
126                     msg_size, grpMembers, num_senders,
127                     log_interval).initialize();
128         }
129         catch(FileNotFoundException JavaDoc notFound) {
130             System.err.println("File not found.\n" + notFound);
131         }
132         catch(IOException JavaDoc ioError) {
133             System.err.println(ioError);
134         }
135         catch(Exception JavaDoc ex) {
136             ex.printStackTrace();
137         }
138     }
139
140
141
142     public static List JavaDoc parseCommaDelimitedList(String JavaDoc s) throws Exception JavaDoc {
143         List JavaDoc retval=new ArrayList JavaDoc();
144         StringTokenizer JavaDoc tok;
145         InetAddress JavaDoc host;
146
147         if(s == null) return null;
148         tok=new StringTokenizer JavaDoc(s, ",");
149         while(tok.hasMoreTokens()) {
150             host=InetAddress.getByName(tok.nextToken());
151             retval.add(host);
152         }
153         return retval;
154     }
155
156     static void help() {
157         System.out.println("Test [-help] ([-sender] | [-receiver]) [-config <config file>]");
158     }
159 }
160
Popular Tags