001: package org.jgroups.tests;
002:
003: import org.jgroups.JChannel;
004: import org.jgroups.Message;
005: import org.jgroups.View;
006: import org.jgroups.jmx.JmxConfigurator;
007: import org.jgroups.util.Util;
008:
009: import javax.management.MBeanServer;
010: import javax.management.ObjectName;
011:
012: /**
013: * @author Bela Ban
014: * @version $Id: JmxTest.java,v 1.9 2006/07/31 09:21:59 belaban Exp $
015: */
016: public class JmxTest {
017: MBeanServer server;
018: JChannel channel;
019: final String channel_name = "JGroups:channel=";
020:
021: private boolean start(String props) throws Exception {
022: server = Util.getMBeanServer();
023: if (server == null) {
024: System.err
025: .println("No MBeanServers found;"
026: + "\nJmxTest needs to be run with an MBeanServer present, or inside JDK 5");
027: return false;
028: }
029: channel = new JChannel(props);
030: channel.connect("DemoChannel");
031: JmxConfigurator.registerChannel(channel, server, channel_name,
032: channel.getClusterName(), true);
033: return true;
034: }
035:
036: void doWork() throws Exception {
037: server = Util.getMBeanServer();
038: if (server == null) {
039: System.err
040: .println("No MBeanServers found;"
041: + "\nJmxTest needs to be run with an MBeanServer present, or inside JDK 5");
042: return;
043: }
044: ObjectName channelName = new ObjectName(
045: "JGroups:channel=DemoChannel");
046:
047: // 1. get view and print it
048: View v = (View) server.getAttribute(channelName, "View");
049: System.out.println("view: " + v);
050:
051: // 2. send a bunch of messages
052: System.out.println("sending some messages");
053: Message msg;
054: for (int i = 0; i < 5; i++) {
055: msg = new Message(null, null, "hello from " + i);
056: server.invoke(channelName, "send", new Object[] { msg },
057: new String[] { msg.getClass().getName() });
058: }
059:
060: Util.sleep(500);
061:
062: // 3. dump number of messages
063: int numMsgs = ((Integer) server.getAttribute(channelName,
064: "NumMessages")).intValue();
065: System.out.println("channel has " + numMsgs + " messages:");
066:
067: String queue = (String) server.invoke(channelName, "dumpQueue",
068: null, null);
069: System.out.println(queue);
070:
071: System.out.println("messages are:");
072: Object obj;
073: for (int i = 0; i < numMsgs; i++) {
074: obj = server.invoke(channelName, "receive",
075: new Object[] { new Long(10) },
076: new String[] { long.class.getName() });
077: System.out.println("#" + i + ": " + obj);
078: }
079: }
080:
081: public static void main(String[] args) {
082: String props = null;
083:
084: for (int i = 0; i < args.length; i++) {
085: if (args[i].equals("-props")) {
086: props = args[++i];
087: continue;
088: }
089: System.out.println("JmxTest [-props <props>]");
090: }
091:
092: try {
093: boolean rc = false;
094: JmxTest test = new JmxTest();
095: rc = test.start(props);
096: if (rc == false)
097: return;
098: // test.doWork();
099: while (true)
100: Util.sleep(60000);
101: } catch (Exception e) {
102: e.printStackTrace();
103: }
104: }
105:
106: }
|