Showing posts with label JConsole. Show all posts
Showing posts with label JConsole. Show all posts

Sunday, January 26, 2014

How to demo Garbage Collection, JConsole and VisualVM

First, create with JDK 7 this Java Project in Eclipse:
package com.pierre.gctests;

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;

public class GCTestMain {

 private static void init() throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
  MBeanServer mbs = null;
  mbs = ManagementFactory.getPlatformMBeanServer();
  GCTestAgent agent = new GCTestAgent();
  ObjectName agentName;
  agentName = new ObjectName("PVTests:name=GCTestAgent");
  mbs.registerMBean(agent, agentName);
 }
 
 public static void main(String[] args) throws Exception {
  init();
  for (;;) {
   Thread.sleep(1000);
  }
 }
}
package com.pierre.gctests;

public interface GCTestAgentMBean {
 void newThread(String threadName);
 void newCollectableObject(int size);
 void newLeakedObject(int size);
 void clearLeaked();
 void cpuIntensiveOperation(int iterations);
}


package com.pierre.gctests;

import java.util.ArrayList;
import java.util.Date;

public class GCTestAgent implements GCTestAgentMBean, Runnable {
 ArrayList<Object> leakingMap = new ArrayList<Object>(); 
 volatile double val = 10;

 @Override
 public void newThread(String threadName) {
  Thread newThread = new Thread(this);
  newThread.setName(threadName);
  newThread.start();
 }

 @Override
 public void newCollectableObject(int size) {
  createObject(size);
 }

 private Object createObject(int size) {
  ArrayList<String> list = new ArrayList<String>();
  for (int i = 0; i < size; i++) {
   list.add( (new Date()).toString() + " " +  i);
  }
  return list;
 }

 @Override
 public void newLeakedObject(int size) {
  leakingMap.add(createObject(size));
 }

