Article
Enterprise JavaBeans: A Primer
3. A class that implements either javax.ejb.SessionBean or javax.ejb.EntityBean (referred to as the Enterprise Bean Class), as the following example illustrates:
package com.jeffhanson.ejb;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class SimpleMessengerBean
implements SessionBean
{
private SessionContext sessionCtx = null;
public String retrieveMessage()
{
return "Hello from SimpleMessengerBean ";
}
public void ejbCreate()
{
}
public void ejbRemove()
{
}
public void ejbActivate()
{
}
public void ejbPassivate()
{
}
public void setSessionContext(SessionContext sessionCtx)
{
this.sessionCtx = sessionCtx;
}
}
The Deployment Descriptor
To deploy an EJB into an EJB container's environment, you must supply a deployment descriptor file to the EJB container. A deployment descriptor file is an XML document, named ejb-jar.xml, which specifies information about the bean such as its persistence type and transaction attributes. You need to package the java classes and the deployment descriptor into a JAR or EAR file.
The following is an example of a typical deployment descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<description>A sample EJB</description>
<display-name>Simple Message EJB</display-name>
<enterprise-beans>
<session>
<ejb-name>SimpleMessenger</ejb-name>
<home>com.jeffhanson.ejb.SimpleMessengerHome</home>
<remote>com.jeffhanson.ejb.SimpleMessenger</remote>
<ejb-class>com.jeffhanson.ejb.SimpleMessengerBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
Summary
Enterprise developers approaching J2EE often have to face such issues as transactions, remote access, database portability and instance pooling. EJBs help to solve these problems.
EJB technology defines a Java-based, server-side, reusable component framework for distributed applications that facilitates centrally-managed business logic, and declarative deployment.
The EJB specification and architecture are attempts to make remote, distributed, multi-tier development much easier. It has hoped to do this by introducing a concept known as an Enterprise Java Bean (EJB).
An Enterprise JavaBean is a Java object exposing a number of interfaces, which lives and executes inside of a runtime environment known as an EJB container.
An EJB container is a runtime environment for managing enterprise beans. The container hosts and manages an enterprise bean in much the same manner that a Java servlet engine hosts a Java servlet. The EJB container instantiates and controls the enterprise beans and provides them with system-level services.
The EJB architecture defines three distinct types of enterprise beans; session beans, entity beans and message-driven beans. Session beans and entity beans are invoked synchronously by an enterprise bean client. Message-driven beans (MDBs) are invoked by a message container, such as a publish/subscribe topic.
When considering the possible use of EJBs, you should take into account the following problems that EJBs can help to solve: scalability, transaction support, client-location transparency, data source portability, reusability and asynchronous messaging.
In our next article, we'll try to assuage any fears you may have to overcome when building your first EJB. We will carefully step through installing a J2EE SDK, writing, compiling, and deploying a simple Entity and Session bean, and a client Web application that uses them.