Write a session EJB

Find out how EJB frees developers from the complexity of middleware development by providing complete infrastructure support

1 2 Page 2
Page 2 of 2

The ejbActivate() and ejbPassivate() methods are used by the container to notify the bean that it is about to be taken out of or put into secondary storage, respectively.

// ForumSessionServerHome method public void ejbCreate (String d, String db, String u, String p, String t) { // called second by container this.driver = d; this.dbURL = u; this.passwd = p; this.tableName = t; }

// ForumSessionServer business methods Hashtable loadAllThreads () { // open db connection // load threads from db // close db connection // return a Hashtable with the threads and empty Vectors }

Vector loadThreadArticles (String t) { // open db connection // load articles from thread t from db // close db connection // return a Vector with all the articles from thread t }

boolean postArticle (String art, String t) { // open db connection // post article on thread t to the database // close db connection return true; } }

The method of ForumSessionServerHome is the single create (...) method. This method can be overloaded to accept any number of arguments. These arguments are used to pass initial "conversational state" into the client's bean object.

The business methods should be filled in with database-specific code. Note that a session EJB does not keep permanent state information as an entity EJB does. The session bean is visible only to the client that creates it and is (eventually) removed when its client goes away.

Now that the bean code is defined, it should be compiled into class files for deployment packaging.

The deployment descriptor

The deployment descriptor provides details on EJB deployment to the runtime container. The descriptor is a serialized instance of

javax.ejb.deployment.SessionDescriptor

. EJB provider tools typically handle the task of creating the serialized deployment descriptor. However, you can do it manually by writing a mini-application that creates an instance of the deployment descriptor and serializes it to the filesystem. The deployment descriptor instance has its session timeout set to a timeout value, or 0, to indicate that the container should use a container default timeout. It also has its

StateManagementType

set to

STATEFUL_SESSION

or

STATELESS_SESSION

, depending on whether or not the bean keeps state in instance variables.

For the ForumSessionServer bean, we call the following methods on our deployment descriptor instance:

deploymentDescriptor.setSessionTimeout (0);
deploymentDescriptor.setStateManagementType (SessionDescriptor.STATELESS_SESSION);

Once these values are set, the deployment descriptor instance should be serialized to a file. The serialized deployement descriptor for the ForumSessionServer should be called ForumSessionServerDeployment.ser.

Packaging the ForumSessionServer EJB

The final step for the bean provider is to package the ForumSessionServer EJB into an ejb-jar file for deployment. The ejb-jar file contains a manifest file that identifies the EJBs included in the file. The manifest file must be named META-INF/MANIFEST.MF. The format of the manifest file for the ForumSessionServer is the following, with the Name: field set to the name of the deployment descriptor:

Name: ForumSessionServerDeployment.ser
Enterprise-Bean: True

The two interface files and the bean class file go into the ejb-jar, along with the deployment descriptor and a serialized Properties object, if one is needed. The Properties object has key/value pairs that represent any environment properties that the bean will need when it is instantiated by the container. Our ForumSessionServerBean doesn't require a Properties object.

All that's left is to create the ejb-jar file (use the jar utility) and the bean is completely packaged and ready to be deployed.

Deployment to the EJB container

The person acting as EJB deployer will deploy the ejb-jar file to the EJB server node. The EJB container tools, provided by the container vendor, will generate the support classes necessary to do this.

Using the ForumSessionServer in the Forum client

Recall that each client instance of the Forum consists of two primary objects -- a Forum, which is a Frame, and an associated communications object. The Forum contains the GUI and handles user events, and the ForumComm implements the actual networking tasks involved with talking to the server.

The Forum client essentially needs only to replace its RMI lookup code with JNDI code to find the ForumSessionServer:

// props is a Properties that contains the server location and type of context
Context context = new InitialContext (props);
ForumSessionServerHome fssh;
fssh = (ForumSessionServerHome) context.lookup("apps/ForumSessionServer");
ForumSessionServer server;
server = fssh.create ("driver", "location", "pass", "tablename");

The new code goes in ForumComm in place of the RMI code that is already there.

When the client is ready to shut down, it should remove the ForumSessionServer bean object from the server:

server.remove ();

Conclusion

As you can see, writing a session bean is pretty easy. Thanks to the support that the container and server provide, you are not responsible for writing networking, synchronization, transaction management, or any other infrastructure to support the bean's business logic. The work to produce the original RMI implementation was far more tedious!

In addition, you can deploy the ForumSessionServer EJB on any node running an EJB server. You can also provide it to a third party for use as a building block in an application.

You can apply the techniques used in creating the Forum EJB to projects of your own if the backend has session-like characteristics. You can also use entity EJBs, which are a bit more complicated, to model objects that represent permanent entities. In either case, writing to EJB confers numerous advantages over more "old fashioned" ways of writing applications.

The software industry is a strange world, where the only real sanctuary is to be quick or be dead. Fear of the dark can make you rue the evil that men do and all the wasted years. Sometimes it seems that the iron maiden would be kinder. But Michael Shoffner, like thousands of his fellow developers, tries his best every day to be a trooper and fly where eagles dare.

Learn more about this topic

  • Download the complete source as a zip file http://www.javaworld.com/jw-07-1998/step/jw-07-step.zip
  • Download the complete source as a gzipped tar file http://www.javaworld.com/jw-07-1998/step/jw-07-step.tar.gz
  • Information about EJB
  • EJB development libraries and documentation
  • Previous Step by Step articles

1 2 Page 2
Page 2 of 2