Java Code Examples for javax.management.MBeanServerConnection#createMBean()

The following examples show how to use javax.management.MBeanServerConnection#createMBean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RMIConnectorLogAttributesTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void doTest(JMXConnector connector) throws IOException,
MalformedObjectNameException, ReflectionException,
InstanceAlreadyExistsException, MBeanRegistrationException,
MBeanException, NotCompliantMBeanException, InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException {
    MBeanServerConnection  mbsc = connector.getMBeanServerConnection();


    ObjectName objName = new ObjectName("com.redhat.test.jmx:type=NameMBean");
    System.out.println("DEBUG: Calling createMBean");
    mbsc.createMBean(Name.class.getName(), objName);

    System.out.println("DEBUG: Calling setAttributes");
    AttributeList attList = new AttributeList();
    attList.add(new Attribute("FirstName", ANY_NAME));
    attList.add(new Attribute("LastName", ANY_NAME));
    mbsc.setAttributes(objName, attList);
}
 
Example 2
Source File: ConcurrentModificationTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void mbeanOp(MBeanServerConnection mserver, ObjectName name, boolean adding)
        throws Exception {
    if (adding) {
        mserver.createMBean("javax.management.timer.Timer", name);
    } else {
        mserver.unregisterMBean(name);
    }
}
 
Example 3
Source File: Client.java    From vjtools with Apache License 2.0 5 votes vote down vote up
protected static Object[] doBeans(final MBeanServerConnection mbsc, final ObjectName objName,
		final String[] command, final boolean oneBeanOnly) throws Exception {
	Object[] result = null;
	Set beans = mbsc.queryMBeans(objName, null);
	if (beans.size() == 0) {
		// No bean found. Check if we are to create a bean?
		if (command.length == 1 && notEmpty(command[0]) && command[0].startsWith(CREATE_CMD_PREFIX)) {
			String className = command[0].substring(CREATE_CMD_PREFIX.length());
			mbsc.createMBean(className, objName);
		} else {
			// TODO: Is there a better JMX exception that RE for this
			// scenario?
			throw new RuntimeException(objName.getCanonicalName() + " not registered.");
		}
	} else if (beans.size() == 1) {
		result = doBean(mbsc, (ObjectInstance) beans.iterator().next(), command);
	} else {
		if (oneBeanOnly) {
			throw new RuntimeException("Only supposed to be one bean " + "query result");
		}
		// This is case of multiple beans in query results.
		// Print name of each into a StringBuffer. Return as one
		// result.
		StringBuffer buffer = new StringBuffer();
		for (Iterator i = beans.iterator(); i.hasNext();) {
			Object obj = i.next();
			if (obj instanceof ObjectName) {
				buffer.append((((ObjectName) obj).getCanonicalName()));
			} else if (obj instanceof ObjectInstance) {
				buffer.append((((ObjectInstance) obj).getObjectName().getCanonicalName()));
			} else {
				throw new RuntimeException("Unexpected object type: " + obj);
			}
			buffer.append("\n");
		}
		result = new String[] { buffer.toString() };
	}
	return result;
}
 
Example 4
Source File: Client.java    From vjtools with Apache License 2.0 5 votes vote down vote up
protected static Object[] doBeans(final MBeanServerConnection mbsc, final ObjectName objName,
		final String[] command, final boolean oneBeanOnly) throws Exception {
	Object[] result = null;
	Set beans = mbsc.queryMBeans(objName, null);
	if (beans.isEmpty()) {
		// No bean found. Check if we are to create a bean?
		if (command.length == 1 && notEmpty(command[0]) && command[0].startsWith(CREATE_CMD_PREFIX)) {
			String className = command[0].substring(CREATE_CMD_PREFIX.length());
			mbsc.createMBean(className, objName);
		} else {
			// TODO: Is there a better JMX exception that RE for this
			// scenario?
			throw new RuntimeException(objName.getCanonicalName() + " not registered.");
		}
	} else if (beans.size() == 1) {
		result = doBean(mbsc, (ObjectInstance) beans.iterator().next(), command);
	} else {
		if (oneBeanOnly) {
			throw new RuntimeException("Only supposed to be one bean " + "query result");
		}
		// This is case of multiple beans in query results.
		// Print name of each into a StringBuffer. Return as one
		// result.
		StringBuffer buffer = new StringBuffer();
		for (Iterator i = beans.iterator(); i.hasNext();) {
			Object obj = i.next();
			if (obj instanceof ObjectName) {
				buffer.append((((ObjectName) obj).getCanonicalName()));
			} else if (obj instanceof ObjectInstance) {
				buffer.append((((ObjectInstance) obj).getObjectName().getCanonicalName()));
			} else {
				throw new RuntimeException("Unexpected object type: " + obj);
			}
			buffer.append("\n");
		}
		result = new String[] { buffer.toString() };
	}
	return result;
}
 
