JMX

How to Generate HeapDump in case of Windows Service ?

Hi,

Jay SenSharma

Jay SenSharma

Based on the query of Magic Subscriber “fipnova51we are writing this article.  This article is very very important in case of Windows Service.  Because if we run any Server like JBoss or WebLogic like a Windows Service then NONE of the JVM utilities like “jmap”, “jconsole” , “jps” …etc will work. So it becomes very difficult to collect the Thread Dump or Heap Dump of the Running Service.

We wrote an article using JMX to collect all the JVM details using JMX code “Remote JVM Analysis (Mini JConsole)“. Here we are extending the same program to make the JMX program to even collect the HeapDump whenever we want.

Step1). Make a Windows Service for example you can use the following Script to make your WebLogic Server as Windows Service…

echo off
SETLOCAL
set DOMAIN_NAME=Test_Domain
set USERDOMAIN_HOME=C:bea103user_projectsdomainsTest_Domain
set SERVER_NAME=AdminServer
set WLS_USER=weblogic
set WLS_PW=weblogic
set PRODUCTION_MODE=true
set JAVA_VENDOR=Sun
set JAVA_HOME=C:bea103jdk160_05
set MEM_ARGS=-Xms1024m -Xmx1024m  -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=127.0.0.1

set JAVA_OPTIONS=-Dweblogic.Stdout=C:bea103user_projectsdomainsTest_DomainASstdout.txt -Dweblogic.Stderr=C:bea103user_projectsdomainsTest_DomainASstderr.txt %MEM_ARGS%
call "C:bea103wlserver_10.3serverbininstallSvc.cmd"
ENDLOCAL

NOTE: Make sure that the -Dcom.sun.management.* flags are added properly in the JAVA_OPTIONS of the script.

Step2). Now You can also use the following Script to Uninstall your WebLogic Server Windows Service …

SETLOCAL
set WL_HOME=C:bea103wlserver_10.3
rem *** Uninstall the service
"%WL_HOME%serverbinbeasvc" -remove -svcname:"beasvc Test_Domain_AdminServer"
ENDLOCAL

Step3). Now Write the following JMX code “GenerateHeapDump.java”  inside some directory.

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 GenerateHeapDump
{
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 getHeapDump(String fileName) throws Exception
   {
      ObjectName memoryMXBean=new ObjectName("com.sun.management:type=HotSpotDiagnostic");
      Object[] params = new Object[] { "C:/WindowsService_Script/"+fileName, Boolean.TRUE };
      String[] signature = new String[] { String.class.getName(), boolean.class.getName() };
      Object result = connection.invoke(memoryMXBean, "dumpHeap" , params, signature);
      System.out.println("nt Heap Dump Generated to " +fileName);
}

public static void main(String[] args) throws Exception
{
String hostname = args[0];
String port = args[1];
Connection(hostname, port);
System.out.println("nt----------Generating Heap Dump---------");
getHeapDump("HeapDump.hprof");
connector.close();
}
}

Step4). Run your Windows Service and then compile and run your Program to generate the HeapDump.

C:WindowsService_Script>set PATH=c:bea103jdk160_05bin;%PATH%;

C:WindowsService_Script>javac GenerateHeapDump.java

C:WindowsService_Script>java GenerateHeapDump localhost 9999
GOT THE MBeanServerConnection---SUCCESSFULLY

        ----------Generating Heap Dump---------

         Heap Dump Generated to HeapDump.hprof

Step5). Now you can see that the Heap Dump is generated as desired … 🙂

.

.

Thanks

Jay SenSharma


Changing MinimumPasswordLength using JMX

Hi,

Jay SenSharma

Jay SenSharma

Here is a Simple example of changing the MinimumPasswordLength using JMX code. This sample uses New style of MBeans so u can use this JMX code for Any WerbLogic Version from WLS9.x onwards. Thanks to Mr. Alecomputacao for giving me an idea for write this very useful JMX code. http://middlewaremagic.com/weblogic/2010/08/28/changing-userlockoutduration-using-jmx-9-x-and-above/#comment-1897

This JMX code illustrates that if we want to make any configuration related changes then first of all we need to get the “EditServiceMBean” object reference first of all. This is must because from WLS9.x onwards we have Change Management Feature added as part of WebLogic Server .. It means until you dont press the “Lock & Edit” Button on the AdminConsole you cannot make any changes.

Step1). Create a directory somewhere in your file system like :  F:DELETE919

