EJB Deployment   «Prev  Next»

Lesson 1

Deploying EJBs

The first lessons in this module describe the initial steps in deploying an EJB in a container. In these lessons you will create the XML deployment descriptor that describes the bean. Then you will package it in a jar file[1] together with the interfaces and classes for the bean. Once the beans are packaged, you will deploy them on the EJB platform. This process involves building the stubs, skeletons, and the Home and Remote objects, followed by the loading of the bean.

Steps to deploy Enterprise JavaBeans 3.0 on Tomcat 10

Deploying Enterprise JavaBeans (EJB) 3.0 on Apache Tomcat 10 requires additional setup because Tomcat is not a Java EE-compliant application server and does not natively support EJBs. You need to use a library or framework, such as OpenEJB (or its modern version, Apache TomEE), to enable EJB support. Below are the steps to deploy EJBs on Tomcat 10:
1. Install Tomcat 10:
  1. Download and install Tomcat 10 if not already installed.
  2. Extract the archive and configure environment variables (`CATALINA_HOME`).
  3. Start Tomcat to ensure it is functioning correctly.

2. Install Apache OpenEJB (or TomEE)
  1. Option A: Use OpenEJB for EJB support in Tomcat:
    • Download OpenEJB from the OpenEJB website.
    • Extract OpenEJB and integrate it with Tomcat:
      • Copy the required JAR files from OpenEJB to Tomcat's `lib` directory.
  2. Option B: Use Apache TomEE (a Tomcat distribution with EJB support):
    • Download Apache TomEE, which is a Tomcat build with EJB and other Java EE features. URL: TomEE Downloads
    • Replace your existing Tomcat installation with TomEE if preferred.


3. Prepare Your EJB Application:
  1. Develop Your EJB:
    • Create your EJB 3.0 application. For example:
    •           @Stateless
                public class HelloWorldBean implements HelloWorld {
                  public String sayHello(String name) {
                    return "Hello, " + name + "!";
                  }
                }
              
  2. Package the Application as a JAR or EAR:
    • EJB modules are typically packaged in a `.jar` file. If your application includes web components, package it as an `.ear` file.
    •           META-INF/
                  persistence.xml (optional)
                  ejb-jar.xml
                com/
                  example/
                    HelloWorldBean.class
                    HelloWorld.class
              
  3. Generate the Deployment Descriptor (`ejb-jar.xml`):
    • Example `ejb-jar.xml`:
    •           <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.0">
                  <enterprise-beans>
                    <session>
                      <ejb-name>HelloWorldBean</ejb-name>
                      <ejb-class>com.example.HelloWorldBean</ejb-class>
                      <session-type>Stateless</session-type>
                      <transaction-type>Container</transaction-type>
                    </session>
                  </enterprise-beans>
                </ejb-jar>
              


4. Deploy the EJB Application
  1. Copy the `.jar` or `.ear` file to the Tomcat `webapps` or TomEE `apps` directory.
  2. For OpenEJB:
    • Modify the `conf/openejb.xml` file to map the EJB JNDI context to your application:
                <deployment>
                  <ejb>
                    <ejb-name>HelloWorldBean</ejb-name>
                    <jndi-name>HelloWorldBean</jndi-name>
                  </ejb>
                </deployment>
              

5. Configure and Run the Application
  1. Start Tomcat/TomEE and Test the EJB Deployment:
    • Use the `startup.sh` or `startup.bat` script in the `bin` directory.
    • Test the EJB deployment:
                Context context = new InitialContext();
                HelloWorld helloWorld = (HelloWorld) context.lookup("java:global/HelloWorldBean");
                System.out.println(helloWorld.sayHello("World"));
              

6. Optional: Debugging and Logs
  • Check the Tomcat logs in the `logs` directory for any issues during deployment.

Notes
  • If you frequently deploy EJBs, consider using Apache TomEE instead of plain Tomcat.
  • Ensure your classpath includes all required dependencies for EJBs.

Working hands-on

During the exercises in this module you will download, install, and configure the J2EE reference implementation. Once that is complete, you will then deploy the Hello bean and execute the HelloClient program. The final lesson includes the first project exercise, you will write a Customer client that accesses a BankAccount bean that you deploy. In the next lesson, you will review the principles of deployment and be introduced to the J2EE Reference Implementation. Deployment descriptor: Information that describes both the structure of an Enterprise Java Bean, and the information required by it at runtime.
[1] Jar file:A file created with the Java jar program which is similar to tar on the Unix platform.

SEMrush Software