/** * Return a proxy that implements the given interface by forwarding its methods through the given * MBean server to the named MBean. As of 1.6, the methods {@link * JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class)} and {@link * JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class, boolean)} are preferred to this * method. * * <p>This method is equivalent to {@link Proxy#newProxyInstance Proxy.newProxyInstance}<code> * (interfaceClass.getClassLoader(), * interfaces, handler)</code>. Here <code>handler</code> is the result of {@link * #MBeanServerInvocationHandler new MBeanServerInvocationHandler(connection, objectName)}, and * <code>interfaces</code> is an array that has one element if <code>notificationBroadcaster * </code> is false and two if it is true. The first element of <code>interfaces</code> is <code> * interfaceClass</code> and the second, if present, is <code>NotificationEmitter.class</code>. * * @param connection the MBean server to forward to. * @param objectName the name of the MBean within <code>connection</code> to forward to. * @param interfaceClass the management interface that the MBean exports, which will also be * implemented by the returned proxy. * @param notificationBroadcaster make the returned proxy implement {@link NotificationEmitter} by * forwarding its methods via <code>connection</code>. A call to {@link * NotificationBroadcaster#addNotificationListener} on the proxy will result in a call to * {@link MBeanServerConnection#addNotificationListener(ObjectName, NotificationListener, * NotificationFilter, Object)}, and likewise for the other methods of {@link * NotificationBroadcaster} and {@link NotificationEmitter}. * @param <T> allows the compiler to know that if the {@code interfaceClass} parameter is {@code * MyMBean.class}, for example, then the return type is {@code MyMBean}. * @return the new proxy instance. * @see JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class) */ public static <T> T newProxyInstance( MBeanServerConnection connection, ObjectName objectName, Class<T> interfaceClass, boolean notificationBroadcaster) { return JMX.newMBeanProxy(connection, objectName, interfaceClass, notificationBroadcaster); }
public void use() { JMXConnector jmxc = null; try { JMXServiceURL serviceURL = new JMXServiceURL(this.serviceUrl); jmxc = JMXConnectorFactory.connect(serviceURL, null); MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); ObjectName mbeanName = new ObjectName(MBeanName); StatusMBean mbeanProxy = JMX.newMBeanProxy(mbsc, mbeanName, StatusMBean.class, true); System.out.println(mbeanProxy.getName()); } catch (Exception e) { e.printStackTrace(); } finally { if (jmxc != null) { try { jmxc.close(); } catch (IOException e) { e.printStackTrace(); } } } }