KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests;
2
3 import org.jgroups.JChannel;
4 import org.jgroups.Message;
5 import org.jgroups.View;
6 import org.jgroups.jmx.JmxConfigurator;
7 import org.jgroups.util.Util;
8
9 import javax.management.MBeanServer JavaDoc;
10 import javax.management.ObjectName JavaDoc;
11
12 /**
13  * @author Bela Ban
14  * @version $Id: JmxTest.java,v 1.9 2006/07/31 09:21:59 belaban Exp $
15  */

16 public class JmxTest {
17     MBeanServer JavaDoc server;
18     JChannel channel;
19     final String JavaDoc channel_name="JGroups:channel=";
20
21
22
23
24     private boolean start(String JavaDoc props) throws Exception JavaDoc {
25         server=Util.getMBeanServer();
26         if(server == null) {
27             System.err.println("No MBeanServers found;" +
28                     "\nJmxTest needs to be run with an MBeanServer present, or inside JDK 5");
29             return false;
30         }
31         channel=new JChannel(props);
32         channel.connect("DemoChannel");
33         JmxConfigurator.registerChannel(channel, server, channel_name, channel.getClusterName() , true);
34         return true;
35     }
36
37
38     void doWork() throws Exception JavaDoc {
39         server=Util.getMBeanServer();
40         if(server == null) {
41             System.err.println("No MBeanServers found;" +
42                     "\nJmxTest needs to be run with an MBeanServer present, or inside JDK 5");
43             return;
44         }
45         ObjectName JavaDoc channelName=new ObjectName JavaDoc("JGroups:channel=DemoChannel");
46
47         // 1. get view and print it
48
View v=(View)server.getAttribute(channelName, "View");
49         System.out.println("view: " + v);
50
51         // 2. send a bunch of messages
52
System.out.println("sending some messages");
53         Message msg;
54         for(int i=0; i < 5; i++) {
55             msg=new Message(null, null, "hello from " + i);
56             server.invoke(channelName, "send", new Object JavaDoc[]{msg}, new String JavaDoc[]{msg.getClass().getName()});
57         }
58
59         Util.sleep(500);
60
61         // 3. dump number of messages
62
int numMsgs=((Integer JavaDoc)server.getAttribute(channelName, "NumMessages")).intValue();
63         System.out.println("channel has " + numMsgs + " messages:");
64
65         String JavaDoc queue=(String JavaDoc)server.invoke(channelName, "dumpQueue", null, null);
66         System.out.println(queue);
67
68         System.out.println("messages are:");
69         Object JavaDoc obj;
70         for(int i=0; i < numMsgs; i++) {
71             obj=server.invoke(channelName, "receive", new Object JavaDoc[]{new Long JavaDoc(10)},
72                               new String JavaDoc[]{long.class.getName()});
73             System.out.println("#" + i + ": " + obj);
74         }
75     }
76
77     public static void main(String JavaDoc[] args) {
78         String JavaDoc props=null;
79
80         for(int i=0; i < args.length; i++) {
81             if(args[i].equals("-props")) {
82                 props=args[++i];
83                 continue;
84             }
85             System.out.println("JmxTest [-props <props>]");
86         }
87
88         try {
89             boolean rc=false;
90             JmxTest test=new JmxTest();
91             rc=test.start(props);
92             if(rc == false)
93                 return;
94             // test.doWork();
95
while(true)
96                 Util.sleep(60000);
97         }
98         catch(Exception JavaDoc e) {
99             e.printStackTrace();
100         }
101     }
102
103 }
104
Popular Tags