 @Override
 public void run() {
  for (;;) {
   System.out.println(Thread.currentThread().getName());
   try {
    Thread.sleep(10000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }

 @Override
 public void clearLeaked() {
  leakingMap.clear();
 }

 @Override
 public void cpuIntensiveOperation(int iterations) {
  int[] myArrayToBeSorted = new int[] {4,2,6,7,2,1,6};
  for (int i = 0; i < iterations; i++) {
   for (int j = 0; j < myArrayToBeSorted.length - 1; j++) {
    myArrayToBeSorted[j] = myArrayToBeSorted[j] + myArrayToBeSorted[j + 1];
   }
  }
 }

}






Then, install the VisualVM GC plugin. Run the GCTestMain main, using these JVM arguments: -verbose:gc -Xms256m -Xmx256m. THen connect with JConsole and with VisualVM (I downloaded the one from the main website.... beware that there can be issues on connection when running on Windows when the username has uppercase characters (Windows sucks, don't forget).



Thursday, May 3, 2012

WebLogic monitoring with JMX and JConsole

This great post explains you everything


cd C:\Oracle\Middleware\wlserver_10.3\server\lib
java -jar wljarbuilder.jar
echo %JAVA_HOME%

C:\Oracle\MIDDLE~1\JDK160~1
cd %JAVA_HOME%
cd bin
jconsole -J-Djava.class.path=C:\Oracle\MIDDLE~1\JDK160~1\lib\jconsole.jar;C:\Oracle\MIDDLE~1\JDK160~1\lib\tools.jar;C:\Oracle\Middleware\wlserver_10.3\server\lib\wlfullclient.jar -J-Djmx.remote.protocol.provider.pkgs=weblogic.management.remote -debug



(make sure you enter it like that, with the -J-D which looks very weird)

when jconsole comes up, choose remote connection,
provide
service:jmx:iiop://yourhost:yourport/jndi/weblogic.management.mbeanservers.runtime

(your port is NOT the jmx port provided in -Dcom.sun.management.jmxremote.port=8888, but the listen port of the WLS)
and enter your weblogic username and password to authenticate


You can also find MBean Object Names with weblogic.Admin:


java weblogic.Admin -url t3://myserver.acme.com:8105 -username Pierluigi -password weblogic1 -pretty GET -type ClusterRuntime


and the MBean name is osbdv2do:ServerRuntime=osbdv2ms1,Name=osbdv2cl,Type=ClusterRuntime,Location=osbdv2ms1



---------------------------
MBeanName: "osbdv2do:ServerRuntime=osbdv2ms1,Name=osbdv2cl,Type=ClusterRuntime,Location=osbdv2ms1"
ActiveSingletonServices:
AliveServerCount: 2
CachingDisabled: true
CurrentMachine: myserver
CurrentSecondaryServer: -7551940697957911746S:myserver.acme.com:[8205,8205,-1,-1,-1,-1,-1]:myserver.acme.com:8105,myserver.acme.com:8205:osbdv2do:osbdv2ms2
DetailedSecondariesDistribution:
ForeignFragmentsDroppedCount: 0
FragmentsReceivedCount: 696
FragmentsSentCount: 805
HealthState: Component:null,State:HEALTH_OK,MBean:null,ReasonCode:[]
JobSchedulerRuntime:
MulticastMessagesLostCount: 2
Name: osbdv2cl
Parent: osbdv2ms1
PrimaryCount: 0
Registered: true
ResendRequestsCount: 1
SecondaryCount: 0
SecondaryDistributionNames:
SecondaryServerDetails: -7551940697957911746S:myserver.acme.com:[8205,8205,-1,-1,-1,-1,-1]:myserver.acme.com:8105,myserver.acme.com:8205:osbdv2do:osbdv2ms2
ServerMigrationRuntime: ServerMigrationRuntime
ServerNames: osbdv2ms2,osbdv2ms1
Type: ClusterRuntime
UnicastMessaging: UnicastMessagingRuntime




wlst jmx objectname for clusterruntime:

This is the way to find the OBJECT_NAME associated to a MBean with WLST:

http://docs.oracle.com/cd/E23943_01/web.1111/e13715/nav_edit.htm#i1001906

java weblogic.WLST

connect(...)
serverRuntime()
easeSyntax()
cd ClusterRuntime
cd osbdv2cluster
cmo

[MBeanServerInvocationHandler]com.bea:ServerRuntime=osbdv2ms1,Name=osbdv2cluster,Type=ClusterRuntime

so com.bea:ServerRuntime=osbdv2ms1,Name=osbdv2cluster,Type=ClusterRuntime is the OBJECT_NAME

you can extract attributes: ServerNames et AliveServerCount


You can test if your OBJECT_NAME is correct with "weblogic.Admin GET -mbean":

http://docs.oracle.com/cd/E13222_01/wls/docs81/admin_ref/cli30.html#1035148

java weblogic.Admin -url t3://myserver.acme.com:8105 -username pippo -password weblogic1 -pretty GET -mbean osbdv2do:ServerRuntime=osbdv2ms1,Name=osbdv2cl,Type=ClusterRuntime,Location=osbdv2ms1


To enable remote JMX connection, in $DOMAIN_HOME/bin/setDomainEnv.sh, almost at the bottom, add:

if [ "${SERVER_NAME}" = "osbdv2ms1" ] ; then
JAVA_OPTIONS="${JAVA_OPTIONS} -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8888 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
fi


Here a good article on how to expose an interface through a MBean, including the HtmlAdaptorServer

Sunday, June 13, 2010

Never again without JMX

Simple tutorial to get started with exposing a Bean with JMX:
http://www.devdaily.com/blog/post/java/source-code-java-jmx-hello-world-application

and here
http://javatoolsforweblogic.googlecode.com/svn/trunk/JMXTest/

the Eclipse project in SVN (remember to set the JVM arguments as specified in the SimpleAgent javadoc)

Run SimpleAgent in Eclipse, run JConsole, connect to SimpleAgent and you can invoke the methods and inspect the attributes via JMX... fabulous!


It's great HtmlAdaptorServer to implement the solution depicted here.

To download the JDMK HtmlAdaptorServer go here: http://java.sun.com/products/jdmk/