Post on 13-Dec-2015
Java Management ExtensionsJava Management Extensions
Borcon 2004Ken Sipe
kensipe@codementor.net
Borcon 2004Ken Sipe
kensipe@codementor.net
2
All StuffAll StuffNo Fluff No Fluff ™
Overall Presentation Goal Overall Presentation Goal
Defined the problem space and need that JMX solves.Provided details on the architecture and understanding of the technologyProvide resources need to expound on this learning.
3
All StuffAll StuffNo Fluff No Fluff ™
Speaker’s QualificationsSpeaker’s Qualifications
Sun Certified Java 2 Architect. Instructor for VisiBroker for Java, OOAD, Rational Rose, and Java
Development. Frequently speaks on the subject of distributed computing programming,
including CORBA and EJB architecture. JBoss Certified Developer ;)
4
All StuffAll StuffNo Fluff No Fluff ™
Presentation Agenda Presentation Agenda
JMX Introduction JMX Architecture
Instrumentation Level Agent Level Distributed Services Level JMX Object Naming
Leveraging JMX
5
All StuffAll StuffNo Fluff No Fluff ™
What is JMX?What is JMX?
Java Management Extensions Provides runtime application configuration and
management. Update services on the fly No downtime
Provides monitoring services with notification services. Standardized toolsets
Required for future J2EE Services
6
All StuffAll StuffNo Fluff No Fluff ™
JMX HistoryJMX History
JSR – 03 Early need recognition
JMX 1.0 – Sept 2000 JMX 1.2 – Dec 2002
7
All StuffAll StuffNo Fluff No Fluff ™
Implementations of the SpecImplementations of the Spec
Sun http://java.sun.com/products/JavaManagement/index.jsp
IBM http://alphaworks.ibm.com/tech/TMX4J Not really available separately any more, part of WAS 5.x
AdventNet http://adventnet.com/
JBoss http://jboss.org
mx4j http://mx4j.sourceforge.net/
8
All StuffAll StuffNo Fluff No Fluff ™
Where’s JMX currently usedWhere’s JMX currently used
WAS 5.0 Tomcat 5.0
mx4j JBoss 2.4.x, 3.x, and 4.0 beta
jbossmx Weblogic 7.x Required
J2EE 1.4 J2SE 1.5
9
All StuffAll StuffNo Fluff No Fluff ™
Related SpecificationsRelated Specifications
JSR-77 J2EE Management API JSR-88 J2EE Deployment API JSR-160 JMX Remote API JSR-174 Monitoring and Management for the JVM machine
10
All StuffAll StuffNo Fluff No Fluff ™
J2EE Management API JSR-77J2EE Management API JSR-77
J2EE 1.4 Required Provides API for Managed Objects
J2EEDomain JVM SessionBean
State Management Performance Monitoring SNMP
Provides a common tool set to provide monitoring tools for Provides a common tool set to provide monitoring tools for any J2EE server.any J2EE server.
11
All StuffAll StuffNo Fluff No Fluff ™
Java Management DiagramJava Management Diagram
12
All StuffAll StuffNo Fluff No Fluff ™
J2EE Deployment API JSR-88J2EE Deployment API JSR-88
J2EE 1.4 Required Leverages JSR-77 for Targeting deployment
13
All StuffAll StuffNo Fluff No Fluff ™
JMX Remote API JSR -160JMX Remote API JSR -160
JMX specification since 1.0 defined distributed services as a level of the architecture. However even specification 1.2 merely mentions it and do not define it.
Defines the an interoperable, transparent, secure and flexible solution for clients to connect to JMX servers.
Defines: Connectors Transport Lookup Services Bindings
14
All StuffAll StuffNo Fluff No Fluff ™
New to JMX 1.2New to JMX 1.2
Open Mbeans manitory New interface StandardMBean interface Class loader repository redesigned and class loading behavior clarified. New Security Chapter Updates to:
Timer service Monitor service ObjectName class
15
All StuffAll StuffNo Fluff No Fluff ™
Presentation Agenda Presentation Agenda
JMX Introduction JMX Architecture
Instrumentation Level Agent Level Distributed Services Level JMX Object Naming
Leveraging JMX
16
All StuffAll StuffNo Fluff No Fluff ™
JMX Architecture DiagramJMX Architecture Diagram
17
All StuffAll StuffNo Fluff No Fluff ™
JMX ArchitectureJMX Architecture
Instrumentation Level Defines the implementation of manageable resources commonly referred to as
MBeans or Managed Beans. There are several defined types of MBeans which will be outlined in the Instrumentation section. This level essentially defines interfaces and APIs for the developer to adhere to during the implement of an Mbean.
Agent Level Defines the agents and services which will provide access to the managed
resources. This level is implemented and provided to the developer providing services and the MBeanServer.
Distributed Level This level is defined in the specification as being needed and provides the details of
HTML accessible, or SNMP capable. The specification doesn’t provide a clear direction here, stating that it isn’t in scope for this phase of the specification. Tribal knowledge defines this level as Connectors and Adaptors to the services. Additionally there is JSR-160 which defines JMX remote API, which purpose is to provide JMX agent discovery and access to clients.
18
All StuffAll StuffNo Fluff No Fluff ™
Instrumentation LevelInstrumentation Level
MBeans Must provide the following characteristics
Public and concreteOne public constructor
Does not have to be a no argument constructor Categories
Standard MBeanDynamic MBean
19
All StuffAll StuffNo Fluff No Fluff ™
Standard MBeanStandard MBean
Most Simple Predefined set of attributes and operations which are fixed in an Interface Two Components
The interfaceThis interface defines the publicly exposed APIIt has a specific naming convention*
The class implementationClass must implement the defined interface
20
All StuffAll StuffNo Fluff No Fluff ™
Standard MBean Interface ExampleStandard MBean Interface Example
public interface VMMonitorMBean{ public String getThreadCount();
public String getFreeMemory();
public String getTotalMemory();
}
21
All StuffAll StuffNo Fluff No Fluff ™
Standard MBean Class ExampleStandard MBean Class Example
public class VMMonitor implements VMMonitorMBean {
public String getThreadCount() { ThreadGroup root = Thread.currentThread().getThreadGroup(); while (root.getParent() != null) root = root.getParent(); return root.activeCount() + " active threads"; }
public String getFreeMemory() { return Runtime.getRuntime().freeMemory() + " bytes"; }
public String getTotalMemory() { return Runtime.getRuntime().totalMemory() + " bytes"; }}
22
All StuffAll StuffNo Fluff No Fluff ™
JMX 1.2 Standard MBeanJMX 1.2 Standard MBean
The JMX 1.2 Specification addresses the issue of forcing the MBean notation on an interface name. It allows for the creation of a standard MBean with an interface of an arbitrary name. The relationship of the interface and the implementation is managed by the creation of a StandardMBean object.
The new StandardMBean class removes the necessary to follow the previously required naming convention of the MBean interface. Previously the association of the MBean interface to the MBean implementation was by naming convention. The StandardMBean constructor provides the associate in JMX 1.2. This allows the developer to name new interfaces any name desired or to over existing interfaces without change. In our StandardMBean example, the interface VMMonitorMBean could be named VMMonitor. In this case the following would create the StandardMBean which could then be registered with the MBeanServer:
Mbean = new StandardMBean(new VMMonitorImpl(), VMMonitor.class);
23
All StuffAll StuffNo Fluff No Fluff ™
Dynamic MBeanDynamic MBean
Does NOT define fixed list of attributes and operations. Attributes and operations are dynamic at runtime.
Must implement the DynamicMBean interface Types
Dynamic MBeanAny MBean which is dynamic
Model MBeanExtends ModelMBean interface, which extends the DynamicMBean interface
Open MBeanDynamic MBean which follows specific rules
24
All StuffAll StuffNo Fluff No Fluff ™
DynamicMBean InterfaceDynamicMBean Interface
public interface DynamicMBean {public MBeanInfo getMBeanInfo();public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException;
public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException;
public AttributeList getAttributes(String[] attributes);
public AttributeList setAttributes(AttributeList attributes);
public Object invoke(String method, Object[] arguments, String[] params)
throws MBeanException, ReflectionException;}
25
All StuffAll StuffNo Fluff No Fluff ™
Dynamic MBean CharacteristicsDynamic MBean Characteristics
Works much like a COM object. Query the interface with the getMBeanInfo() Invoke the method with the invoke() method
26
All StuffAll StuffNo Fluff No Fluff ™
MBeanInfo ClassMBeanInfo Class
public class MBeanInfo implements Cloneable, Serializable
{public String getClassName()public String getDescription() public MBeanConstructorInfo[] getConstructors() public MBeanAttributeInfo[] getAttributes()public MBeanOperationInfo[] getOperations() public MBeanNotificationInfo[] getNotifications()
}
27
All StuffAll StuffNo Fluff No Fluff ™
Dynamic MBean Pros / ConsDynamic MBean Pros / Cons
Pro: Programmatic access to the meta data MBean is not statically bound to Java class.
The bean could manage a resource which didn’t exist at the time of development.
Faster execution. Cons:
Much more to develop.
28
All StuffAll StuffNo Fluff No Fluff ™
Model MBeansModel MBeans
Mbean which extends the ModelMBean interface.public abstract interface ModelMBean extends DynamicMBean, PersistentMBean,
ModelMBeanNotificationBroadcaster {
void setModelMBeanInfo(ModelMBeanInfo modelMBeanInfo) throws MBeanException, RuntimeOperationsException;
void setManagedResource(Object object, String string) throws MBeanException, RuntimeOperationsException, InstanceNotFoundException, InvalidTargetObjectTypeException;
} PersistentMBean
Interfaces adds the signatures necessary to persist the state of the Mbean ModelMBeanNotificationBroadcaster
Interface provides the methods necessary to broadcast notifications
29
All StuffAll StuffNo Fluff No Fluff ™
Model Mbean CharacteristicsModel Mbean Characteristics
Provides a generic template for managing resources. Provides extensions to the management interfaces, such as attributes,
operations, constructors, and notifications. Defines behavioral properties such as security, transactions, persistence,
and caching.
30
All StuffAll StuffNo Fluff No Fluff ™
Open MBeanOpen MBean
The open in Open MBeans carries the same connotation as open standards. It refers to providing MBeans which follow standards in such a way that there is a high
degree of interoperability. Uses OpenMBeanInfo vs. MBeanInfo class Implements the DynamicMBean interface All types are constrained to:
Java data types Java Wrapper types (java.lang.Double)
java.lang.String java.lang.Void
java.util.Date java.math.BigIntegerjava.math.BigDecimal javax.management.ObjectNameJava.management.openmbean.CompositeData
java.management.openmbean. TabularData (interface)
31
All StuffAll StuffNo Fluff No Fluff ™
XMBeans (Not part of Spec)XMBeans (Not part of Spec)
Term first introduced by JBoss Development team. Defines a Dynamic Mbean which is configured and specified in XML.
All the meta-data is described in XML Other follow:
IBM has a XML specification for Mbeans deployed in WAS Common Modeler
Used by Tomcat 5http://jakarta.apache.org/commons/modeler/
32
All StuffAll StuffNo Fluff No Fluff ™
Presentation Agenda Presentation Agenda
JMX Introduction JMX Architecture
Instrumentation Level Agent Level Distributed Services Level JMX Object Naming
Leveraging JMX
33
All StuffAll StuffNo Fluff No Fluff ™
Agent LevelAgent Level
MBean Server MBean Registration Component Communication Channel
Pre-defined Service / MBeans M-Let Service
Dynamic loading service Timer Service
Notification service Monitoring Service
Counter Monitor, Gauge Monitor, String Monitor Relation Service
Maintains relationship informationNotifies on relationship changes
34
All StuffAll StuffNo Fluff No Fluff ™
MBeanServerMBeanServer
Creating an MBeanServer MBeanServer server = MBeanServerFactory.createMBeanServer(); MBeanServer server = MBeanServerFactory.createMBeanServer(“server1”);
Finding an MBeanServer ArrayList servers = MBeanServerFactory.findMBeanServer(null); MBeanServer server = (MBeanServer)servers.get(0);
35
All StuffAll StuffNo Fluff No Fluff ™
Registering an MBeanRegistering an MBean
MBeanServer server = MBeanServerFactory.createMBeanServer();ObjectName name = new ObjectName("Server:name=Monitor");
server.registerMBean(new VMMonitor(), name);
36
All StuffAll StuffNo Fluff No Fluff ™
M-Let ServicesM-Let Services
MLet Tag<MLET CODE = class ARCHIVE=“jar list”> constructor arglist </MLET>
Example<MLET CODE=“net.codementor.VMMonitor” ARCHIVE=“VM.jar”> </MLET>
37
All StuffAll StuffNo Fluff No Fluff ™
M-Let Dynamic LoadingM-Let Dynamic Loading
name = new ObjectName(“service:name=MLET"); server.registerMBean(new MLet(), name); server.invoke(name, "getMBeansFromURL", new Object[] {new URL("http://codementor.net/config.mlet")}, new String[]{URL.class.getName()} );
38
All StuffAll StuffNo Fluff No Fluff ™
Timer ServiceTimer Service
Registration of 2 Mbeans with MBeanServer Timer - Broadcaster Reciever
Start the timer Invoke the addNotification method on the Timer addNotificationListener with a Filter
39
All StuffAll StuffNo Fluff No Fluff ™
Timer RegistrationTimer Registration
ObjectName timer = new ObjectName("service:name=timer");ObjectName receiver = new ObjectName("domain:name=timerlistener");
// Timer: import javax.management.timer.Timer;server.registerMBean(new Timer(), timer);
// TimerReciever: is user defined, implements// javax.management.NotificationListener;server.registerMBean(new TimerReceiver(),receiver);
server.invoke(timer, "start", null, null);
40
All StuffAll StuffNo Fluff No Fluff ™
AddNotificationAddNotification
Date date = new Date(System.currentTimeMillis() + Timer.ONE_SECOND * 5); Integer id = (Integer)server.invoke(timer,"addNotification", new Object[] { "timer.event", // type "Notification Message", // message null, // user specific data date // event time }, new String[]{ String.class.getName(), String.class.getName(), Object.class.getName(), Date.class.getName(), });
server.addNotificationListener(timer, receiver, new ExampleFilter(id), null);
41
All StuffAll StuffNo Fluff No Fluff ™
Notification FilterNotification Filter
public class ExampleFilter implements NotificationFilter { Integer id; public ExampleFilter(Integer id) { this.id = id; }
public boolean isNotificationEnabled(Notification notification) { // check / filter the notification message if(notification.getType().equals("timer.event")) { // we know it is of type TimerNotification TimerNotification timerNote = (TimerNotification) notification; if(timerNote.getNotificationID().equals(id)) // it's a timerNotification and it's this specific timer notification return true; } return false; } }
42
All StuffAll StuffNo Fluff No Fluff ™
Presentation Agenda Presentation Agenda
JMX Introduction JMX Architecture
Instrumentation Level Agent Level Distributed Services Level JMX Object Naming
Leveraging JMX
43
All StuffAll StuffNo Fluff No Fluff ™
Distribution LevelDistribution Level
Very loose in the specification Tribal definition: Connectors and Adaptors
HtmlAdaptorServer HttpAdaptor RMIAdaptor
JSR-160 Remote Access Specification allows any transport RMI ** IIOP ** JMXMP * HTTP SOAP JMS
44
All StuffAll StuffNo Fluff No Fluff ™
HtmlAdaptor ExampleHtmlAdaptor Example
HtmlAdaptorServer adaptor = new HtmlAdaptorServer();adaptor.setPort(8082);
ObjectName adName = new ObjectName("Server:name=HttpAdaptor");server.registerMBean(adaptor, adName);
// start he adaptor service, which blocks the main threadadaptor.start();
45
All StuffAll StuffNo Fluff No Fluff ™
Html ConsoleHtml Console
46
All StuffAll StuffNo Fluff No Fluff ™
HttpAdaptor ConsoleHttpAdaptor Console
47
All StuffAll StuffNo Fluff No Fluff ™
Remote Access JSR-160Remote Access JSR-160
MBeanServerConnection Provides remote access All methods through IOException Must explicitly close connections
48
All StuffAll StuffNo Fluff No Fluff ™
Remote / Local Access ComparisonRemote / Local Access Comparison
Local access:MBeanServer server =
MBeanServerFactory.createMBeanServer();server.createMBean(className, obName);Object a = server.getAttribute(obName, “Attr”);Set names = server.queryNames(...);
Remote access:JMXConnector c = JMXConnectorFactory.connect(url);MBeanServerConnection server =
c.getMBeanServerConnection();server.createMBean(className, obName);Object a = server.getAttribute(obName, “Attr”);Set names = server.queryNames(...);c.close();
49
All StuffAll StuffNo Fluff No Fluff ™
Remote ListenersRemote Listeners
Code is the same as local Connector handles forwarding notifications to remote clients.
class MyListener
implements NotificationListener {...}
NotificationListener l = new MyListener();
server.addNotificationListener(obName, l);
50
All StuffAll StuffNo Fluff No Fluff ™
JMX Remote URLJMX Remote URL
Starts with “service:jmx:” service:jmx:rmi://host/… service:jmx:jmxmp://host:port
51
All StuffAll StuffNo Fluff No Fluff ™
Presentation Agenda Presentation Agenda
JMX Introduction JMX Architecture
Instrumentation Level Agent Level Distributed Services Level JMX Object Naming
Leveraging JMX
52
All StuffAll StuffNo Fluff No Fluff ™
ObjectNameObjectName
Defines a unique name for an Mbean Used to distinguish a bean for invocation Used to query a server for a specific bean Used to manage notification and relationship.
53
All StuffAll StuffNo Fluff No Fluff ™
Object Naming ConventionObject Naming Convention
[Domain]:property=value[,otherproperty=othervalue]*
Special Characters can not be used in the name Colon (:) Comma(;) Equals(=) Asterisks (*)
Example Names Server:name=Monitor WebSphere:name=server1,process=server1,platform=common,node=fezzik,version
=5.0,type=Server,mbeanIdentifier=cells/fezzik/nodes/fezzik/servers/server1/server.xml#Server_1,cell=fezzik,processType=UnManagedProcess
54
All StuffAll StuffNo Fluff No Fluff ™
Querying Object NamesQuerying Object Names
*:*,type=Server,* All Mbeans of type Server
*:*,cell=fezzik All Mbeans on the cell named fezzik
55
All StuffAll StuffNo Fluff No Fluff ™
Presentation Agenda Presentation Agenda
JMX Introduction JMX Architecture
Instrumentation Level Agent Level Distributed Services Level JMX Object Naming
Leveraging JMX Code and Demos
56
All StuffAll StuffNo Fluff No Fluff ™
VMMonitor ExampleVMMonitor Example
public class VMMonitorTester {public static void main(String[] args) throws Exception {
MBeanServer server = MBeanServerFactory.createMBeanServer(); // register the new service ObjectName name = new ObjectName("VM"); server.registerMBean(new VMMonitor(), name); // register the http adaptor
// HttpAdaptor adaptor = new HttpAdaptor(); HtmlAdaptorServer adaptor = new HtmlAdaptorServer(); adaptor.setPort(8082);
ObjectName adName = new ObjectName("Server:name=HttpAdaptor"); server.registerMBean(adaptor, adName);
adaptor.start();}
57
All StuffAll StuffNo Fluff No Fluff ™
Working with WAS 5Working with WAS 5
Starting ServerStarting Server
Starting wsadminStarting wsadmin
Query a MBean Query a MBean object name.object name.Get all attributes for Get all attributes for the MBean.the MBean.
Demos / CodeDemos / Code
VMMonitorTimer Example
WAS 5
VMMonitorTimer Example
WAS 5
59
All StuffAll StuffNo Fluff No Fluff ™
Need to know ToolsNeed to know Tools
Mx4j http://mx4j.sourceforge.net/
EJTools http://www.ejtools.org/ https://sourceforge.net/projects/ejtools/
Jakarta Commons Modeler http://jakarta.apache.org/commons/modeler/
MBeanInspector - (WebSphere) http://www.alphaworks.ibm.com/tech/mbeaninspector
Sun http://java.sun.com/jmx
60
All StuffAll StuffNo Fluff No Fluff ™
ConclusionConclusion
JMX is matureJMX is here to stayJMX defines the standard for Management and Monitoring
Questions?Questions?
Thank YouThank You
Please fill out the speaker evaluation
You can contact me further at ... kensipe@codementor.net
Please fill out the speaker evaluation
You can contact me further at ... kensipe@codementor.net