- Creating a Custom JMX Client
- Importing the JMX Remote API Classes
- Creating a Notification Listener
- Creating an RMI Connector Client
- Connecting to the Remote MBean Server
- Performing Operations on Remote MXBeans via Proxies
- Closing the Connection
- To Run the Custom JMX Client Example
- Java JMX Client Example – JMX Authentication [Latest]
- Java JMX Client
- JMX Client Example
Creating a Custom JMX Client
The previous lessons in this trail have shown you how to create JMX technology MBeans and MXBeans, and register them with a JMX agent. However, all the previous examples have used an existing JMX client, JConsole. This lesson will demonstrate how to create your own custom JMX client.
An example of a custom JMX client, Client is included in jmx_examples.zip . This JMX client interacts with the same MBean, MXBean and JMX agent as were seen in the previous lessons. Due to the size of the Client class, it will be examined in chunks, in the following sections.
Importing the JMX Remote API Classes
To be able to create connections to JMX agents that are running remotely from the JMX client, you need to use the classes from the javax.management.remote .
package com.example; . import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; public class Client < .
The Client class will be creating JMXConnector instances, for which it will need a JMXConnectorFactory and a JMXServiceURL .
Creating a Notification Listener
The JMX client needs a notification handler, to listen for and to process any notifications that might be sent by the MBeans that are registered in the JMX agent's MBean server. The JMX client's notification handler is an instance of the NotificationListener interface, as shown below.
. public static class ClientListener implements NotificationListener < public void handleNotification(Notification notification, Object handback) < echo("\nReceived notification:"); echo("\tClassName: " + notification.getClass().getName()); echo("\tSource: " + notification.getSource()); echo("\tType: " + notification.getType()); echo("\tMessage: " + notification.getMessage()); if (notification instanceof AttributeChangeNotification) < AttributeChangeNotification acn = (AttributeChangeNotification) notification; echo("\tAttributeName: " + acn.getAttributeName()); echo("\tAttributeType: " + acn.getAttributeType()); echo("\tNewValue: " + acn.getNewValue()); echo("\tOldValue: " + acn.getOldValue()); >> > .
This notification listener determines the origin of any notifications it receives, and retrieves the information stored in the notification. It then performs different actions with the notification information according to the type of notification received. In this case, when the listener receives notifications of the type AttributeChangeNotification it will obtain the name and type of the MBean attribute that has changed, as well as its old and new values, by calling the AttributeChangeNotification methods getAttributeName , getAttributeType , getNewValue and getOldValue .
A new ClientListener instance is created by later in the code.
ClientListener listener = new ClientListener();
Creating an RMI Connector Client
The Client class creates an RMI connector client that is configured to connect to an RMI connector server that you will launch when you start the JMX agent, Main . This will allow the JMX client to interact with the JMX agent as if they were running on the same machine.
. public static void main(String[] args) throws Exception < echo("\nCreate an RMI connector client and " + "connect it to the RMI connector server"); JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi"); JMXConnector jmxc = JMXConnectorFactory.connect(url, null); .
As you can see, the Client defines a JMXServiceURL named url , that represents the location at which the connector client expects to find the connector server. This URL allows the connector client to retrieve the RMI connector server stub jmxrmi from the RMI registry running on port 9999 of the local host, and to connect to the RMI connector server.
With the RMI registry thus identified, the connector client can be created. The connector client, jmxc , is an instance of the interface JMXConnector , created by the connect() method of JMXConnectorFactory . The connect() method is passed the parameters url and a null environment map when it is called.
Connecting to the Remote MBean Server
With the RMI connection in place, the JMX client must connect to the remote MBean server, so that it can interact with the various MBeans registered in it by the remote JMX agent.
. MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); .
An instance of MBeanServerConnection , named mbsc, is then created by calling the getMBeanServerConnection() method of the JMXConnector instance jmxc .
The connector client is now connected to the MBean server created by the JMX agent, and can register MBeans and perform operations on them with the connection remaining completely transparent to both ends.
To start with, the client defines some simple operations to discover information about the MBeans found in the agent's MBean server.
. echo("\nDomains:"); String domains[] = mbsc.getDomains(); Arrays.sort(domains); for (String domain : domains) < echo("\tDomain = " + domain); >. echo("\nMBeanServer default domain = " + mbsc.getDefaultDomain()); echo("\nMBean count = " + mbsc.getMBeanCount()); echo("\nQuery MBeanServer MBeans:"); Set names = new TreeSet(mbsc.queryNames(null, null)); for (ObjectName name : names) < echo("\tObjectName codeblock">. ObjectName mbeanName = new ObjectName("com.example:type=Hello"); HelloMBean mbeanProxy = JMX.newMBeanProxy(mbsc, mbeanName, HelloMBean.class, true); echo("\nAdd notification listener. "); mbsc.addNotificationListener(mbeanName, listener, null, null); echo("\nCacheSize = " + mbeanProxy.getCacheSize()); mbeanProxy.setCacheSize(150); echo("\nWaiting for notification. "); sleep(2000); echo("\nCacheSize = " + mbeanProxy.getCacheSize()); echo("\nInvoke sayHello() in Hello MBean. "); mbeanProxy.sayHello(); echo("\nInvoke add(2, 3) in Hello MBean. "); echo("\nadd(2, 3) APILink" target="_blank" href="https://docs.oracle.com/javase/8/docs/api/javax/management/JMX.html">javax.management.JMX
class, passing it the MBean'sMBeanServerConnection
, object name, the class name of the MBean interface andtrue
, to signify that the proxy must behave as aNotificationBroadcaster
. The JMX client can now perform the operations defined byHello
as if they were the operations of a locally registered MBean. The JMX client also adds a notification listener and changes the MBean'sCacheSize
attribute, to make it send a notification.Performing Operations on Remote MXBeans via Proxies
You can create proxies for MXBeans in exactly the same way as you create MBean proxies.
. ObjectName mxbeanName = new ObjectName ("com.example:type=QueueSampler"); QueueSamplerMXBean mxbeanProxy = JMX.newMXBeanProxy(mbsc, mxbeanName, QueueSamplerMXBean.class); QueueSample queue1 = mxbeanProxy.getQueueSample(); echo("\nQueueSample.Date = " + queue1.getDate()); echo("QueueSample.Head = " + queue1.getHead()); echo("QueueSample.Size = " + queue1.getSize()); echo("\nInvoke clearQueue() in QueueSampler MXBean. "); mxbeanProxy.clearQueue(); QueueSample queue2 = mxbeanProxy.getQueueSample(); echo("\nQueueSample.Date = " + queue2.getDate()); echo("QueueSample.Head = " + queue2.getHead()); echo("QueueSample.Size APILink" target="_blank" href="https://docs.oracle.com/javase/8/docs/api/javax/management/JMX.html#newMXBeanProxy-javax.management.MBeanServerConnection-javax.management.ObjectName-java.lang.Class-">JMX.newMXBeanProxy
instead ofnewMBeanProxy
. The MXBean proxymxbeanProxy
allows the client to invoke theQueueSample
MXBean's operations as if they were the operations of a locally registered MXBean.Closing the Connection
Once the JMX client has obtained all the information it needs and performed all the required operations on the MBeans in the remote JMX agent's MBean server, the connection must be closed down.
jmxc.close();The connection is closed with a call to the JMXConnector.close method.
To Run the Custom JMX Client Example
This example requires version 6 of the Java SE platform. To monitor the Main JMX agent remotely using a custom JMX client Client , follow these steps:
- If you have not done so already, save jmx_examples.zip into your work_dir directory.
- Unzip the bundle of sample classes by using the following command in a terminal window.
java -Dcom.sun.management.jmxremote.port=9999 \ -Dcom.sun.management.jmxremote.authenticate=false \ -Dcom.sun.management.jmxremote.ssl=false \ com.example.Main
- A notification listener is added to Client to listen for notifications from Main .
- The value of the CacheSize attribute is changed from 200 to 150.
- In the terminal window in which you started Main , confirmation of the CacheSize attribute change is displayed.
- In the terminal window in which you started Client , a notification from Main is displayed, informing Client of the CacheSize attribute change.
- The Hello MBean's sayHello operation is invoked.
- In the terminal window in which you started Main , the message "Hello world" is displayed.
- The Hello MBean's add operation is invoked, with the values 2 and 3 as parameters. The result is displayed by Client .
- The QueueSample values date , head , and size are displayed.
- The clearQueue operation is invoked.
Java JMX Client Example – JMX Authentication [Latest]
In last tutorial we learned about JMX Basics and how can we use JConsole to manage MBeans. Today we will look into java jmx client example and role based authentication through config files.
Java JMX Client
Although JConsole provides a graphical view but it requires human effort to work with MBean and not suitable where you want to invoke some features of MBean periodically. For example, you have a MBean that provides the current state of application and you want to invoke it to check the state every 10 minutes. In this case, having a java program that can work as JMX Client to connect to JMX MBean server and invoke MBean operations are very useful.
JMX Client Example
Here we will write a java program that can connect to MBean server and create a proxy application to invoke MBean operations. I will use the MBean application created in JMX Tutorial and use our client program to connect to MBean. Later on we will check how to make our MBean server secure using JMX Configuration files for role based access and how we can use credentials in the JMX Client to connect to MBean server with correct role.
Before writing JMX Client application, we need to know the port where MBean server is running. Also for starter, we will disable all the authentication. We can do all these by passing correct java options.
So I will start my MBean with following command. Notice the java options specified for MBean server port and disabling SSL and authentication.