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:
Now, we'll create the stateless session bean:
This will create four files, the essential parts of the EJB:
The Home Interface HelloWorldHome.javaThe 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.javaAs 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.javaThe 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.xmlJDeveloper 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.
We don't need to make any changes to this file. Running the Bean Inside the JDeveloper EnvironmentTo test the bean, we'll run it inside the embedded application server:
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 BeanTo create a sample application:
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:
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. |