Tuesday, May 20, 2014

Creating Mbeans(JMX) in ADF Application and accessing them from jrockit mission control



Mbeans(JMX) can be very useful to manage an application or any resource. ADF provides some inbuilt mbeans. These can be enabled by adding the below tags in web.xml
<listener>
   <listener-class>
        oracle.adf.mbean.share.connection.ADFConnectionLifeCycleCallBack
   </listener-class>
</listener>
<listener>
   <listener-class>
        oracle.adf.mbean.share.config.ADFConfigLifeCycleCallBack</listener-class>
</listener>
<listener>
   <listener-class>
        oracle.bc4j.mbean.BC4JConfigLifeCycleCallBack</listener-class>
</listener>


Some times you might want to add custom Mbeans to control your application. You also might want to access these using your jrockit mission control. Lets understand this by take an instance of changing log levels of certain classes when you use log4j for logging using mbeans(JMX).
 
1. First you need an interface of the methods you want to expose.
public interface Log4jConfigMBean {
    public String fetchLogLevel(String logger);
    public String changeLogLevel(String level, String logger);
}

 
2. You need a class that implements the previously created Log4jConfigMBean and javax.management.MBeanRegistration
package com.test.backing;
import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class Log4jConfig implements Log4jConfigMBean , MBeanRegistration {
    public Log4jConfig() {
        super();
    }

    public String fetchLogLevel(String name) {
        if (name==null ||"".equals(name) )
        {
          return "invalid usage";
        }
        else
        {
          return  "Log level of " +name +" is "+ (Logger.getLogger(name).getLevel()!= null ?Logger.getLogger(name).getLevel() :Logger.getRootLogger().getLevel());
        }
    }

    public String changeLogLevel(String level, String name) {
        if (name==null ||"".equals(name)|| level==null || "".equals(level) )
        {
          return "invalid usage";
        }
        else
        {
          Logger logger = Logger.getLogger(name);
          logger.setLevel(Level.toLevel(level));
          return  "Log level of "+ name + " set to " +Logger.getLogger(name).getLevel();
        }
    }

    public ObjectName preRegister(MBeanServer mBeanServer, ObjectName objectName) {
        return null;
    }

    public void postRegister(Boolean boolean) {
    }

    public void preDeregister() {
    }

    public void postDeregister() {
    }
}


3. We have to register these in someplace when the application starts. I chose initialize these using a ServletContextListener. You can use other methods too.

package com.test.backing;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;

import javax.naming.InitialContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class Log4jMBeanLifeCycleListener implements ServletContextListener {
    public Log4jMBeanLifeCycleListener() {
        super();
    }

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
            InitialContext ctx = new InitialContext();
            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
            Log4jConfig mbean = new Log4jConfig();
            ObjectName oname;
            oname = new ObjectName("Log4j:type=Log4jConfig,name=RuntimeLog4jConfig");
            mbs.registerMBean(mbean, oname);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
}


4. Add the ServletContextListener to the web.xml
<listener>
  <listener-class>com.test.backing.Log4jMBeanLifeCycleListener</listener-class>
</listener>

 
 
5. Testing the mbeans.

a. Make sure you have enabled JMX on the weblogic server. In Jdeveloper you can add them to the Run Configurations
-Dcom.sun.management.jmxremote -Dtangosol.coherence.management=all -Dtangosol.coherence.management.remote=true -Dcom.sun.management.jmxremote.port=7772
image
b. Start jrockit. You can now see the new Management Extension. You can change the log level in runtime.
image































Wednesday, March 12, 2014

Weblogic Bridge for IBM MQ Series(Running on AS400 / iseries / Mainframe) Part 1


Bridges help in improving reliability of messages that need to be created on a remote Messaging Servers. If we have a program in weblogic writing to queues in MQ series, we write the messages into a JMS queue created in weblogic. Then, we create a bridge that takes care of sending the message from the local weblogic queue to MQ series. By doing this, We are immune to remote MQ going down. The bridge takes care of reconnecting and sending messages to the remote MQ.

In this part we will look at the pre requisites to setup a Weblogic Bridge for IBM MQ. We need a .bindings file which described JNDI lookups for weblogic to connect to the MQSeries (Websphere MQ)

1. Install MQ Series MQ Explorer on Windows.

2. Set up connection to Remote Queue Manager

a. Open MQ explorer and right click on queue managers and select “Add Remote Queue Manager”

image

b. give a queue manager name, select “Connect Directly” and click next



image

c. Give HostName, port number and Server Connection Channel. (The MQ administrator can give you these details) and click next

d. click next

e. give the user name and password if MQ is secure

f. click finish (Sometimes you might have to put in other information. Contact your MQ Administrator)
you should be able to see the Queue manager and expand queues and see the queues on the remote server.


3. Create an Initial Context

a. right click on JMS administered Objects and select “Add Initial Context”


image

b. Select File System, and give a valid Bindings Directory. Click Next.

image

c.Click Finish

image


4. Create Connection Factory

a. Expand the Initial context you just created,
right click on Connection Factory –> New –> Connection Factory.

image

b. Give a connection factory name( We use this name to configure connection factory in weblogic)

image

c. Select Queue Connection Factory and check supports XA transactions

image

d. Select MQ Client in transport and click Next. click next

image

e. select the appropriate version of the server

image

f. Select connection on left.
Select the queue manager
Give the appropriate url and port in connection list
click Finish.
image


5. Create a Destination


a. Right click on Destinations
Select new –> Destinations

image

b. Give a Destination Name(You will use the same name in Weblogic Destination Name)
Select Type and click Next.

image

c. Click Next.

d. Select the Queue Manager and the queue and click Finish.


image 


(Will continue tomorrow)