Monday, March 31, 2008

Using JMX to Start/Stop Message Driven Bean

OC4J 10.1.3.x allows users to start/stop MDBs dynamically without having recycle container or application.

For example, you can disable an MDB upon start/deployment by adding enabled="false" in your orion-ejb-jar.xml as follows

<message-driven-deployment name="MessageLogger" enabled="false">

MDB will not start when the application is deployed/started.

You can use Application Server Control to start the MDB later as shown in the following screen shot:


You may want to perform this operation from command line. You can download this zip file that contains code to stop/start MDB using JMX.


  • Unpackage the zip and change ant-oracle.properties file to change your URL, userid and password
  • Set ORACLE_HOME to the directory where OC4J installed


  • To start your MDB:


ant start -DappName=ejb30mdb -DejbModule=ejb30mdb-ejb -DmdbName MessageLogger

  • To stop it


ant stop -DappName=ejb30mdb -DejbModule=ejb30mdb-ejb -DmdbName MessageLogger

Following is the code snippet from the Java Class that perform the stop/start operation:

StartStopMDB mdb = new StartStopMDB();

try {

mdb.connect("service:jmx:"+args[0], args[1] ,args[2] );

System.out.println(mdb);

System.out.printf("Client connected? %s\n",mdb.isConnected());

MBeanServerConnection mbs = jmxCon.getMBeanServerConnection();

String objName = "oc4j:j2eeType=MessageDrivenBean,EJBModule=\""+args[4]

+"\",J2EEApplication="+args[3]+",J2EEServer=standalone,name=\""+args[5]+"\"";

ObjectName myMDBObjectName = new ObjectName(objName);

MessageDrivenBeanMBeanProxy MDBMBean =

(MessageDrivenBeanMBeanProxy)MBeanServerInvocationHandler.newProxyInstance(mbs,

myMDBObjectName, MessageDrivenBeanMBeanProxy.class, false);

if (args[6].equals("start"))

{

System.out.println("Starting up MDB: "+args[5] +" in ejb module:"+args[4]+

" in application name:"+args[3]);

MDBMBean.start();

System.out.println("Successfully started MDB!");

}

else if (args[6].equals("stop")) {

System.out.println("Stopping MDB: "+args[5] +

" in ejb module:"+args[4]+ " in application name:"+args[3]);

MDBMBean.stop();

System.out.println("Successfully started MDB!");

}

mdb.close();

} catch (Exception e) {

e.printStackTrace();

}

}


Hope this helps!

6 comments:

Anonymous said...

The schema definition for the element "message-driven-deployment" does not have an attribute called "enabled". Here is the link to the schema: http://www.oracle.com/technology/oracleas/schema/orion-ejb-jar-10_0.xsd. What version of the OC4J do you use? Thanks.

Anonymous said...

Oracle has just updated the schema to include the "enabled" attribute for the message-driven-deployment element after I reported the missing of this attribute in the xsd file.

Ram Konchada said...

I am trying to add the same attribute in the annotation of my MDB and I could not get to compile with the JARs I am already using to build provided with 10.1.3.4 standalone version. Can you please suggest?

Anonymous said...

I wonder if anyone else was able to achieve the same through annotations...like Ram is trying to do ?

Polimetla said...

Oracle has updated the orion-ejb-jar-10_0.xsd, but not the annotation unfortunately.

Unknown said...

Hi,
do you Know if with web logic 12 exists the same possibility?
I should start the MDB in state disabled like in OC4J.
Thanks