Last active
August 29, 2015 14:01
-
-
Save mikecroft/ae2204dc1e2279656012 to your computer and use it in GitHub Desktop.
Class taken from public Oracle blog on WLDF: https://blogs.oracle.com/WebLogicServer/entry/are_my_servers_running [requires weblogic.jar]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package jmxwatchnotificationlistener; | |
import javax.management.NotificationListener; | |
import javax.management.MBeanServerConnection; | |
import javax.management.ObjectName; | |
import javax.management.Notification; | |
import javax.management.remote.JMXServiceURL; | |
import javax.management.remote.JMXConnectorFactory; | |
import javax.management.remote.JMXConnector; | |
import javax.naming.Context; | |
import java.util.Hashtable; | |
import weblogic.management.mbeanservers.runtime.RuntimeServiceMBean; | |
import weblogic.diagnostics.watch.WatchNotification; | |
import weblogic.diagnostics.watch.JMXWatchNotification; | |
public class JMXWatchNotificationListener implements NotificationListener { | |
private MBeanServerConnection rmbs = null; | |
private String notifName = "myjmx"; | |
private int notifCount = 0; | |
private String serverName = "AdminServer"; | |
private String hostName = "localhost"; | |
private int port = 7001; | |
private String user = "weblogic"; | |
private String password = "weblogic"; | |
public JMXWatchNotificationListener(String args[]) throws Exception { | |
int size = (args != null ? args.length : 0); | |
for (int i=0; i < size; i++) { | |
String arg = args[i]; | |
if (arg.equals("-server")) { | |
serverName = args[++i]; | |
} else if (arg.equals("-host")) { | |
hostName = args[++i]; | |
} else if (arg.equals("-port")) { | |
port = Integer.parseInt(args[++i]); | |
} else if (arg.equals("-user")) { | |
user = args[++i]; | |
} else if (arg.equals("-password")) { | |
password = args[++i]; | |
} | |
} | |
} | |
private void register() throws Exception { | |
rmbs = getRuntimeMBeanServerConnection(); | |
addNotificationHandler(); | |
} | |
private MBeanServerConnection getRuntimeMBeanServerConnection() | |
throws Exception { | |
String JNDI = "/jndi/"; | |
JMXServiceURL serviceURL; | |
serviceURL = | |
new JMXServiceURL("t3", hostName, port, | |
JNDI + RuntimeServiceMBean.MBEANSERVER_JNDI_NAME); | |
System.out.println("URL=" + serviceURL); | |
Hashtable h = new Hashtable(); | |
h.put(Context.SECURITY_PRINCIPAL,user); | |
h.put(Context.SECURITY_CREDENTIALS,password); | |
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, | |
"weblogic.management.remote"); | |
JMXConnector connector = JMXConnectorFactory.connect(serviceURL,h); | |
return connector.getMBeanServerConnection(); | |
} | |
private void addNotificationHandler() throws Exception { | |
/* | |
* The JMX Watch notification listener registers with a Runtime MBean | |
* that matches the name of the corresponding watch bean. | |
* Each watch has its own Runtime MBean instance. | |
*/ | |
ObjectName oname = | |
new ObjectName( | |
"com.bea:ServerRuntime=" + serverName + ",Name=" + | |
JMXWatchNotification.GLOBAL_JMX_NOTIFICATION_PRODUCER_NAME + | |
",Type=WLDFWatchJMXNotificationRuntime," + | |
"WLDFWatchNotificationRuntime=WatchNotification," + | |
"WLDFRuntime=WLDFRuntime" | |
); | |
System.out.println("Adding notification handler for: " + | |
oname.getCanonicalName()); | |
rmbs.addNotificationListener(oname, this, null, null); | |
} | |
/** | |
* Handle JMX notification | |
*/ | |
public void handleNotification(Notification notif, Object handback) { | |
synchronized (this) { | |
try { | |
if (notif instanceof JMXWatchNotification) { | |
WatchNotification wNotif = | |
((JMXWatchNotification)notif).getExtendedInfo(); | |
notifCount++; | |
System.out.println("==============================================="); | |
System.out.println("Notification name: " + | |
notifName + " called. Count= " + notifCount + "."); | |
System.out.println("Watch severity: " + | |
wNotif.getWatchSeverityLevel()); | |
System.out.println("Watch time: " + | |
wNotif.getWatchTime()); | |
System.out.println("Watch ServerName: " + | |
wNotif.getWatchServerName()); | |
System.out.println("Watch RuleType: " + | |
wNotif.getWatchRuleType()); | |
System.out.println("Watch Rule: " + | |
wNotif.getWatchRule()); | |
System.out.println("Watch Name: " + | |
wNotif.getWatchName()); | |
System.out.println("Watch DomainName: " + | |
wNotif.getWatchDomainName()); | |
System.out.println("Watch AlarmType: " + | |
wNotif.getWatchAlarmType()); | |
System.out.println("Watch AlarmResetPeriod: " + | |
wNotif.getWatchAlarmResetPeriod()); | |
System.out.println("Watch Message: " + | |
wNotif.getMessage()); | |
System.out.println("Watch Data: " + | |
wNotif.getWatchDataToString()); | |
System.out.println("==============================================="); | |
} | |
} catch (Throwable x) { | |
System.out.println("Exception occurred processing JMX watch notification: " + notifName +"\n" + x); | |
x.printStackTrace(); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
try { | |
JMXWatchNotificationListener listener = | |
new JMXWatchNotificationListener(args); | |
listener.register(); | |
// Sleep waiting for notifications | |
Thread.sleep(Long.MAX_VALUE); | |
} catch (Throwable e) { | |
e.printStackTrace(); | |
} // end of try-catch | |
} // end of main() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment