Thursday, January 17, 2008

Deploy EJB With JBoss

Starting JBoss
On Windows:


Use the setvars.bat script on Windows, located in your JDeveloper\jdev\bin directory to set the JAVA_HOME and PATH environment variables. For help on using the setvars script, just execute setvars.bat and usage examples will be displayed.

Example:

setvars -go

On UNIX (Bourne Shell):

Set the JAVA_HOME environment variable and add the Java2 SDK's bin directory to your PATH.

Example:

export JAVA_HOME=/usr/java/jdk1.3

export PATH=${PATH}:${JAVA_HOME}/bin

Then execute the run script (or run_with_tomcat) located in the JBoss/bin directory. ( For example, this might be C:\JBoss-2.4.4\bin).

After JBoss is started, you will see many lines of information printed to the command shell.





Creating a Library in JDeveloper for JBoss
Oracle9i JDeveloper needs a library definition which contains all of the client side libraries from JBoss to test an Enterprise JavaBean deployed inside JBoss. All of these archives are located in the client directory under the JBoss directory. (For example: C:\JBoss-2.4.4\client). Create a new Library in JDeveloper, call it "JBoss" and add all of these .jar files to the library. Here is an example of the library definition as created with JDeveloper:





Creating a Simple EJB
Create a New Stateless Session Bean. This is a choice under the Enterprise JavaBeans category from the New Gallery. Follow all of the defaults provided by the Wizard. In the Enterprise JavaBean Wizard, click Next to accept all defaults, then click Finish. In the EJB Class Editor, select the Fields tab, and add a new field called "info". This creates a method on the EJB that can be invoked in the EJB test application.



Creating a jboss.xml File for EJB Customization.
This step is not required, however JBoss, like other Application Servers support vendor-specific configuration options for the Enterprise Java Beans. One reason to use a jboss.xml file is to modify the JNDI name of your EJB. More information on jboss.xml is available in the JBoss documentation.

To create a jboss.xml file in your project, simply:


*Click the Open menu item from the File Menu.
*Navigate to the META-INF directory under your source path.
*Enter jboss.xml as the filename.
*Edit the newly created XML file manually.

You can include this in the EJB's .jar file at deployment time.







Here is an example of a very basic jboss.xml:







<jboss>
<enterprise-beans>
<session>
<ejb-name>MySessionEJB</ejb-name>
<jndi-name>anotherPath/AnotherNameForMySessionEJB</jndi-name>
</session>
</enterprise-beans>
</jboss>


 





Creating the Test Client for JBoss
Create a test client for your EJB with the "Create Sample Java Client" context menu item from your EJB node in the Navigator. Choose "Connect to Remote App Server" in the details dialog, and ignore the values of the J2EE Application Name and Oracle9iAS Connection Name.



The default test client needs to be modified for use with JBoss because JNDI properties for JBoss are different than Oracle9iAS, replace the initial environment with the following:

env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "localhost");

env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" );



Add code to invoke the setInfo and getInfo methods previously added by the EJB Class Designer. Here is an example of the test application modified to invoke these methods:






      Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "localhost");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" );
Context ctx = new InitialContext(env);
MySessionEJBHome mySessionEJBHome = (MySessionEJBHome)ctx.lookup("MySessionEJB");
// Use one of the create() methods below to create a new instance
MySessionEJB mySessionEJB = mySessionEJBHome.create( );
// Call any of the Remote methods below to access the EJB
mySessionEJB.setInfo( "Hello World" );
System.out.println( mySessionEJB.getInfo() );





Deploying the EJB jar File to JBoss
Use these steps to create and deploy an EJB .jar file.

Invoke the New Gallery by clicking the New menu item under the File menu.
Choose the "Deployment Profiles" as the category, then "J2EE EJB Module (EJB JAR file)"
Click OK.
Leave the name the default "ejb1.deploy"
Just click "OK" on the J2EE EJB Module Deployment Profile Settings which is invoked, no changes need to be made here.
In the Navigator, right-click on the "ejb1.deploy" node.
Choose "Deploy to JAR file" on the context menu.
On the Message window, under the Deployment tab, a message will state the location of the newly created .jar file. Copy this file to the JBoss\deploy directory.
Here is an example of the text displayed in the Message Log on the Deployment tab:

Beginning to deploy to the EJB JAR file...
Wrote EJB .jar file to D:\jdev\9irc\jdev\mywork\Workspace2\Project1\ejb1.jar
---- Deployment finished. ---- Feb 6, 2002 2:58:05 PM





Once JBoss detects a file has changed in this location, it will automatically deploy your EJB. After JBoss has deployed the EJB, a message will be displayed in the console window in which JBoss was started.





