EJB applications

EJB applications are packaged as JAR files referred to as EJB JAR files. An EJB application is synonymous with the standalone EJB module (which is not packaged in an EAR). Geronimo provides the ability to deploy standalone EJB modules. The deployment descriptor ejb-jar.xml is packaged in the META-INF directory in the JAR file. As we will see later in this section, ejb-jar.xml may not be required, and the meta information in this case is derived from the annotations. Geronimo integrates OpenEJB to provide EJB3 support. Geronimo/OpenEJB fully supports Container Managed Persistence (CMP), although JPA is considered to be a better approach in Java EE. In this section, we focus on session- and message-driven EJBs.

Annotations

In this section, we examine some annotations used in EJB3 applications.

  • Stateless: The @Stateless annotation is used on a stateless session bean class. The name attribute of the annotation is used to specify the name of the EJB. If this attribute is not specified, then the default name of the EJB is the unqualified Java class name of the bean class.

  • Stateful: The @Stateful annotation is used on a stateful session bean class. The name attribute of the annotation is used to specify the name of the EJB. If this attribute is not specified, then the default name of the EJB is the unqualified Java class name of the bean class.

  • PostConstruct: The @PostConstruct annotation is used on a session bean method. This method is invoked after the EJB instance is created and all references have been injected.

  • PreDestroy: The @PreDestroy annotation is used on a session bean method. This method is invoked before the EJB instance is destroyed.

  • PostActivate: The @PostActivate annotation is used on a stateful session bean method. This method is invoked after the container has activated a passivated bean instance. Note that this method is applicable only to stateful session beans.

  • PrePassivate: The @PrePassivate annotation is used on a stateful session bean method. This method is invoked before the container passivated a bean instance. Note that this method is applicable only to stateful session beans.

  • MessageDriven: The @MessageDriven annotation is used on a message-driven bean class. The name attribute of the annotation is used to specify the name of the EJB. If this attribute is not specified, then the default name of the EJB is the unqualified Java class name of the bean class. The activationConfig attribute of the annotation is used to specify the activation configuration parameters. The activation configuration parameters are specified by using the @ActivationConfigProperty annotation. The @ActivationConfigProperty annotation has two attributes, namely propertyName and propertyValue.

  • EJB: The @EJB annotation is used on an EJB's fields or setter methods to define an EJB reference. The name attribute specifies the EJB reference name. If the name attribute is not specified, then the default value is class-name/field-name in the case of an annotation on a field, and class-name/property-name in the case of an annotation on a setter method. The EJB can be looked up from the beans context by using the reference name. For example, the EJB reference defined by the following code:

    @EJB(name="languageService")
    LanguageService langService;
    

    can be looked up by using the following code:

    InitialContext ic = new InitialContext();
    ic.lookup("languageService");
    
  • Resource: The @Resource annotation is used to annotate resources such as JMS Connection Factories, JMS Queues, JMS Topics, DataSources, and so on. This annotation can be used on a field or a setter method. The name attribute of the annotation specifies the name that can be used to look up the resource in the bean's context. For example, the @Resource annotation in the following code:

    @Resource(name="jdbc/ds")
    private DataSource ds;
    

    results in resources bound in the bean's context at jdbc/ds. The following deployment plan maps the resource:

    <ejb:session>
    <ejb:ejb-name>HelloworldBean</ejb:ejb-name>
    <ejb:jndi-name>HelloworldServiceBean</ejb:jndi-name>
    <nam:resource-ref xmlns:nam= "http://geronimo.apache.org/xml/ns/naming-1.2">
    <nam:ref-name>jdbc/ds</nam:ref-name>
    <nam:resource-link>SystemDatasource</nam:resource-link>
    </nam:resource-ref>
    </ejb:session>
    

    Here, the bean's field ds is injected with the physical resource SystemDatasource mapped in the deployment plan.

An @Resource annotation on a field of type Boolean, Byte, Character, String, Short, Integer, Long, Float, or Double will result in an injection of the corresponding env-entry value defined in the deployment descriptor. For example, the @Resource annotation defined in the following code:

@Resource(name="country")
private String country;

results in the env-entry value defined in the following deployment descriptor being injected into the bean field:

<session>
<ejb-name>HelloworldBean</ejb-name>
<env-entry>
<env-entry-name>country</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>USA</env-entry-value>
</env-entry>
</session>

Here the bean field country is injected with the value USA.

The @Resource annotation can be used on a SessionContext field to get the bean's context injected into the field. The following code:

@Resource
private SessionContext ctx;

results in the bean's context being injected into the field ctx.

EJB deployment plan

The deployment plan for an EJB module is openejb-jar.xml and is packaged in the META-INF directory in the JAR file. The deployment plan can also be supplied as an external file during deployment. The schema geronimo-openejb-2.0.xsd is shown in the following image:

The following is an explanation of the various elements:

  • environment: This element specifies the Geronimo environment for the EJB module. See Appendix A, Deployment Plans, for more details.

  • jndiEnviromentRefsGroup: The JNDI Environment references group elements are used to map the resources to JNDI names. See Chapter 8, Naming and JNDI, for more details.

  • message-destination: This element is used to configure references to a JMS queue or topic which are actually message destinations.

  • web-service-binding: This element provides the address and security information of the web service that is implemented by this EJB.

  • security: This is an abstract element that will be used for supporting application level security features, that is, in the actual plan, this element will be replaced by other elements that implement this element.