Example 5
Source File: AuthorizationTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected int doCreateRequest(MBeanServerConnection mbsc,
                              ObjectName on,
                              boolean expectedException) {
    int errorCount = 0;

    try {
        Utils.debug(Utils.DEBUG_STANDARD,
            "ClientSide::doCreateRequest: Create and register the MBean") ;

        mbsc.createMBean("Simple", on) ;

        if (expectedException) {
            System.out.println("ClientSide::doCreateRequest: " +
                "(ERROR) Create did not fail with expected SecurityException");
            errorCount++;
        } else {
            System.out.println("ClientSide::doCreateRequest: (OK) Create succeed") ;
        }
    } catch(Exception e) {
        Utils.printThrowable(e, true) ;
        if (expectedException) {
            if (e instanceof java.lang.SecurityException) {
                System.out.println("ClientSide::doCreateRequest: " +
                    "(OK) Create failed with expected SecurityException") ;
            } else {
                System.out.println("ClientSide::doCreateRequest: " +
                    "(ERROR) Create failed with " +
                    e.getClass() + " instead of expected SecurityException");
                errorCount++;
            }
        } else {
            System.out.println("ClientSide::doCreateRequest: " +
                "(ERROR) Create failed");
            errorCount++;
        }
    }
    return errorCount;
}
 
Example 6
Source File: Client.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void main(String[] args) throws Exception
{
   // The address of the connector server
   JMXServiceURL url = new JMXServiceURL("rmi", "localhost", 0, "/jndi/jmx");

   // Create and connect the connector client
   JMXConnector cntor = JMXConnectorFactory.connect(url, null);

   // The connection represent, on client-side, the remote MBeanServer
   MBeanServerConnection connection = cntor.getMBeanServerConnection();

   // The listener that will receive notifications from a remote MBean
   NotificationListener listener = new NotificationListener()
   {
      public void handleNotification(Notification notification, Object handback)
      {
         System.out.println(notification);
      }
   };

   // The MBeanServerDelegate emits notifications about registration/unregistration of MBeans
   ObjectName delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");

   connection.addNotificationListener(delegateName, listener, null, null);

   // Give chance to the notification machinery to setup
   Thread.sleep(1000);

   // Now register a remote MBean, for example an MLet, so that the MBeanServerDelegate
   // will emit notifications for its registration
   ObjectName name = ObjectName.getInstance("examples:mbean=mlet");
   // First notification
   connection.createMBean(MLet.class.getName(), name, null);
   // Second notification
   connection.unregisterMBean(name);
}
 
Example 7
Source File: ConcurrentModificationTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void mbeanOp(MBeanServerConnection mserver, ObjectName name, boolean adding)
        throws Exception {
    if (adding) {
        mserver.createMBean("javax.management.timer.Timer", name);
    } else {
        mserver.unregisterMBean(name);
    }
}
 