Testing the EJB in JBoss
Edit your project properties, and remove the J2EE and Oracle9i iAS libraries, and add the JBoss library to your project.
Run the test client.
Verify that "Hello World" is displayed in the Message Log.

References:
JBoss

Life Cycle Of EJB

The Life Cycles of Enterprise Beans :

An enterprise bean goes through various stages during its lifetime, or life cycle. Each type of enterprise bean--session, entity, or message-driven--has a different life cycle.

The descriptions that follow refer to methods that are explained along with the code examples in the next two chapters. If you are new to enterprise beans, you should skip this section and try out the code examples first.

The Life Cycle of a Stateful Session Bean :
Figure 23-4 illustrates the stages that a session bean passes through during its lifetime. The client initiates the life cycle by invoking the create method. The EJB container instantiates the bean and then invokes the setSessionContext and ejbCreate methods in the session bean. The bean is now ready to have its business methods invoked.




Figure 23-4 Life Cycle of a Stateful Session Bean

While in the ready stage, the EJB container may decide to deactivate, or passivate, the bean by moving it from memory to secondary storage. (Typically, the EJB container uses a least-recently-used algorithm to select a bean for passivation.) The EJB container invokes the bean's ejbPassivate method immediately before passivating it. If a client invokes a business method on the bean while it is in the passive stage, the EJB container activates the bean, calls the bean's ejbActivate method, and then moves it to the ready stage.

At the end of the life cycle, the client invokes the remove method, and the EJB container calls the bean's ejbRemove method. The bean's instance is ready for garbage collection.

Your code controls the invocation of only two life-cycle methods: the create and remove methods in the client. All other methods in Figure 23-4 are invoked by the EJB container. The ejbCreate method, for example, is inside the bean class, allowing you to perform certain operations right after the bean is instantiated. For example, you might wish to connect to a database in the ejbCreate method. See Chapter 31 for more information.

The Life Cycle of a Stateless Session Bean
Because a stateless session bean is never passivated, its life cycle has only two stages: nonexistent and ready for the invocation of business methods. Figure 23-5 illustrates the stages of a stateless session bean.





Figure 23-5 Life Cycle of a Stateless Session Bean

The Life Cycle of an Entity Bean
Figure 23-6 shows the stages that an entity bean passes through during its lifetime. After the EJB container creates the instance, it calls the setEntityContext method of the entity bean class. The setEntityContext method passes the entity context to the bean.

After instantiation, the entity bean moves to a pool of available instances. While in the pooled stage, the instance is not associated with any particular EJB object identity. All instances in the pool are identical. The EJB container assigns an identity to an instance when moving it to the ready stage.

There are two paths from the pooled stage to the ready stage. On the first path, the client invokes the create method, causing the EJB container to call the ejbCreate and ejbPostCreate methods. On the second path, the EJB container invokes the ejbActivate method. While an entity bean is in the ready stage, an it's business methods can be invoked.

There are also two paths from the ready stage to the pooled stage. First, a client can invoke the remove method, which causes the EJB container to call the ejbRemove method. Second, the EJB container can invoke the ejbPassivate method.







Figure 23-6 Life Cycle of an Entity Bean

At the end of the life cycle, the EJB container removes the instance from the pool and invokes the unsetEntityContext method.

In the pooled state, an instance is not associated with any particular EJB object identity. With bean-managed persistence, when the EJB container moves an instance from the pooled state to the ready state, it does not automatically set the primary key. Therefore, the ejbCreate and ejbActivate methods must assign a value to the primary key. If the primary key is incorrect, the ejbLoad and ejbStore methods cannot synchronize the instance variables with the database. In the section The SavingsAccountBean Example, the ejbCreate method assigns the primary key from one of the input parameters. The ejbActivate method sets the primary key (id) as follows:

id = (String)context.getPrimaryKey();
In the pooled state, the values of the instance variables are not needed. You can make these instance variables eligible for garbage collection by setting them to null in the ejbPassivate method.

The Life Cycle of a Message-Driven Bean
Figure 23-7 illustrates the stages in the life cycle of a message-driven bean.

The EJB container usually creates a pool of message-driven bean instances. For each instance, the EJB container instantiates the bean and performs these tasks:

It calls the setMessageDrivenContext method to pass the context object to the instance.
It calls the instance's ejbCreate method.



Figure 23-7 Life Cycle of a Message-Driven Bean

Like a stateless session bean, a message-driven bean is never passivated, and it has only two states: nonexistent and ready to receive messages.

At the end of the life cycle, the container calls the ejbRemove method. The bean's instance is then ready for garbage collection.