Red Hat Training

A Red Hat training course is available for Red Hat JBoss Enterprise Application Platform

Chapter 2. Creating Enterprise Bean Projects

2.1. Create an EJB Archive Project Using Red Hat JBoss Developer Studio

This task describes how to create an Enterprise JavaBeans (EJB) project in Red Hat JBoss Developer Studio.

Prerequisites

  • A server and server runtime for JBoss EAP have been configured in JBoss Developer Studio.

Create an EJB Project in Red Hat JBoss Developer Studio

  1. Open the New EJB Project wizard.

    1. Navigate to the File menu, select New, then select Project.
    2. When the New Project wizard appears, select EJB/EJB Project and click Next.

      Figure 2.1. New EJB Project Wizard

      New EJB Project Wizard
  2. Enter the following details:

    • Project name: The name of the project that appears in Red Hat JBoss Developer Studio, and also the default file name for the deployed JAR file.
    • Project location: The directory where the project files will be saved. The default is a directory in the current workspace.
    • Target runtime: This is the server runtime used for the project. This will need to be set to the same JBoss EAP runtime used by the server that you will be deploying to.
    • EJB module version: This is the version of the EJB specification that your enterprise beans will comply with. Red Hat recommends using 3.2.
    • Configuration: This allows you to adjust the supported features in your project. Use the default configuration for your selected runtime.

      Click Next to continue.

  3. The Java project configuration screen allows you to add directories containing Java source files and specify the directory for the output of the build.

    Leave this configuration unchanged and click Next.

  4. In the EJB Module settings screen, check Generate ejb-jar.xml deployment descriptor if a deployment descriptor is required. The deployment descriptor is optional in EJB 3.2 and can be added later if required.

    Click Finish and the project is created and will be displayed in the Project Explorer.

    Figure 2.2. Newly Created EJB Project in the Project Explorer

    Newly created EJB Project in the Project Explorer
  5. To add the project to the server for deployment, right-click on the target server in the Server tab and choose Add and Remove.

    In the Add and Remove dialog, select the resource to deploy from the Available column and click the Add button. The resource will be moved to the Configured column. Click Finish to close the dialog.

    Figure 2.3. Add and Remove Dialog

    Add and Remove Dialog

You now have an EJB project in Red Hat JBoss Developer Studio that can build and deploy to the specified server.

Warning

If no enterprise beans are added to the project then Red Hat JBoss Developer Studio will display the warning stating An EJB module must contain one or more enterprise beans. This warning will disappear once one or more enterprise beans have been added to the project.

2.2. Create an EJB Archive Project in Maven

This task demonstrates how to create a project using Maven that contains one or more enterprise beans packaged in a JAR file.

Prerequisites

  • Maven is already installed.
  • You understand the basic usage of Maven.

Create an EJB Archive Project in Maven

  1. Create the Maven project: An EJB project can be created using Maven’s archetype system and the ejb-javaee7 archetype. To do this run the mvn command with parameters as shown:

    $ mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=ejb-javaee7

    Maven will prompt you for the groupId, artifactId, version and package for your project.

    $ mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=ejb-javaee7
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Maven Stub Project (No POM) 1
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] >>> maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom >>>
    [INFO]
    [INFO] <<< maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom <<<
    [INFO]
    [INFO] --- maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom ---
    [INFO] Generating project in Interactive mode
    [INFO] Archetype [org.codehaus.mojo.archetypes:ejb-javaee7:1.5] found in catalog remote
    Define value for property 'groupId': : com.shinysparkly
    Define value for property 'artifactId': : payment-arrangements
    Define value for property 'version':  1.0-SNAPSHOT: :
    Define value for property 'package':  com.shinysparkly: :
    Confirm properties configuration:
    groupId: com.company
    artifactId: payment-arrangements
    version: 1.0-SNAPSHOT
    package: com.company.collections
    Y: :
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 32.440s
    [INFO] Finished at: Mon Oct 31 10:11:12 EST 2011
    [INFO] Final Memory: 7M/81M
    [INFO] ------------------------------------------------------------------------
    [localhost]$
  2. Add your enterprise beans: Write your enterprise beans and add them to the project under the src/main/java directory in the appropriate sub-directory for the bean’s package.
  3. Build the project: To build the project, run the mvn package command in the same directory as the pom.xml file. This will compile the Java classes and package the JAR file. The built JAR file is named -.jar and is placed in the target/ directory.