Example 8
Source File: AuthorizationTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected int doCreateRequest(MBeanServerConnection mbsc,
                              ObjectName on,
                              boolean expectedException) {
    int errorCount = 0;

    try {
        Utils.debug(Utils.DEBUG_STANDARD,
            "ClientSide::doCreateRequest: Create and register the MBean") ;

        mbsc.createMBean("Simple", on) ;

        if (expectedException) {
            System.out.println("ClientSide::doCreateRequest: " +
                "(ERROR) Create did not fail with expected SecurityException");
            errorCount++;
        } else {
            System.out.println("ClientSide::doCreateRequest: (OK) Create succeed") ;
        }
    } catch(Exception e) {
        Utils.printThrowable(e, true) ;
        if (expectedException) {
            if (e instanceof java.lang.SecurityException) {
                System.out.println("ClientSide::doCreateRequest: " +
                    "(OK) Create failed with expected SecurityException") ;
            } else {
                System.out.println("ClientSide::doCreateRequest: " +
                    "(ERROR) Create failed with " +
                    e.getClass() + " instead of expected SecurityException");
                errorCount++;
            }
        } else {
            System.out.println("ClientSide::doCreateRequest: " +
                "(ERROR) Create failed");
            errorCount++;
        }
    }
    return errorCount;
}
 
Example 9
Source File: ConcurrentModificationTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void mbeanOp(MBeanServerConnection mserver, ObjectName name, boolean adding)
        throws Exception {
    if (adding) {
        mserver.createMBean("javax.management.timer.Timer", name);
    } else {
        mserver.unregisterMBean(name);
    }
}
 
Example 10
Source File: AuthorizationTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected int doCreateRequest(MBeanServerConnection mbsc,
                              ObjectName on,
                              boolean expectedException) {
    int errorCount = 0;

    try {
        Utils.debug(Utils.DEBUG_STANDARD,
            "ClientSide::doCreateRequest: Create and register the MBean") ;

        mbsc.createMBean("Simple", on) ;

        if (expectedException) {
            System.out.println("ClientSide::doCreateRequest: " +
                "(ERROR) Create did not fail with expected SecurityException");
            errorCount++;
        } else {
            System.out.println("ClientSide::doCreateRequest: (OK) Create succeed") ;
        }
    } catch(Exception e) {
        Utils.printThrowable(e, true) ;
        if (expectedException) {
            if (e instanceof java.lang.SecurityException) {
                System.out.println("ClientSide::doCreateRequest: " +
                    "(OK) Create failed with expected SecurityException") ;
            } else {
                System.out.println("ClientSide::doCreateRequest: " +
                    "(ERROR) Create failed with " +
                    e.getClass() + " instead of expected SecurityException");
                errorCount++;
            }
        } else {
            System.out.println("ClientSide::doCreateRequest: " +
                "(ERROR) Create failed");
            errorCount++;
        }
    }
    return errorCount;
}
 
Example 11
Source File: MXBeanWeirdParamTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String args[]) throws Exception {

            int errorCount = 0 ;
            String msgTag = "ClientSide::main: ";

            try {

                // Get a connection to remote mbean server
                JMXServiceURL addr = new JMXServiceURL(args[0]);
                JMXConnector cc = JMXConnectorFactory.connect(addr);
                MBeanServerConnection mbsc = cc.getMBeanServerConnection();

                // ----
                System.out.println(msgTag + "Create and register the MBean");
                ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;
                mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                System.out.println(msgTag +"Get attribute SqeParameterAtt on our MXBean");
                Object result = mbsc.getAttribute(objName, "SqeParameterAtt");
                System.out.println(msgTag +"(OK) Got result of class "
                        + result.getClass().getName());
                System.out.println(msgTag +"Received CompositeData is " + result);
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                // We use the value returned by getAttribute to perform the invoke.
                System.out.println(msgTag +"Call operation doWeird on our MXBean [1]");
                mbsc.invoke(objName, "doWeird",
                        new Object[]{result},
                        new String[]{"javax.management.openmbean.CompositeData"});
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                // We build the CompositeData ourselves that time.
                System.out.println(msgTag +"Call operation doWeird on our MXBean [2]");
                String typeName = "SqeParameter";
                String[] itemNames = new String[] {"glop"};
                OpenType<?>[] openTypes = new OpenType<?>[] {SimpleType.STRING};
                CompositeType rowType = new CompositeType(typeName, typeName,
                        itemNames, itemNames, openTypes);
                Object[] itemValues = {"HECTOR"};
                CompositeData data =
                        new CompositeDataSupport(rowType, itemNames, itemValues);
                TabularType tabType = new TabularType(typeName, typeName,
                        rowType, new String[]{"glop"});
                TabularDataSupport tds = new TabularDataSupport(tabType);
                tds.put(data);
                System.out.println(msgTag +"Source CompositeData is " + data);
                mbsc.invoke(objName, "doWeird",
                        new Object[]{data},
                        new String[]{"javax.management.openmbean.CompositeData"});
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                System.out.println(msgTag +"Unregister the MBean");
                mbsc.unregisterMBean(objName);
                System.out.println(msgTag +"---- OK\n") ;

                // Terminate the JMX Client
                cc.close();

            } catch(Exception e) {
                Utils.printThrowable(e, true) ;
                errorCount++;
                throw new RuntimeException(e);
            } finally {
                System.exit(errorCount);
            }
        }
 