Step2). Write the following JMX Code inside the above directory “ChangeMinimumPasswordLength.java”

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import javax.management.Attribute;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import weblogic.management.configuration.DomainMBean;
import weblogic.management.configuration.SecurityConfigurationMBean;
import weblogic.management.runtime.RealmRuntimeMBean;
import weblogic.management.security.RealmMBean;
import weblogic.management.security.authentication.AuthenticationProviderMBean;
import weblogic.management.security.authentication.UserLockoutManagerMBean;
import weblogic.management.security.authentication.UserPasswordEditorMBean;
import weblogic.security.UserLockoutManagerRuntime;
import weblogic.security.acl.internal.DefaultRealmImpl;
import weblogic.security.providers.authentication.DefaultAuthenticatorMBean;

public class ChangeMinimumPasswordLength {
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;

static
{
try {
service = new ObjectName("com.bea:Name=EditService,Type=weblogic.management.mbeanservers.edit.EditServiceMBean");
}
catch (MalformedObjectNameException e)
{
throw new AssertionError(e.getMessage());
}
}

public static void initConnection(String hostname, String portString, String username, String password) throws IOException,MalformedURLException
{
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.edit";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port,jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
}

public ObjectName startEditSession() throws Exception
{
ObjectName cfgMgr = (ObjectName) connection.getAttribute(service,"ConfigurationManager");
ObjectName domainConfigRoot = (ObjectName)
connection.invoke(cfgMgr,"startEdit", new Object[] { new Integer(60000).  new Integer(120000) }, new String[] { "java.lang.Integer","java.lang.Integer" });
if (domainConfigRoot == null)
{
throw new Exception("Somebody else is editing already");
}
return domainConfigRoot;
}

public void editUserLockoutminPassLength(ObjectName cfgRoot,Integer minPassLength) throws Exception
{
Attribute newminPassLength = new Attribute("MinimumPasswordLength", minPassLength);
connection.setAttribute(cfgRoot, newminPassLength);
System.out.println("Changed the MinimumPasswordLength to " +minPassLength);
}

public ObjectName activate() throws Exception
{
ObjectName cfgMgr = (ObjectName) connection.getAttribute(service,"ConfigurationManager");
ObjectName task = (ObjectName) connection.invoke(cfgMgr, "activate",new Object[] { new Long(120000) }, new String[] { "java.lang.Long" });
return task;
}

public static void main(String[] args) throws Exception
{
String hostname = args[0];
String portString = args[1];
String username = args[2];
String password = args[3];
ChangeMinimumPasswordLength ewb = new ChangeMinimumPasswordLength();
System.out.println("--------------1");
initConnection(hostname, portString, username, password);
System.out.println("--------------2");
ObjectName cfgMgr = (ObjectName) connection.getAttribute(service,"ConfigurationManager");
System.out.println("--------------3");
ObjectName cfgRoot = ewb.startEditSession();
System.out.println("--------------4");
Integer minPassLength=new Integer("3");
ewb.editUserLockoutminPassLength(new ObjectName("Security:Name=myrealmDefaultAuthenticator").  minPassLength);
System.out.println("--------------5");
connection.invoke(cfgMgr, "save", null, null);
System.out.println("--------------6");
ewb.activate();
connector.close();
}
}
Step3). Now open a command prompt and then run the “setWLSEnv.cmd” script to set the Environment like “CLASSPATH” and “PATH” in your command prtompt.
Step4). run the Above program like following:

Running the ChangeMinPassword_Jmx Code

Running the ChangeMinPassword_Jmx Code

Step5). Verify from the Server STDOUT that the changes gets reflected on Server or not….(NOTE: You will have to restart your Server sothat the changes can take effect)
Server_Stdout_AfterMinPassChange JMX

Server_Stdout_AfterMinPassChange JMX

.
.
Thanks
Jay SenSharma

All WebLogic Server HealthState Checking Using JMX

Hi,

Jay SenSharma

Jay SenSharma

Here we are going to see the demonstration of a Server Healt State Monitoring with the help of JMX. Many times we need to not only monitor the State of the Server …rather we want to know what is the Health Status of the Server. WebLogic APIs contain a class ” weblogic.health.HealthState” which provides some constants variables to represent the Actual HealthState of a Server.At anytime the WebLogic Server may be in any of the following state:

.
public static final int HEALTH_OK;
public static final int HEALTH_WARN;
public static final int HEALTH_CRITICAL;
public static final int HEALTH_FAILED;
public static final int HEALTH_OVERLOADED;