You now have a Maven project that builds and packages a JAR file. This project can contain enterprise beans and the JAR file can be deployed to an application server.

2.3. Create an EAR Project Containing an EJB Project

This task describes how to create a new enterprise archive (EAR) project in Red Hat JBoss Developer Studio that contains an EJB project.

Prerequisites

  • A server and server runtime for JBoss EAP have been set up.

Create an EAR Project Containing an EJB Project

  1. Open the New Java EE EAR Project Wizard.

    1. Navigate to the File menu, select New, then select Project.
    2. When the New Project wizard appears, select Java EE/Enterprise Application Project and click Next.

    Figure 2.4. New EAR Application Project Wizard

    New EAR Application Project Wizard
  2. Enter the following details:

    • Project name: The name of the project that appears in Red Hat JBoss Developer Studio, and also the default file name for the deployed EAR file.
    • Project location: The directory where the project files will be saved. The default is a directory in the current workspace.
    • Target runtime: This is the server runtime used for the project. This will need to be set to the same JBoss EAP runtime used by the server that you will be deploying to.
    • EAR version: This is the version of the Java Enterprise Edition specification that your project will comply with. Red Hat recommends using Java EE 7.
    • Configuration: This allows you to adjust the supported features in your project. Use the default configuration for your selected runtime.

      Click Next to continue.

  3. Add a new EJB module.

    New modules can be added from the Enterprise Application page of the wizard. To add a new EJB Project as a module follow the steps below:

    1. Click New Module, uncheck Create Default Modules checkbox, select the Enterprise Java Bean and click Next. The New EJB Project wizard appears.
    2. The New EJB Project wizard is the same as the wizard used to create new standalone EJB Projects and is described in Create an EJB Archive Project Using Red Hat JBoss Developer Studio.

      The minimum details required to create the project are:

      • Project name
      • Target runtime
      • EJB module version
      • Configuration

        All the other steps of the wizard are optional. Click Finish to complete creating the EJB Project.

        The newly created EJB project is listed in the Java EE module dependencies and the checkbox is checked.

  4. Optionally, add an application.xml deployment descriptor.

    Check the Generate application.xml deployment descriptor checkbox if one is required.

  5. Click Finish.

    Two new projects will appear: the EJB project and the EAR project.

  6. Add the build artifact to the server for deployment.

    Open the Add and Remove dialog by right-clicking in the Servers tab on the server you want to deploy the built artifact to in the server tab and then select Add and Remove.

    Select the EAR resource to deploy from the Available column and click the Add button. The resource will be moved to the Configured column. Click Finish to close the dialog.

Figure 2.5. Add and Remove Dialog

Add and Remove Dialog

You now have an Enterprise Application Project with a member EJB Project. This will build and deploy to the specified server as a single EAR deployment containing an EJB subdeployment.

2.4. Add a Deployment Descriptor to an EJB Project

An EJB deployment descriptor can be added to an EJB project that was created without one. To do this, follow the procedure below.

Prerequisites

  • You have a EJB project in Red Hat JBoss Developer Studio to which you want to add an EJB deployment descriptor.

Add a Deployment Descriptor to an EJB Project

  1. Open the project in Red Hat JBoss Developer Studio.
  2. Add a deployment descriptor.

    Right-click on the Deployment Descriptor folder in the project view and select Generate Deployment Descriptor tab.

    Figure 2.6. Adding a Deployment Descriptor

    Adding a Deployment Descriptor

    The new file, ejb-jar.xml, is created in ejbModule/META-INF/. Double-click on the Deployment Descriptor folder in the project view to open this file.