Example 12
Source File: EmptyDomainNotificationTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        final MBeanServer mbs = MBeanServerFactory.createMBeanServer();

        final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");

        JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        server.start();

        JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);

        final MBeanServerConnection mbsc = client.getMBeanServerConnection();

        final ObjectName mbean = ObjectName.getInstance(":type=Simple");
        mbsc.createMBean(Simple.class.getName(), mbean);

        System.out.println("EmptyDomainNotificationTest-main: add a listener ...");
        final Listener li = new Listener();
        mbsc.addNotificationListener(mbean, li, null, null);

        System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");
        mbsc.invoke(mbean, "emitNotification", null, null);

        System.out.println("EmptyDomainNotificationTest-main: waiting notif...");
        final long stopTime = System.currentTimeMillis() + 2000;
        synchronized(li) {
            long toWait = stopTime - System.currentTimeMillis();

            while (li.received < 1 && toWait > 0) {
                li.wait(toWait);

                toWait = stopTime - System.currentTimeMillis();
            }
        }

        if (li.received < 1) {
            throw new RuntimeException("No notif received!");
        } else if (li.received > 1) {
            throw new RuntimeException("Wait one notif but got: "+li.received);
        }

        System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");

        System.out.println("EmptyDomainNotificationTest-main: remove the listener.");
        mbsc.removeNotificationListener(mbean, li);

        // clean
        client.close();
        server.stop();

        System.out.println("EmptyDomainNotificationTest-main: Bye.");
    }
 
Example 13
Source File: MXBeanWeirdParamTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String args[]) throws Exception {

            int errorCount = 0 ;
            String msgTag = "ClientSide::main: ";

            try {

                // Get a connection to remote mbean server
                JMXServiceURL addr = new JMXServiceURL(args[0]);
                JMXConnector cc = JMXConnectorFactory.connect(addr);
                MBeanServerConnection mbsc = cc.getMBeanServerConnection();

                // ----
                System.out.println(msgTag + "Create and register the MBean");
                ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;
                mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                System.out.println(msgTag +"Get attribute SqeParameterAtt on our MXBean");
                Object result = mbsc.getAttribute(objName, "SqeParameterAtt");
                System.out.println(msgTag +"(OK) Got result of class "
                        + result.getClass().getName());
                System.out.println(msgTag +"Received CompositeData is " + result);
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                // We use the value returned by getAttribute to perform the invoke.
                System.out.println(msgTag +"Call operation doWeird on our MXBean [1]");
                mbsc.invoke(objName, "doWeird",
                        new Object[]{result},
                        new String[]{"javax.management.openmbean.CompositeData"});
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                // We build the CompositeData ourselves that time.
                System.out.println(msgTag +"Call operation doWeird on our MXBean [2]");
                String typeName = "SqeParameter";
                String[] itemNames = new String[] {"glop"};
                OpenType<?>[] openTypes = new OpenType<?>[] {SimpleType.STRING};
                CompositeType rowType = new CompositeType(typeName, typeName,
                        itemNames, itemNames, openTypes);
                Object[] itemValues = {"HECTOR"};
                CompositeData data =
                        new CompositeDataSupport(rowType, itemNames, itemValues);
                TabularType tabType = new TabularType(typeName, typeName,
                        rowType, new String[]{"glop"});
                TabularDataSupport tds = new TabularDataSupport(tabType);
                tds.put(data);
                System.out.println(msgTag +"Source CompositeData is " + data);
                mbsc.invoke(objName, "doWeird",
                        new Object[]{data},
                        new String[]{"javax.management.openmbean.CompositeData"});
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                System.out.println(msgTag +"Unregister the MBean");
                mbsc.unregisterMBean(objName);
                System.out.println(msgTag +"---- OK\n") ;

                // Terminate the JMX Client
                cc.close();

            } catch(Exception e) {
                Utils.printThrowable(e, true) ;
                errorCount++;
                throw new RuntimeException(e);
            } finally {
                System.exit(errorCount);
            }
        }
 
