How can you monitor your own Java applications in Enterprise Manager?
In most cases you would use the built in functionality around performance, incidents etc. but what if you need to get some diagnostic data or want to track changes or behaviour of your application?
A standard way to control and monitor Java applications is using JMX and MBeans. In this example I'll first go through creating a Java EE Application with a MBean interface, then register and monitor the parameter from Oracle Enterprise Manager.
In my little example below I have used the standard WebLogic documentation to create my MBean application:
http://docs.oracle.com/cd/E14571_01/web.1111/e13729/instmbeans.htm
You can find several MBean example applications around on the internet. You may choose one of those or follow my simple recipe below.
For this programming part I used Oracle Enterprise Pack for Eclipse (OEPE) that can be downloaded from the Oracle Technology Network site:
http://www.oracle.com/technetwork/developer-tools/eclipse/downloads/index.html
Start out by creating a new Java project (just use default settings).
Then a new java packet under the src directory. Mine is called com.oracle.demo.
We start with creating the MBean Interface:
- package com.oracle.demo;
-
- public interface MyMBean {
- public String getMyName();
- public void setMyName(String name);
- }
Next the MyBeanImpl class that implements the MBean interface
- package com.oracle.demo;
-
- import javax.management.StandardMBean;
-
- public class MyMBeanImpl extends StandardMBean
-
- implements MyMBean {
-
- String myName = "Duke";
-
- public MyMBeanImpl() throws
- javax.management.NotCompliantMBeanException {
- super(MyMBean.class);
- }
-
- @Override
- public String getMyName() {
- return myName;
- }
-
- @Override
- public void setMyName(String name) {
- this.myName = name;
- }
- }
After that we need to create the registration class that will register our MyBeanImpl class in WebLogic.
- package com.oracle.demo;
-
- import javax.management.MBeanServer;
- import javax.management.ObjectName;
-
- import javax.naming.InitialContext;
-
- import javax.naming.NamingException;
-
- import weblogic.application.ApplicationLifecycleListener;
-
- public class RegisterMyMBean extends ApplicationLifecycleListener {
-
-
- // register the mbean when application starts.
- public void postStart(weblogic.application.ApplicationLifecycleEvent p1) {
- try {
- ObjectName mymbeanobj =
- new ObjectName("mymbean:Name=MyMBean,Type=MyMBean");
- InitialContext ctx = new InitialContext();
-
- MBeanServer server = (MBeanServer)ctx.lookup("java:comp/jmx/runtime");
-
- MyMBean mbean = new MyMBeanImpl();
-
- server.registerMBean(mbean, mymbeanobj);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
- // unregister the mbean when the application stops.
- public void preStop(weblogic.application.ApplicationLifecycleEvent p1) {
- InitialContext ctx;
- try {
- ObjectName mymbeanobj =
- new ObjectName("mymbean:Name=MyMBean,Type=MyMBean");
-
- ctx = new InitialContext();
-
- MBeanServer server = (MBeanServer)ctx.lookup("java:comp/jmx/runtime");
- server.unregisterMBean(mymbeanobj);
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Once done, you should have a similar directory structure:
Don't mind any compiler warnings or IDE warnings at this time. They will disappear with the next step, which is to create the .ear file.
Create a new Enterprise Application Project, New->Other->Java EE->Enterprise Application Project
Give the application a name and enter the run time information needed by Eclipse to deploy your application:
Click Next, and then add you Java project to the Java EE Application
Afterwards your project window should look something like:
We are almost there. But you need to add a couple of more things. First of all, lets change the weblogic-application.xml file to actually register the MBean at startup.
<wls:weblogic-application xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-application" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/javaee_5.xsd http://xmlns.oracle.com/weblogic/weblogic-application http://xmlns.oracle.com/weblogic/weblogic-application/1.3/weblogic-application.xsd">
<!--weblogic-version:10.3.6-->
<wls:application-param>
<wls:param-name>webapp.encoding.default<wls:param-name>
<wls:param-value>UTF-8</wls:param-value>
</wls:application-param>
<wls:listener>
<wls:listener-class>com.oracle.demo.RegisterMyMBean</wls:listener-class>
</wls:listener>
</wls:weblogic-application>
-
The final little piece to the demo application is a web project. Normally this is where you add your code or application. Add a new Dynamic Web Project.
call the project hello, and remember to add the project to our EAR.
Then press Finish.
Now we have three projects in the explorer. The last thing to do is to add a JSP and add our web application to the application.xml file.
Right click on hello->WebContent and select JSP File. Name the file hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello all!
</body>
</html>
Now we need to add the hello web application to the EAR application.xml deployment descriptor. If you did not "hook" the generate application.xml file during the EAR creation, you should just add the file under MyCustomMBeanApp->EarContent->META-INF
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:application="http://java.sun.com/xml/ns/javaee/application_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" id="Application_ID" version="5">
<display-name>MBeanProject</display-name>
<module>
<web>
<web-uri>hello.war</web-uri>
<context-root>hello</context-root>
</web>
</module>
</application>
The project is now ready to deploy to a WebLogic server. Deploy directly from Eclipse or go through an .ear file.
Deploy the application to a WebLogic server managed by Enterprise Manager and make sure that you are able to see the hello world page.
Now log into Enterprise Manager and refresh the domain.