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 ; 10 import javax.management.ObjectName ; 11 12 16 public class JmxTest { 17 MBeanServer server; 18 JChannel channel; 19 final String channel_name="JGroups:channel="; 20 21 22 23 24 private boolean start(String props) throws Exception { 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 { 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 channelName=new ObjectName ("JGroups:channel=DemoChannel"); 46 47 View v=(View)server.getAttribute(channelName, "View"); 49 System.out.println("view: " + v); 50 51 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 []{msg}, new String []{msg.getClass().getName()}); 57 } 58 59 Util.sleep(500); 60 61 int numMsgs=((Integer )server.getAttribute(channelName, "NumMessages")).intValue(); 63 System.out.println("channel has " + numMsgs + " messages:"); 64 65 String queue=(String )server.invoke(channelName, "dumpQueue", null, null); 66 System.out.println(queue); 67 68 System.out.println("messages are:"); 69 Object obj; 70 for(int i=0; i < numMsgs; i++) { 71 obj=server.invoke(channelName, "receive", new Object []{new Long (10)}, 72 new String []{long.class.getName()}); 73 System.out.println("#" + i + ": " + obj); 74 } 75 } 76 77 public static void main(String [] args) { 78 String 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 while(true) 96 Util.sleep(60000); 97 } 98 catch(Exception e) { 99 e.printStackTrace(); 100 } 101 } 102 103 } 104 | Popular Tags |