Example 14
Source File: JMXAccessorCreateTask.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * create new Mbean and when set from ClassLoader Objectname
 * @param jmxServerConnection
 * @param name
 * @return The value of the given named attribute
 * @throws Exception
 */
protected String jmxCreate(MBeanServerConnection jmxServerConnection,
        String name) throws Exception {
    String error = null;
    Object argsA[] = null;
    String sigA[] = null;
    if (args != null) {
       argsA = new Object[ args.size()];
       sigA = new String[args.size()];
       for( int i=0; i<args.size(); i++ ) {
           Arg arg=args.get(i);
           if (arg.getType() == null) {
               arg.setType("java.lang.String");
               sigA[i]=arg.getType();
               argsA[i]=arg.getValue();
           } else {
               sigA[i]=arg.getType();
               argsA[i]=convertStringToType(arg.getValue(),arg.getType());
           }                
       }
    }
    if (classLoader != null && !"".equals(classLoader)) {
        if (isEcho()) {
            handleOutput("create MBean " + name + " from class "
                    + className + " with classLoader " + classLoader);
        }
        if(args == null)
            jmxServerConnection.createMBean(className, new ObjectName(name), new ObjectName(classLoader));
        else
            jmxServerConnection.createMBean(className, new ObjectName(name), new ObjectName(classLoader),argsA,sigA);
            
    } else {
        if (isEcho()) {
            handleOutput("create MBean " + name + " from class "
                    + className);
        }
        if(args == null)
            jmxServerConnection.createMBean(className, new ObjectName(name));
        else
            jmxServerConnection.createMBean(className, new ObjectName(name),argsA,sigA);
    }
    return error;
}
 
Example 15
Source File: EmptyDomainNotificationTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        final MBeanServer mbs = MBeanServerFactory.createMBeanServer();

        final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");

        JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        server.start();

        JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);

        final MBeanServerConnection mbsc = client.getMBeanServerConnection();

        final ObjectName mbean = ObjectName.getInstance(":type=Simple");
        mbsc.createMBean(Simple.class.getName(), mbean);

        System.out.println("EmptyDomainNotificationTest-main: add a listener ...");
        final Listener li = new Listener();
        mbsc.addNotificationListener(mbean, li, null, null);

        System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");
        mbsc.invoke(mbean, "emitNotification", null, null);

        System.out.println("EmptyDomainNotificationTest-main: waiting notif...");
        final long stopTime = System.currentTimeMillis() + 2000;
        synchronized(li) {
            long toWait = stopTime - System.currentTimeMillis();

            while (li.received < 1 && toWait > 0) {
                li.wait(toWait);

                toWait = stopTime - System.currentTimeMillis();
            }
        }

        if (li.received < 1) {
            throw new RuntimeException("No notif received!");
        } else if (li.received > 1) {
            throw new RuntimeException("Wait one notif but got: "+li.received);
        }

        System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");

        System.out.println("EmptyDomainNotificationTest-main: remove the listener.");
        mbsc.removeNotificationListener(mbean, li);

        // clean
        client.close();
        server.stop();

        System.out.println("EmptyDomainNotificationTest-main: Bye.");
    }
 
