Introduction to Bean Building: A Stateless Session Bean

In the previous chapter, we used JDeveloper to create a container-managed persistent entity bean automatically for us. At that time, we merely used the bean as a sort of magic data access object and didn't examine it in detail. For bean types other than CMP, JDeveloper produces some common boilerplate code, but by itself, this code has no functionality and we'll need to add some code of our own in order to complete the bean.

To become familiar with the details of EJBs and the process of developing, packaging, and deploying them and before moving on to the issues of persistence we'll start with a simple Hello, world example, using a stateless session bean.

First we'll add a new project, HelloWorld, to the JDeveloper workspace we created in the last chapter:

  • Right-click on the workspace ejbExamples.

  • Select New Project.

  • Select Projects on the left side of the box that appears and Empty on the right.

  • Change the directory name to HelloWorld.

  • Change the filename to HelloWorld.jpr and press OK.

Now, we'll create the stateless session bean:

  • Right-click on the new project, HelloWorld.jpr.

  • Select New…

  • Click on Enterpise JavaBeans on the left side of the box and Stateless Session bean on the right. Press OK.

  • On the Enterprise JavaBean Wizard welcome screen, press Next.

  • Change the EJB name to HelloWorld; press Next.

  • Accept the defaults in the next screen, and press Finish.

This will create four files, the essential parts of the EJB:

  • HelloWorldHome.java The home interface, extends EJBHome.

  • HelloWorldBean.java The bean implementation, implements sessionBean.

  • HelloWorld.java The remote interface, extends EJBObject.

  • Deployment descriptor An XML file.

The Home Interface HelloWorldHome.java

The home interface allows us to obtain a reference to the bean. This is the home interface that JDeveloper created for us:

 package mypackage2; import javax.ejb.EJBHome; import java.rmi.RemoteException; import javax.ejb.CreateException; public interface HelloWorldHome extends EJBHome {   HelloWorld create() throws RemoteException, CreateException; } 

This is fine just the way it is. Typically, we don't need to do anything special in the home interface of a session bean.

The Bean Implementation HelloWorldBean.java

As it stands, our bean won't do much. We need to add a method to the bean implementation that returns a Hello, world message. This is what the bean implementation looks like before we touch it:

 package mypackage2.impl; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class HelloWorldBean implements SessionBean {   public void ejbCreate()   {   }   public void ejbActivate()   {   }   public void ejbPassivate()   {   }   public void ejbRemove()   {   }   public void setSessionContext(SessionContext ctx)   {   } } 

Notice that JDeveloper has created a number of methods that begin with ejb. These are required methods that the application server will call during the life cycle of the bean. We can add code to these if we want the bean to do something special immediately after it's created or before it's destroyed, for example. Otherwise, we can just leave them empty, as we will here.

The setSessionContext() gives us the opportunity to save a reference to the context that the bean is running in. We could create a class attribute, SessionContext ctx, and set it using a line of code like this:

 this.ctx=ctx; 

This can be used for obtaining session information and information about the bean itself. This is important in some cases, but we won't need it for this example.

The only change we'll make is to add this method after the setSessionContext(), for example:

 public String getMessage() {   return "Hello, world"; } 

The Remote Interface HelloWorld.java

The bean implementation isn't the only code we need to change. Client programs interact with our bean only indirectly. What clients actually see is the remote interface. For every method in our bean implementation that we want a client to able to call, we need to add the method's signature to the remote interface. Here is what the interface looks like as initially created by JDeveloper:

 package mypackage2; import javax.ejb.EJBObject; public interface HelloWorld extends EJBObject { } 

We'll add the following line to the HelloWorld class:

 public String getMessage() throws java.rmi.RemoteException; 

Notice that, because this is the remote interface, it is possible that an error may occur while calling it, due to network problems, for example, so we must indicate that it can throw a RemoteException.

The Deployment Descriptor ejb-jar.xml

JDeveloper also creates the deployment descriptor: an XML file that describes an EJB (or EJBs, if we have more than one in a project). The descriptor for a stateless session bean is fairly simple, as deployment descriptors go, but it's still nice not to have to type it in by hand.

[View full width]

<?xml version = '1.0' encoding = 'windows-1252'?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN" graphics/ccc.gif"http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd"> <ejb-jar> <enterprise-beans> <session> <description>Session Bean ( Stateless )</description> <display-name>HelloWorld</display-name> <ejb-name>HelloWorld</ejb-name> <home>mypackage2.HelloWorldHome</home> <remote>mypackage2.HelloWorld</remote> <ejb-class>mypackage2.impl.HelloWorldBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar>

We don't need to make any changes to this file.

Running the Bean Inside the JDeveloper Environment

To test the bean, we'll run it inside the embedded application server:

  • Right-click on the HelloWorld bean.

  • Select Run HelloWorld.

The log window will display messages as the classes are compiled, the embedded server is started, and beans are deployed. If there are no problems, we can next create a sample application to test the HelloWorld bean.

Creating a Sample Application to Test the HelloWorld Bean

To create a sample application:

  • Right-click on the HelloWorld bean.

  • Select Create Sample Java Client.

  • Ensure that Connect to OC4J Embedded in JDeveloper is selected and press OK.

This will create a sample application. It will contain code to create a bean and to call our method but it will be commented out, like this:

   // Use one of the create() methods below to create a new instance   // helloWorld = helloWorldHome.create(  );   // Call any of the Remote methods below to access the EJB   // helloWorld.getMessage(  ); } 

Uncomment the call to helloWorldHome.create() and put the call to our bean, helloWorld.getMessage(), inside a call to System.out.println(), like this:

 // Use one of the create() methods below to create a new instance helloWorld = helloWorldHome.create(  ); // Call any of the Remote methods below to access the EJB System.out.println(helloWorld.getMessage(  )); 

Now run it:

  • Right-click on HelloWorldClient.java.

  • Select Run HelloWorldClient.java from the popup menu.

The message Hello, world should appear in the log window. If it doesn't, examine each of the error logs by selecting the tabs at the top of the log window for compilation errors or other problems.



Java Oracle Database Development
Java Oracle Database Development
ISBN: 0130462187
EAN: 2147483647
Year: 2002
Pages: 71

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net