Hi,
If we want to analyse the Heap & Non-Heap Usages of JBoss Application Server’s JVM remotely then we can use JConsole, Visual JVM etc. Apart from these utilities we can also use the following Simple JMX Code.
You can enhance this tiny code to Develop your own Mini/Light weight class where you can add your Java Mail API code to send an automated alert to the Administrator as soon as the monitoring statistics becomes abnormal or to perform some activities like collecting Thread dumps etc.
To connect to the actual JConsole remotely also we need to provide the following JAVA_OPTS inside our “$JBOSS_HOME/bin/run.conf” or inside “$PROFILE/run.conf” file (NOTE: for windows machines we can add this JAVA_OPTS inside “%JBOSS_HOME/bin/run.conf.bat” or “%PROFILE%/run.conf.bat”) and the connection url format should be:
service:jmx:rmi:///jndi/rmi://"+hostname+":"+port+"/jmxrmi
Step1). Edit the “%JBOSS_HOME%/bin/run.conf.bat” or “%JBOSS_HOME%/bin/run.bat” (Server StartScript configuration file) and then set the following JAVA_OPTS there.
set JAVA_OPTS="%JAVA_OPTS% -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=4545 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl -Djboss.platform.mbeanserver"
For Unix Based Operating System edit the “$JBOSS_HOME/bin/run.conf” OR “$JBOSS_HOME/bin/run.sh” OR “$PROFILE/run.conf” file as following:
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=4545 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl -Djboss.platform.mbeanserver"
Make sure that the 4545 is not being used by any other process. Or define any other port in above parameter which is free.
Step2). Now from any other box or from the same Box you can run the Following JMX code to get Heap & Non Heap Usages of the JVM:
“HeapAndNonHeapMonitoring.java”
import java.util.Hashtable; import java.io.*; import javax.management.MBeanServerConnection; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import javax.naming.Context; import java.lang.management.MemoryMXBean; import java.lang.management.ManagementFactory; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import javax.management.*; import javax.management.openmbean.CompositeDataSupport; public class HeapAndNonHeapMonitoring { private static MBeanServerConnection connection; private static JMXConnector connector; public static void Connection(String hostname, String port) throws IOException { Integer portInteger = Integer.valueOf(port); Hashtable h = new Hashtable(); JMXServiceURL address = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+hostname+":"+port+"/jmxrmi"); connector = JMXConnectorFactory.connect(address,null); connection = connector.getMBeanServerConnection(); System.out.println("GOT THE MBeanServerConnection---SUCCESSFULLY"); } private static void doGarbageCollection() throws Exception { ObjectName memoryMXBean=new ObjectName("java.lang:type=Memory"); connection.invoke(memoryMXBean,"gc", null, null); System.out.println("nnt------Garbage Collection Done Successfully-----"); } private static void doOperatingSystemDetails() throws Exception { ObjectName operatingSystemMXBean=new ObjectName("java.lang:type=OperatingSystem"); Object systemLoadAverage = connection.getAttribute(operatingSystemMXBean, "SystemLoadAverage"); Long freePhysicalMemory = (Long) connection.getAttribute(operatingSystemMXBean, "FreePhysicalMemorySize"); Long processCpuTime = (Long) connection.getAttribute(operatingSystemMXBean, "ProcessCpuTime"); Long committedVirtualMemorySize = (Long) connection.getAttribute(operatingSystemMXBean, "CommittedVirtualMemorySize"); Long freeSwapSpaceSize = (Long) connection.getAttribute(operatingSystemMXBean, "FreeSwapSpaceSize"); Long totalPhysicalMemorySize = (Long) connection.getAttribute(operatingSystemMXBean, "TotalPhysicalMemorySize"); Long totalSwapSpaceSize = (Long) connection.getAttribute(operatingSystemMXBean, "TotalSwapSpaceSize"); System.out.println("Operating SystemLoadAverage: " + systemLoadAverage); System.out.println("Operating System FreePhysicalMemory: " + (freePhysicalMemory/(1024*1024))+"-MB"); System.out.println("Operating System processCpuTime: " + processCpuTime); System.out.println("Operating System committedVirtualMemorySize: " + (committedVirtualMemorySize/(1024*1024))+"-MB"); System.out.println("Operating System freeSwapSpaceSize: " + (freeSwapSpaceSize/(1024*1024))+"-MB"); System.out.println("Operating System totalPhysicalMemorySize: " + (totalPhysicalMemorySize/(1024*1024))+"-MB"); System.out.println("Operating System totalSwapSpaceSize: " + (totalSwapSpaceSize/(1024*1024))+"-MB"); } private static void getHeapMemoryUsage() throws Exception { ObjectName memoryMXBean=new ObjectName("java.lang:type=Memory"); CompositeDataSupport dataSenders = (CompositeDataSupport) connection.getAttribute(memoryMXBean,"HeapMemoryUsage"); if (dataSenders != null) { Long commited = (Long) dataSenders.get("committed"); Long init = (Long) dataSenders.get("init"); Long max = (Long) dataSenders.get("max"); Long used = (Long) dataSenders.get("used"); Long percentage = ((used * 100) / max); System.out.println("nnt commited : "+commited/(1024*1024)+" MB"); System.out.println("t init : "+init/(1024*1024)+" MB"); System.out.println("t max : "+max/(1024*1024)+" MB"); System.out.println("t used : "+used/(1024*1024)+" MB"); System.out.println("t percentage : "+percentage +" %"); } } private static void getNonHeapMemoryUsage() throws Exception { ObjectName memoryMXBean=new ObjectName("java.lang:type=Memory"); CompositeDataSupport dataSenders = (CompositeDataSupport) connection.getAttribute(memoryMXBean,"NonHeapMemoryUsage"); if (dataSenders != null) { Long commited = (Long) dataSenders.get("committed"); Long init = (Long) dataSenders.get("init"); Long max = (Long) dataSenders.get("max"); Long used = (Long) dataSenders.get("used"); Long percentage = ((used * 100) / max); System.out.println("nnt commited : "+commited/(1024*1024)+" MB"); System.out.println("t init : "+init/(1024*1024)+" MB"); System.out.println("t max : "+max/(1024*1024)+" MB"); System.out.println("t used : "+used/(1024*1024)+" MB"); System.out.println("t percentage : "+percentage +" %"); } } public static void main(String[] args) throws Exception { String hostname = args[0]; String port = args[1]; Connection(hostname, port); //doGarbageCollection(); // --> use this method if you want to perform Garbage Collection System.out.println("nt----------HEAP Memory Usages---------"); getHeapMemoryUsage(); System.out.println("nt----------Non-HEAP Memory Usages---------"); getNonHeapMemoryUsage(); System.out.println("nt----------Operating System Usages---------"); doOperatingSystemDetails(); connector.close(); } }
Step3). Compile and run the above program as following:
javac HeapAndNonHeapMonitoring.java java HeapAndNonHeapMonitoring 10.10.10.10 4545
Here 10.10.10.10 is the JBoss Server Listen Address.
4545 is the port -Dcom.sun.management.jmxremote.port defined in the JAVA_OPTS
OUTPUT:
java HeapAndNonHeapMonitoring localhost 4545 GOT THE MBeanServerConnection---SUCCESSFULLY ----------HEAP Memory Usages--------- commited : 384 MB init : 58 MB max : 837 MB used : 344 MB percentage : 41 % ----------Non-HEAP Memory Usages--------- commited : 79 MB init : 23 MB max : 214 MB used : 75 MB percentage : 35 % ----------Operating System Usages--------- Operating System LoadAverage: 0.38 Operating System FreePhysicalMemory: 650-MB Operating System processCpuTime: 54850000000 Operating System committedVirtualMemorySize: 3396-MB Operating System freeSwapSpaceSize: 5813-MB Operating System totalPhysicalMemorySize: 3760-MB Operating System totalSwapSpaceSize: 5823-MB
NOTE: Do not start your JBoss instance on bind address 0.0.0.0 which creates problem while RMI communication.
.
.
Thanks
Middleware Magic Team
September 30th, 2012 on 12:13 pm
Team,
Can you help me in sharing some exclusive jboss 4 documents/knowledge material . I am new to JBOSS didnt know about it. Like in JBOSS 4.2.0 whether it has support for JMS,Datasource,Clustering..? Can we have multiple instances of JBoss like we have in weblogic domain,,?
Kindly help in understanding the basic architecture of JBOSS
September 30th, 2012 on 12:28 pm
For me this code not working in Jboss 4.2.0
getting following error :-
===============================================================================
JBoss Bootstrap Environment
JBOSS_HOME: C:UsersvivekDownloadsjbossjboss-4.2.0.GA (2)jboss-4.2.0.GA
JAVA: C:Program Files (x86)Javajdk1.6.0_25binjava
JAVA_OPTS: ” -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=4545 -Dcom.sun.management.jmxremo
te.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djavax.management.builder.initial=org.jboss.system.serve
r.jmx.MBeanServerBuilderImpl -Djboss.platform.mbeanserver” -Dprogram.name=run.bat -server -Xms128m -Xmx512m -Dsun.rmi.dg
c.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000
CLASSPATH: C:Program Files (x86)Javajdk1.6.0_25libtools.jar;C:UsersvivekDownloadsjbossjboss-4.2.0.GA (2)jbo
ss-4.2.0.GAbinrun.jar
===============================================================================
Exception in thread “main” java.lang.NoClassDefFoundError: -Dcom/sun/management/jmxremote=true -Dcom/sun/management/jmx
remote/port=4545 -Dcom/sun/management/jmxremote/authenticate=false -Dcom/sun/management/jmxremote/ssl=false -Djavax/mana
gement/builder/initial=org/jboss/system/server/jmx/MBeanServerBuilderImpl -Djboss/platform/mbeanserver
Caused by: java.lang.ClassNotFoundException: -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=45
45 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djavax.management.builder
.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl -Djboss.platform.mbeanserver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=4545 -Dcom.sun.m
anagement.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djavax.management.builder.initial=org.j
boss.system.server.jmx.MBeanServerBuilderImpl -Djboss.platform.mbeanserver. Program will exit.
April 14th, 2013 on 11:06 pm
Dear all,
firstly i would like to your thanks for you all m really this forum is helping for admins who works in middle ware… really appreciated…
and secondly
1.Iam much familiar for with weblogic , but now i have to work on jboss. iam bit confused with clustering concept when compared to weblogic and jboss . i need help, how to configure clustering in jboss 5 .
2. configuring apache webserver with jboss… (windows based ), can you please help me by posting this forumm in your site and also requesting you to mail on this on this if possible.
Awaiting for your reply.
Thanks in Advance,
Sreeni
April 23rd, 2013 on 8:11 pm
Exception in thread “main” java.lang.NoClassDefFoundError: -Dprogram/name=run/b
at ervice:jmx:rmi:///jndi/rmi://+hostname+:+port+/jmxrmi -server -Xms128m -Xmx512m -Dsun/rmi/dgc/client/gcInterval=3600000 -Dsun/rmi/dgc/server/gcInterval=3600
000 -Dcom/sun/management/jmxremote=true -Dcom/sun/management/jmxremote/port=4545
-Dcom/sun/management/jmxremote/authenticate=false -Dcom/sun/management/jmxremot
e/ssl=false -Djavax/management/builder/initial=org/jboss/system/server/jmx/MBeanServerBuilderImpl -Djboss/platform/mbeanserver
Caused by: java.lang.ClassNotFoundException: -Dprogram.name=run.bat service:jmx
:rmi:…jndi.rmi:..+hostname+:+port+.jmxrmi -server -Xms128m -Xmx512m -Dsun.rmi.
dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Dcom.sun.
management.jmxremote=true -Dcom.sun.management.jmxremote.port=4545 -Dcom.sun.man
agement.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -D
javax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderI
mpl -Djboss.platform.mbeanserver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: -Dprogram.name=run.bat service:jmx:rmi:///jndi/r
mi://+hostname+:+port+/jmxrmi -server -Xms128m -Xmx512m -Dsun.rmi.dgc.client.gcI
nterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Dcom.sun.management.jmx
remote=true -Dcom.sun.management.jmxremote.port=4545 -Dcom.sun.management.jmxrem
ote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djavax.manageme
nt.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl -Djboss.pl
atform.mbeanserver. Program will exit.
Press any key to continue . . .
April 23rd, 2013 on 8:12 pm
I am getting this error. while trying your code and things you asked to do. i am using jboss4.*
Please help me how to proceed.
March 12th, 2016 on 12:33 am
Hello. Thank you for sharing your knowledge.
I’m testing this, on jboss 6.3 with RedHat 6.5
I have compiled the java program, and when I run it, I receive the following error
java HeapAndNonHeapMonitoring 192.168.19.79 4448
Connection: Host=192.168.19.79 Port=4448
Exception in thread “main” java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: non-JRMP server at remote endpoint]
at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:369)
at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:268)
at HeapAndNonHeapMonitoring.Connection(HeapAndNonHeapMonitoring.java:25)
at HeapAndNonHeapMonitoring.main(HeapAndNonHeapMonitoring.java:100)
Caused by: javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: non-JRMP server at remote endpoint]
at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:118)
at com.sun.jndi.toolkit.url.GenericURLContext.lookup(GenericURLContext.java:203)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at javax.management.remote.rmi.RMIConnector.findRMIServerJNDI(RMIConnector.java:1936)
at javax.management.remote.rmi.RMIConnector.findRMIServer(RMIConnector.java:1903)
at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:286)
… 3 more
Caused by: java.rmi.ConnectIOException: non-JRMP server at remote endpoint
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:248)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:341)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:114)
… 8 more
The port is listen by jboss
netstat -antulp | grep 4448
tcp 0 0 ::ffff:192.168.19.79:4448 :::* LISTEN 106897/java
Have you any idea ??
Best Regards.
March 12th, 2016 on 12:58 am
Hello jconte99,
The article which you are referring here is designed to connect to JBossAS 5/6. But If you are using JBoss EAP 6.x Or JBossAS7.x then you should be using the remoting port (example:4447) and the protocol should be “remoting-jmx” something ;like following:
For more information please see:
https://github.com/jbossas/remoting-jmx
https://developer.jboss.org/wiki/UsingJconsoleToConnectToJMXOnAS7Regards
Jay SenSharma
May 26th, 2017 on 3:34 pm
I need to monitor any bean running on the tomcat server remotely using this code…how will I do it?Thanx.
May 26th, 2017 on 4:05 pm
Hello Priyanksharma,
In the apache tomcat installation you will find “bin/catalina.sh” (Example: “apache-tomcat-8.0.43/bin/catalina.sh” ) in that file you can set the
If you are using Windows Operating System then replace $JAVA_OPTS with %JAVA_OPTS% in the catalina.cmd script.
Then restart the tomcat container normally and then you should find that the tomcat has started the mentioned port “4545”. You can connect to that port using the java code or using jconsole.
Regards
Jay SenSharma
February 28th, 2018 on 4:44 pm
Hello Jay,
I am getting the following error while running the program:
Exception in thread “main” javax.management.InstanceNotFoundException: java.lang:type=Memory is not registered.
at org.jboss.mx.server.registry.BasicMBeanRegistry.get(BasicMBeanRegistry.java:526)
at org.jboss.mx.server.MBeanServerImpl.getAttribute(MBeanServerImpl.java:559)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1447)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1311)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1403)
at javax.management.remote.rmi.RMIConnectionImpl.getAttribute(RMIConnectionImpl.java:641)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$255(TCPTransport.java:683)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$1/211970770.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
at com.sun.jmx.remote.internal.PRef.invoke(Unknown Source)
at javax.management.remote.rmi.RMIConnectionImpl_Stub.getAttribute(Unknown Source)
at javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.getAttribute(RMIConnector.java:878)
at HeapAndNonHeapMonitoring.getHeapMemoryUsage(HeapAndNonHeapMonitoring.java:62)
at HeapAndNonHeapMonitoring.main(HeapAndNonHeapMonitoring.java:104)
Could you please help me with what I could be doing wrong?
Thanks!