JMX 101

Add -Dcom.sun.management.jmxremote as VM Arguments

package com.aayush.tutorial;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class Main {
public static void main(String[] args) {
//Creating JMX Client
Main main = new Main();
try {
System.out.println("JMX Client is running - Press <q> to End");
char c = (char) System.in.read();
while (c != 'q') {
System.out.println("JMX Client is running - Press <q> to End");
Thread.sleep(1000);
c = (char) System.in.read();
}
System.out.println("Quitting MBean Server");
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Main() {
System.out.println("I am creating JMX Client");
//1. Create MBeanServer
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
//Create a ManagedBean Object
Person person = new Person();
try {
//Create ObjectName for the JMX_Name
ObjectName beanname = new ObjectName("Foo:name=PersonBean");
mbs.registerMBean(person, beanname);
} catch (MalformedObjectNameException | InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
view raw Main.java hosted with ❤ by GitHub
package com.aayush.tutorial;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Person implements PersonMBean{
private String name = "UserName";
@Override
public void setName(String _name) {
this.name = _name;
}
@Override
public String displayName() {
return "Hello" + name;
}
public void sayHello() {
//Displaying Text using Swing
JFrame f = new JFrame();
f.setVisible(true);
f.setTitle("Hello to JSwing");
f.setSize(300, 300);
JPanel panel = new JPanel();
JLabel lablel = new JLabel("Hello "+ name);
panel.add(lablel);
f.add(panel);
}
//JMX Method without using Attribute Values
public int add(int x, int y) {
return x + y;
}
//JMX Method using Attribute Values
public String getName() {
return this.name;
}
}
view raw Person.java hosted with ❤ by GitHub
package com.aayush.tutorial;
public interface PersonMBean {
//Setter Getter for Name
public void setName(String name);
public String getName();
//Using JFrame to display the Result
public void sayHello();
public int add(int x, int y);
public String displayName();
}