The Basics of Writing Enterprise JavaBean Code

Part of the Enterprise JavaBeans For Dummies Cheat Sheet

An Enterprise JavaBean (EJB) is like a mini-program that confers some unique functionality to the application, or container, it runs in. Below are the fundamentals of writing EJP code.

Summary of frequently used EJB interfaces

The following table identifies the interfaces you need to implement for each type of Enterprise JavaBean (EJB) that you create.

Interface/Class Message-Driven Bean Session Bean Entity Bean
Remote interface None javax.ejb.EJBObject javax.ejb.EJBObject
Local interface None javax.ejb.EJBLocalObject javax.ejb.EJBLocalObject
Remote Home interface None javax.ejb.EJBHome javax.ejb.EJBHome
Local Home interface None javax.ejb.EJBLocalHome javax.ejb.EJBLocalHome
Bean class javax.ejb.MessageDrivenBean javax.ejb.SessionBean javax.ejb.EntityBean

The EJB 2.0 DOCTYPE tag

The following DOCTYPE tag must be included in all EJB 2.0 deployment descriptor files:

<!DOCTYPE ejb-jar PUBLIC 
            "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
  "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

Basic description of a session bean

The following block of XML contains a typical entry for describing a session bean in the EJB application's deployment descriptor:

<session>
  <ejb-name>MyEJBName</ejb-name>
  <home>fully.qualified.RemoteHomeInterfaceName</home>
  <remote>fully.qualified.RemoteInterfaceName</remote>
  <local-home>fully.qualified.LocalHomeInterfaceName</local-home>
  <local>fully.qualified.LocalInterfaceName</local>
  <ejb-class>fully.qualified.BeanClassName</ejb-class>
  <session-type>Stateless | Stateful</session-type>
  <transaction-type>Container | Bean</transaction-type>
</session>

For the <session-type> attribute the value can be either Stateless or Stateful. For the <transaction-type> attribute the value can be either Container or Bean.

Basic description of an entity bean

The following block of XML code is a typical description for an entity bean class in the EJB application's deployment descriptor:

<entity>
  <ejb-name>EJBComponentName</ejb-name>
  <home>fully.qualified.RemoteHomeInterfaceName</home>
  <remote>fully.qualified.RemoteInterfaceName</remote>
  <local-home>fully.qualified.LocalHomeInterfaceName</local-home>
  <local>fully.qualified.LocalInterfaceName</local>
  <ejb-class>fully.qualified.BeanClassName</ejb-class>
  <prim-key-class>fully.qualified.PrimaryKeyName</prim-key-class>
  <persistence-type>Container | Bean</persistence-type>
  <reentrant>False</reentrant>
  <persistence-type>2.x<persistence-type>
  <cmp-field><field-name>fieldName</field-name></cmp-field>
  <primkey-field>primaryKeyFieldName</primkey-field>
</entity>

For the <persistence-type> attribute the value can be either Container or Bean.

Basic description of a message-driven bean

The following block of XML illustrates a typical description of a message-driven bean in the deployment descriptor:

<message-driven>
  <ejb-name></ejb-name>
  <ejb-class>fully.qualified.BeanClassName</ejb-class>
  <transaction-type>Container | Bean</transaction-type>
  <acknowledge-mode>
    Auto-acknowledge | Dups-ok-acknowledge
  </acknowledge-mode>
  <message-driven-destination>
    <destination-type>javax.jms.Queue | javax.jms.Topic
    </destination-type>
<!-- use the following only if destination type is Topic.→
    <subscription-durability>
      Durable | NonDurable
    </subscription-durability>
  </message-driven-destination>
</message-driven>

For the <transaction-type> attribute, the value can be either Container or Bean. For the <destination-type> attribute, the value can be either javax.jms.Queue or javax.jms.Topic. For the <subscription-durrability> attribute, the value can be either Durrable or NonDurable.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus

SERIES
Enterprise JavaBeans For Dummies Cheat Sheet

Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.