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!

Wednesday, March 26, 2008

Using TopLink Essentials as the JPA Provider for WebLogic 10

A couple of weeks ago, one customer asked me for instructions for using TopLink Essentials as the JPA provider with WebLogic 10. I thought I would give it a try as I’m working on migrating my book examples to WebLogic 10.

First, you have to download TopLink Essentials either from OTN or Glassfish web site.
I packaged the toplink-essentals.jar, toplink-essentials-agent.jar in the lib directory for the EAR so the structure of EAR looked like as follows:

chapter2-ejb.jar
lib/toplink-essentials.jar
lib/toplink-essentials-agent.jar

I specified provider class oracle.toplink.essentials.PersistenceProvider
in my persistence.xml packaged in my EJB module as follows:

<persistence-unit name="actionBazaar">
<provider>
class oracle.toplink.essentials.PersistenceProvider
</provider>
<properties>
<property name="toplink.ddl-generation" value="create-tables"> <property name="toplink.ddl-generation.output-mode" value="database">

If you want to use container managed entity manager with WebLogic’s JTA transaction manager you have to create your server platform class by extending oracle.toplink.essentials.transaction.JTATransactionController as follows:

package debu.weblogic;
import javax.transaction.TransactionManager;
import oracle.toplink.essentials.transaction.JTATransactionController;

public class WebLogicTransactionController extends JTATransactionController
{

private static final String WLS_TRANSACTION_MANAGER_NAME = "javax.transaction.TransactionManager";
public WebLogicTransactionController(){

}

protected TransactionManager acquireTransactionManager() throws Exception {
TransactionManager tm = (TransactionManager)jndiLookup(WLS_TRANSACTION_MANAGER_NAME);
return tm;
}
}

As you might have seen already that I’ve specified this class as the target-server property in persistence class.

You can package this class as a part of your application in the same EAR file where you packaged the TopLink Essentials jar files and the entity classes.

Using TopLink 11 with WebLogic 10

If you are using TopLink 11 preview (or EclipseLink) you do not have to create your own server platform class but you can use the server platform shipped in toplink.jar for WebLogic 10.

Here is how your persistence.xml look like when you use TopLink 11.

<persistence-unit name="actionBazaar">
<provider>
oracle.toplink.PersistenceProvider
</provider>
<jta-data-source>
jdbc/ActionBazaarDS</jta-data-source>

<properties>
<property name="toplink.ddl-generation" value="create-tables">
<property name="toplink.ddl-generation.output-mode" value="database">

<property name="toplink.target-server" value="WebLogic_10">

</property>


Note that you have to package toplink-ojdbc.jar in addition to toplink.jar and antlr.jar if you are using Oracle DB as the underlying database.

Hope this helps.
Need to go to bed, feeling sleepy now!

Sunday, March 23, 2008

EJB 3 In Action Examples on WebLogic 10

Several readers requested me to provide the code examples of EJB 3 in Action for BEA WebLogic 10. We have earlier provided examples for Glassfish, Oracle Application Server and JBoss. I spent sometime last weekend to port these examples to Weblogic 10. You can download examples for first four chapters.


These examples have been modified to run on the default examplesServer on the Weblogic in the default server domain. To run the examples, first the set the environment variables by the running the SetExamplesEnv script in $BEA_HOME/wlserver_10.0/samples/domains/wl_server directory.

For example, if you are running on Windows and you have installed WebLogic 10 in C:\bea\wlserver_10.0\ directory you can run the script as follows:

C:\bea\wlserver_10.0\samples\domains\wl_server\SetExamplesEnv.bat


If you unzipped the samples in c:\WLS examples change directory to chapter 1 and you can build and deploy as follows:

cd c:\WLS examples\chapter1

ant - -> attempts to build and deploy in the examplesServer


To run the example:

ant run


You must be wondering what are the changes that I made in the to run on WebLogic 10.

Java EE promises write once run anywhere ! Then why I had to make changes in the code?

Every application server has its minor quirks here and there and code changes were very minimal.

I could not make dependency injection work in the application client code in WebLogic 10, not sure they support it. So I had to change thick client to make use JNDI lookup instead. I modified the Session bean code to define a global JNDI name for the HelloUser session bean as follows: @Stateless(mappedName="HelloUser")

public class HelloUserBean implements HelloUser


I modified the client code to lookup the remote EJB as follows:

Context ctx = new InitialContext(); helloUser = (HelloUser) ctx.lookup("HelloUser#ejb3inaction.example.HelloUser");


As you would expect, I updated the jndi.properties in the client jar file to have right environment properties for WebLogic as follows:

java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory

java.naming.provider.url=t3://localhost:7001
That's it ..

In the next blog entry, I will discuss the code changes the instructions to run Chapter 2-4 and outline changes made to run JPA and MDB examples.