Hi,

Jay SenSharma

Jay SenSharma

Based on the Query of Jmajumde…We tested the following …http://middlewaremagic.com/weblogic/?page_id=114&cpage=1#comment-219

Here we are going to see a Simple JMX code in which we will use the JMX NotificationListener we will add this Listener to the MBeanServer Connection, to get the Notification of STATE attribute change notification.

Ideally this same code should work fine for any attribute which changes for ServerRuntimeMBean like (State, ListenPort, SSLListenAddress , SSLListenPort…changes ..it should be applicable for all the attributes as mentioned here: http://download.oracle.com/docs/cd/E12840_01/wls/docs103/wlsmbeanref/core/index.html {ServerRuntimeMBean attributes})

But it looks like a Bug that the Notification is not being sent by WLS MBean Server …for all these attributes …But It is sending the Notification for Only “State” attribute change notification…for any server available in the Domain.

Step1). Create a Domain with AdminServer and a ManagedServer (MS1), You can choose any port. Creation of machine or Clusters are not required to test this JMX code.

Step2). start AdminServer and Managed Server.

Step3). Create a Directory somewhere in your filesystem “C:Basic_WLS” and then place your own Notification Listener implementation Class inside this directory with name “MyAttributeChangeListenerImpl.java” as following:

import javax.management.*;
import weblogic.management.*;
import java.util.Date;
public class MyAttributeChangeListenerImpl implements RemoteNotificationListener
{
public void handleNotification(javax.management.Notification notification, Object handback)
{
System.out.println("nnt handleNotification(Notification notification, Object handback) () Called");

if(notification instanceof AttributeChangeNotification)
{
AttributeChangeNotification acn = (AttributeChangeNotification) notification;
System.out.println("MBean attribute " + acn.getAttributeName() + " of type " +
acn.getAttributeType() +" changed from " + acn.getOldValue() + " to " + acn.getNewValue());
System.out.println("ttObserved Attribute: "+ acn.getAttributeName());
System.out.println("ttAttribute Type: "+acn.getAttributeType());
System.out.println("ttMessage: "+acn.getMessage());
System.out.println("ttTimeStamp: "+new Date(acn.getTimeStamp()));
System.out.println("ttOld Value: " + acn.getOldValue());
System.out.println("ttNew Value: " + acn.getNewValue());
}
}
}

Step4). Again create a Main program “ServerStateNotification.java” inside “C:Basic_WLS” directory in this rogram we will create an object of “MyAttributeChangeListenerImpl” and add it as a Notification Listener in the MBeanServer.

import java.util.Hashtable;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.MalformedObjectNameException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import javax.management.AttributeChangeNotificationFilter;

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

static {
try {
service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
}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 static void main(String[] args) throws Exception {
String hostname = args[0];
String portString = args[1];
String username = args[2];
String password = args[3];
try {
MyAttributeChangeListenerImpl listener = new MyAttributeChangeListenerImpl();
AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
//filter.enableAttribute("State");
initConnection(hostname, portString, username, password);
ObjectName[] serverLCRT = getServerRuntimes();
int length= (int) serverLCRT.length;
for (int i=0; i < length; i++) {
connection.addNotificationListener(serverLCRT[i], listener, null, null);
// With Filter----> connection.addNotificationListener(serverLCRT[i], listener, filter, null);
System.out.println("n[myListener]: Listener registered with"+serverLCRT[i]);
}
System.out.println("ntWaiting for Notifications (Press Anykey to Exit). ........");
System.in.read();
} catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}

Step5). Make sure that Admin & Managed Servers are Running …Now open a command prompt then execute “setWLSEnv.cmd” to set the Jars/java in the current prompt. Then compile and Run the above two programs. like following:

Step6). Now login to AdminConsole and then goto home –> Summary of Servers –>MS1 –>Control (Tab) and select your ManagedSevrer and click on “Suspend” Button …as soon as you click on the Suspend Button the Managed Server Moves from “RUNNING” State to the “ADMIN” State….

Our Notification listener captures this event and then you will be able to see the output of this in your Command Window.


Note: Not only for state Attribute Change ….rather for all the ServerRuntime Attribute changes we should have got notifications but ..this looks like a BUG in WLS10.3 as well as WLS10.3.2 …that it is able to notify only for the State Change…and not for other Attributes….

.
.
Thanks
Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.