Example 16
Source File: EmptyDomainNotificationTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        final MBeanServer mbs = MBeanServerFactory.createMBeanServer();

        final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");

        JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        server.start();

        JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);

        final MBeanServerConnection mbsc = client.getMBeanServerConnection();

        final ObjectName mbean = ObjectName.getInstance(":type=Simple");
        mbsc.createMBean(Simple.class.getName(), mbean);

        System.out.println("EmptyDomainNotificationTest-main: add a listener ...");
        final Listener li = new Listener();
        mbsc.addNotificationListener(mbean, li, null, null);

        System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");
        mbsc.invoke(mbean, "emitNotification", null, null);

        System.out.println("EmptyDomainNotificationTest-main: waiting notif...");
        final long stopTime = System.currentTimeMillis() + 2000;
        synchronized(li) {
            long toWait = stopTime - System.currentTimeMillis();

            while (li.received < 1 && toWait > 0) {
                li.wait(toWait);

                toWait = stopTime - System.currentTimeMillis();
            }
        }

        if (li.received < 1) {
            throw new RuntimeException("No notif received!");
        } else if (li.received > 1) {
            throw new RuntimeException("Wait one notif but got: "+li.received);
        }

        System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");

        System.out.println("EmptyDomainNotificationTest-main: remove the listener.");
        mbsc.removeNotificationListener(mbean, li);

        // clean
        client.close();
        server.stop();

        System.out.println("EmptyDomainNotificationTest-main: Bye.");
    }
 
Example 17
Source File: MXBeanWeirdParamTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String args[]) throws Exception {

            int errorCount = 0 ;
            String msgTag = "ClientSide::main: ";

            try {

                // Get a connection to remote mbean server
                JMXServiceURL addr = new JMXServiceURL(args[0]);
                JMXConnector cc = JMXConnectorFactory.connect(addr);
                MBeanServerConnection mbsc = cc.getMBeanServerConnection();

                // ----
                System.out.println(msgTag + "Create and register the MBean");
                ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;
                mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                System.out.println(msgTag +"Get attribute SqeParameterAtt on our MXBean");
                Object result = mbsc.getAttribute(objName, "SqeParameterAtt");
                System.out.println(msgTag +"(OK) Got result of class "
                        + result.getClass().getName());
                System.out.println(msgTag +"Received CompositeData is " + result);
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                // We use the value returned by getAttribute to perform the invoke.
                System.out.println(msgTag +"Call operation doWeird on our MXBean [1]");
                mbsc.invoke(objName, "doWeird",
                        new Object[]{result},
                        new String[]{"javax.management.openmbean.CompositeData"});
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                // We build the CompositeData ourselves that time.
                System.out.println(msgTag +"Call operation doWeird on our MXBean [2]");
                String typeName = "SqeParameter";
                String[] itemNames = new String[] {"glop"};
                OpenType<?>[] openTypes = new OpenType<?>[] {SimpleType.STRING};
                CompositeType rowType = new CompositeType(typeName, typeName,
                        itemNames, itemNames, openTypes);
                Object[] itemValues = {"HECTOR"};
                CompositeData data =
                        new CompositeDataSupport(rowType, itemNames, itemValues);
                TabularType tabType = new TabularType(typeName, typeName,
                        rowType, new String[]{"glop"});
                TabularDataSupport tds = new TabularDataSupport(tabType);
                tds.put(data);
                System.out.println(msgTag +"Source CompositeData is " + data);
                mbsc.invoke(objName, "doWeird",
                        new Object[]{data},
                        new String[]{"javax.management.openmbean.CompositeData"});
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                System.out.println(msgTag +"Unregister the MBean");
                mbsc.unregisterMBean(objName);
                System.out.println(msgTag +"---- OK\n") ;

                // Terminate the JMX Client
                cc.close();

            } catch(Exception e) {
                Utils.printThrowable(e, true) ;
                errorCount++;
                throw new RuntimeException(e);
            } finally {
                System.exit(errorCount);
            }
        }
 