The schema openejb-jar-2.2.xsd of the EJB deployment plans is shown in the figure below:

The following is an explanation of the various elements:

  • ejb-ql-compiler-factory: Specifies the fully-qualified class name of a Java class that can compile EJB-QL (Query Language) queries into SQL statements for a particular database product. This class must implement org.tranql.sql.EJBQLCompilerFactory.

  • db-syntax-factory: Specifies the fully-qualified name of a Java class that can customize CMP SQL statements for a particular database product. This class must implement org.tranql.sql.DBSyntaxFactory.

  • enforce-foreign-key-constraints: If this element is present, then Geronimo will execute insert, update, and delete statements in an order consistent with the foreign keys between tables. If this element is not present, then Geronimo may execute the statements in any order, but within the same transaction.

  • enterprise-beans: This element is used to specify the references by entity, session, and message driven beans.

  • relationships: This element is used to map the container-managed relationships defined in the deployment descriptor to a specific database.

The following are the common elements under enterprise-beans:

  • ejb-name: Specifies the name of the EJB to which the configuration applies. This must match the name specified by ejb-name under the deployment descriptor.

  • jndi-name: The name at which the home interface of the EJB is registered in the JNDI. This is applicable only if the EJB has a remote home interface.

  • local-jndi-name: The name at which the local home interface of the EJB is registered in the JNDI. This is applicable only if the EJB has a local home interface.

  • tss-link and tss: These elements are used to specify the CORBA security settings. This is applicable if the EJB is to be accessed over CORBA. See Chapter 7, CORBA, for more details on these elements.

Sample EJB application

The sample EJB application that we have provided has two session beans, namely, HelloworldBean and LanguageServiceBean, and a Message Driven Bean, namely, SampleMDB. The deployment descriptor for this application is given below:

<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<enterprise-beans>
<session>
<ejb-name>HelloworldBean</ejb-name>
<post-construct>
<lifecycle-callback-class>sample.ejb3.HelloworldServiceBean </lifecycle-callback-class>
<lifecycle-callback-method>postConstruct </lifecycle-callback-method>
</post-construct>
<env-entry>
<env-entry-name>country</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>USA</env-entry-value>
</env-entry>
</session>
</enterprise-beans>
</ejb-jar>

Note that this deployment descriptor adds to the metadata obtained through annotations in the java code of the beans.

The deployment plan for this application is given below:

<?xml version="1.0" encoding="UTF-8"?>
<ejb:openejb-jar xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2" xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2">
<dep:environment>
<dep:moduleId>
<dep:groupId>test</dep:groupId>
<dep:artifactId>simple-ejb</dep:artifactId>
<dep:version>1.0</dep:version>
<dep:type>jar</dep:type>
</dep:moduleId>
<dep:dependencies>
<dep:dependency>
<dep:groupId>org.apache.geronimo.configs</dep:groupId>
<dep:artifactId>system-database</dep:artifactId>
<dep:type>car</dep:type>
</dep:dependency>
<dep:dependency>
<dep:groupId>packt-samples</dep:groupId>
<dep:artifactId>jms-resources</dep:artifactId>
<dep:type>rar</dep:type>
</dep:dependency>
</dep:dependencies>
<dep:hidden-classes/>
<dep:non-overridable-classes/>
</dep:environment>
<ejb:enterprise-beans>
<ejb:session>
<ejb:ejb-name>HelloworldBean</ejb:ejb-name>
<ejb:jndi-name>HelloworldServiceBean</ejb:jndi-name>
<nam:resource-ref xmlns:nam="http://geronimo.apache.org/xml/ ns/naming-1.2">
<nam:ref-name>jdbc/ds</nam:ref-name>
<nam:resource-link>SystemDatasource</nam:resource-link>
</nam:resource-ref>
</ejb:session>
<ejb:message-driven>
<ejb:ejb-name>SampleMDB</ejb:ejb-name>
<nam:resource-adapter xmlns:nam="http://geronimo.apache.org/xml/ ns/naming-1.2">
<nam:resource-link>jms-resources</nam:resource-link>
</nam:resource-adapter>
</ejb:message-driven>
</ejb:enterprise-beans>
</ejb:openejb-jar>

The JMS resources required by the MDB are deployed in the packt-samples/jms-resources/1.0/rar module. Notice that a dependency on this module is declared in the deployment plan.

Deploy the JMS resources

In order to run the sample EJB application, you must first deploy the JMS resources required by the Message Driven Bean. In order to do so, we deploy <GERONIMO_HOME> epositoryorgapachegeronimomodulesgeronimo-activemq-ra2.1.4geronimo-activemq-ra-2.1.4.rar with the jms-resources.xml plan, provided under the samples.

Deploy the EJB sample

After deploying the JMS resources, deploy the EJB sample application sample-ejb.jar, provided under the samples.

Deploy the Web application

To test the sample EJB application, we have provided a sample web application named helloworld-web.war that has a servlet to invoke the EJBs and post messages to the message-destination configured with SampleMDB. Once deployed, we can access the application at http://localhost:8080/helloworld.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset