KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > JGroupsLatencyTest


1 package org.jgroups.tests;
2
3 import org.jgroups.JChannel;
4 import org.jgroups.Message;
5 import org.jgroups.ReceiverAdapter;
6 import org.jgroups.util.Util;
7
8 /**
9  * @author Bela Ban
10  * @version $Id: JGroupsLatencyTest.java,v 1.4 2007/05/04 15:50:26 belaban Exp $
11  */

12 public class JGroupsLatencyTest {
13     JChannel ch;
14
15
16     private void start(boolean sender, boolean local, String JavaDoc props) throws Exception JavaDoc {
17         if(local) {
18             JChannel ch1, ch2;
19             ch1=new JChannel(props);
20             ch1.connect("x");
21             ch2=new JChannel(props);
22             ch2.setReceiver(new MyReceiver());
23             ch2.connect("x");
24             for(int i=0; i < 10; i++) {
25                 ch1.send(new Message(null, null, System.currentTimeMillis()));
26                 Util.sleep(1000);
27             }
28             ch2.close();
29             ch1.close();
30             return;
31         }
32
33         if(sender) {
34             ch=new JChannel(props);
35             ch.connect("x");
36             for(int i=0; i < 10; i++) {
37                 ch.send(new Message(null, null, System.currentTimeMillis()));
38                 Util.sleep(1000);
39             }
40             ch.close();
41         }
42         else {
43             ch=new JChannel(props);
44             ch.setReceiver(new MyReceiver());
45             ch.connect("x");
46             System.out.println("receiver ready");
47             while(true)
48                 Util.sleep(10000);
49         }
50     }
51
52
53
54     static class MyReceiver extends ReceiverAdapter {
55
56         public void receive(Message msg) {
57             Long JavaDoc timestamp=(Long JavaDoc)msg.getObject();
58             System.out.println("time for message: " + (System.currentTimeMillis() - timestamp.longValue()) + " ms");
59         }
60     }
61
62     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
63         boolean sender=false, local=false;
64         String JavaDoc props=null;
65         for(int i=0; i < args.length; i++) {
66             if(args[i].equalsIgnoreCase("-sender")) {
67                 sender=true;
68                 continue;
69             }
70             if(args[i].equalsIgnoreCase("-local")) {
71                 local=true;
72                 continue;
73             }
74             if(args[i].equalsIgnoreCase("-props")) {
75                 props=args[++i];
76                 continue;
77             }
78             help();
79             return;
80         }
81         new JGroupsLatencyTest().start(sender, local, props);
82     }
83
84
85     private static void help() {
86         System.out.println("JGroupsLatencyTest [-sender] [-local] [-props <properties>]");
87     }
88
89 }
90
Popular Tags