Example 18
Source File: MXBeanWeirdParamTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String args[]) throws Exception {

            int errorCount = 0 ;
            String msgTag = "ClientSide::main: ";

            try {

                // Get a connection to remote mbean server
                JMXServiceURL addr = new JMXServiceURL(args[0]);
                JMXConnector cc = JMXConnectorFactory.connect(addr);
                MBeanServerConnection mbsc = cc.getMBeanServerConnection();

                // ----
                System.out.println(msgTag + "Create and register the MBean");
                ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;
                mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                System.out.println(msgTag +"Get attribute SqeParameterAtt on our MXBean");
                Object result = mbsc.getAttribute(objName, "SqeParameterAtt");
                System.out.println(msgTag +"(OK) Got result of class "
                        + result.getClass().getName());
                System.out.println(msgTag +"Received CompositeData is " + result);
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                // We use the value returned by getAttribute to perform the invoke.
                System.out.println(msgTag +"Call operation doWeird on our MXBean [1]");
                mbsc.invoke(objName, "doWeird",
                        new Object[]{result},
                        new String[]{"javax.management.openmbean.CompositeData"});
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                // We build the CompositeData ourselves that time.
                System.out.println(msgTag +"Call operation doWeird on our MXBean [2]");
                String typeName = "SqeParameter";
                String[] itemNames = new String[] {"glop"};
                OpenType<?>[] openTypes = new OpenType<?>[] {SimpleType.STRING};
                CompositeType rowType = new CompositeType(typeName, typeName,
                        itemNames, itemNames, openTypes);
                Object[] itemValues = {"HECTOR"};
                CompositeData data =
                        new CompositeDataSupport(rowType, itemNames, itemValues);
                TabularType tabType = new TabularType(typeName, typeName,
                        rowType, new String[]{"glop"});
                TabularDataSupport tds = new TabularDataSupport(tabType);
                tds.put(data);
                System.out.println(msgTag +"Source CompositeData is " + data);
                mbsc.invoke(objName, "doWeird",
                        new Object[]{data},
                        new String[]{"javax.management.openmbean.CompositeData"});
                System.out.println(msgTag +"---- OK\n") ;

                // ----
                System.out.println(msgTag +"Unregister the MBean");
                mbsc.unregisterMBean(objName);
                System.out.println(msgTag +"---- OK\n") ;

                // Terminate the JMX Client
                cc.close();

            } catch(Exception e) {
                Utils.printThrowable(e, true) ;
                errorCount++;
                throw new RuntimeException(e);
            } finally {
                System.exit(errorCount);
            }
        }
 
Example 19
Source File: ScanManager.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create and register a new singleton instance of the ScanManager
 * MBean in the given {@link MBeanServerConnection}.
 * @param mbs The MBeanServer in which the new singleton instance
 *         should be created.
 * @throws JMException The MBeanServer connection raised an exception
 *         while trying to instantiate and register the singleton MBean
 *         instance.
 * @throws IOException There was a connection problem while trying to
 *         communicate with the underlying MBeanServer.
 * @return A proxy for the registered MBean.
 **/
public static ScanManagerMXBean register(MBeanServerConnection mbs)
    throws IOException, JMException {
    final ObjectInstance moi =
            mbs.createMBean(ScanManager.class.getName(),SCAN_MANAGER_NAME);
    final ScanManagerMXBean proxy =
            JMX.newMXBeanProxy(mbs,moi.getObjectName(),
                              ScanManagerMXBean.class,true);
    return proxy;
}
 
Example 20
Source File: ScanManager.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create and register a new singleton instance of the ScanManager
 * MBean in the given {@link MBeanServerConnection}.
 * @param mbs The MBeanServer in which the new singleton instance
 *         should be created.
 * @throws JMException The MBeanServer connection raised an exception
 *         while trying to instantiate and register the singleton MBean
 *         instance.
 * @throws IOException There was a connection problem while trying to
 *         communicate with the underlying MBeanServer.
 * @return A proxy for the registered MBean.
 **/
public static ScanManagerMXBean register(MBeanServerConnection mbs)
    throws IOException, JMException {
    final ObjectInstance moi =
            mbs.createMBean(ScanManager.class.getName(),SCAN_MANAGER_NAME);
    final ScanManagerMXBean proxy =
            JMX.newMXBeanProxy(mbs,moi.getObjectName(),
                              ScanManagerMXBean.class,true);
    return proxy;
}