Step1). Create a Directory somewhere in your file system like: “C:ServerHealthCheckDemo”

Step2). Now write the following JMX Code to monitor the HealthState of all the Servers Which are Part of our WebLogic Domain.
“ServerHealthStateMonitor.java”

import weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean;
import weblogic.management.runtime.ServerRuntimeMBean;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import weblogic.management.jmx.MBeanServerInvocationHandler;
import java.util.Hashtable;
import java.io.IOException;
import java.net.MalformedURLException;
import weblogic.management.runtime.JDBCDataSourceRuntimeMBean;
import javax.management.*;
import javax.naming.*;

public class ServerHealthStateMonitor {
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;
private static String combea = "com.bea:Name=";
private static String service1 = "DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean";
private static String service2 = "RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean";

static {
try {
service =
new ObjectName(combea + service1);
} catch (MalformedObjectNameException e) {
throw new AssertionError(e.getMessage());
}
}

public static void initConnection (String hostname, String portString, String username, String password) throws IOException,MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.domainruntime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
}

public static ObjectName[] getServerRuntimes() throws Exception {
return (ObjectName[]) connection.getAttribute(service,"ServerRuntimes");
}

public void printNameAndState() throws Exception
{
ObjectName arr[]=getServerRuntimes();
for(ObjectName temp : arr)
System.out.println("nt servers: "+temp);
ObjectName domain = (ObjectName) connection.getAttribute(service,"DomainConfiguration");
System.out.println("Domain: " + domain.toString());
ObjectName[] servers = (ObjectName[]) connection.getAttribute(domain,"Servers");
for (ObjectName server : servers)
{
String aName = (String) connection.getAttribute(server,"Name");
try{
ObjectName ser= new ObjectName("com.bea:Name="+aName+",Location="+aName+",Type=ServerRuntime");
String serverState=(String) connection.getAttribute(ser,"State");
System.out.println("nt Server: "+aName+"t State: "+serverState);
weblogic.health.HealthState serverHealthState=( weblogic.health.HealthState) connection.getAttribute(ser,"HealthState");
int hState=serverHealthState.getState();
if(hState==weblogic.health.HealthState.HEALTH_OK)
System.out.println("t Server: "+aName+"t State Health: HEALTH_OK");
if(hState==weblogic.health.HealthState.HEALTH_WARN)
System.out.println("t Server: "+aName+"t State Health: HEALTH_WARN");
if(hState==weblogic.health.HealthState.HEALTH_CRITICAL)
System.out.println("t Server: "+aName+"t State Health: HEALTH_CRITICAL");
if(hState==weblogic.health.HealthState.HEALTH_FAILED)
System.out.println("t Server: "+aName+"t State Health: HEALTH_FAILED");
if(hState==weblogic.health.HealthState. HEALTH_OVERLOADED)
System.out.println("t Server: "+aName+"t State Health: HEALTH_OVERLOADED");
}
catch(javax.management.InstanceNotFoundException e)
{
System.out.println("nt Server: "+aName+"t State: SHUTDOWN (or Not Reachable)");
}
}
}

public static void main(String[] args) throws Exception {
String hostname = args[0];
String portString = args[1];
String username = args[2];
String password = args[3];
ServerHealthStateMonitor s = new ServerHealthStateMonitor();
initConnection(hostname, portString, username, password);
s.printNameAndState();
connector.close();
}
}

Step3). Now open a command prompt and then run the “setWLSEnv.cmd” to set the Environment variables like “CLASSPATH” and “PATH” then compile and run the program like following:

C:ServerHealthCheckDemo>javac ServerHealthStateMonitor.java
Note: ServerHealthStateMonitor.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:ServerHealthCheckDemo>java ServerHealthStateMonitor localhost 7001 weblogic weblogic

servers: com.bea:Name=AdminServer,Location=AdminServer,Type=ServerRuntime
Domain: com.bea:Name=7001_EJB_Domain,Location=7001_EJB_Domain,Type=Domain

Server: AdminServer State: RUNNING
Server: AdminServer State Health: HEALTH_OK

Server: MS1 State: SHUTDOWN (or Not Reachable)

Server: MS2 State: SHUTDOWN (or Not Reachable)

Server_HealthState_JMX

Server_HealthState_JMX

.
.
Thanks
Jay SenSharma


Copyright © 2010-2012 Middleware Magic. All rights reserved. |