EJB Deployment   «Prev  Next»

Lesson 1

Deploying Enterprise JavaBeans (EJBs) on Tomcat 10

Enterprise JavaBeans (EJBs) are server-side components that simplify the development of scalable, transactional enterprise applications. This lesson guides you through deploying an EJB 3.2 application on Apache Tomcat 10, which requires additional configuration since Tomcat is not a full Java EE server. You will create a deployment descriptor, package the EJB into a JAR file, and deploy it using Apache TomEE, a Tomcat distribution with built-in EJB support.

Steps to Deploy EJB 3.2 on Apache TomEE

Tomcat 10 does not natively support EJBs, so we use Apache TomEE, which extends Tomcat with Java EE features like EJB 3.2. Follow these steps to deploy a simple EJB application:
  1. Set Up Apache TomEE:
    • Download Apache TomEE from the official website.
    • Extract the archive and set the CATALINA_HOME environment variable to the TomEE directory.
    • Start TomEE using bin/startup.sh (Unix) or bin/startup.bat (Windows) and verify it runs by accessing http://localhost:8080.
  2. Develop Your EJB Application:
    • Create a stateless EJB. For example:
    • 
      @Stateless
      public class HelloWorldBean implements HelloWorld {
          public String sayHello(String name) {
              return "Hello, " + name + "!";
          }
      }
                  
    • Define the remote interface:
    • 
      public interface HelloWorld {
          String sayHello(String name);
      }
                  
  3. Package the EJB:
    • Create a deployment descriptor (ejb-jar.xml) in the META-INF directory:
    • 
      <ejb-jar xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="3.2">
          <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>
                  
    • Package the EJB classes and ejb-jar.xml into a .jar file using a build tool like Maven or manually:
    • 
      META-INF/
          ejb-jar.xml
      com/
          example/
              HelloWorldBean.class
              HelloWorld.class
                  
  4. Deploy the EJB:
    • Copy the .jar file to the TomEE apps directory.
    • Configure the EJB JNDI context in conf/tomee.xml:
    • 
      <Resource id="HelloWorldBean" type="javax.ejb.StatelessSessionBean">
          ejb-class=com.example.HelloWorldBean
      </Resource>
                  
    • Restart TomEE to load the EJB.
  5. Test the EJB:
    • Write a client to test the EJB:
    • 
      import javax.naming.Context;
      import javax.naming.InitialContext;
      public class HelloClient {
          public static void main(String[] args) throws Exception {
              Context context = new InitialContext();
              HelloWorld helloWorld = (HelloWorld) context.lookup("java:global/HelloWorldBean");
              System.out.println(helloWorld.sayHello("World"));
          }
      }
                  
    • Run the client and verify the output: Hello, World!.
  6. Monitor and Debug:
    • Check the TomEE logs in the logs directory for deployment issues.
    • Use TomEE’s web interface (http://localhost:8080/tomee) to monitor deployed applications.
Notes:

  • Apache TomEE is recommended over plain Tomcat for EJB deployments due to its built-in Java EE support.
  • Ensure all dependencies (e.g., javaee-api) are included in your project’s classpath, preferably managed by Maven or Gradle.

Hands-On Exercise: Deploy a BankAccount EJB

In this exercise, you will deploy a BankAccountBean that manages account balances and allows deposits and withdrawals. Follow these steps:
  1. Create a stateless EJB (BankAccountBean) with methods for deposit, withdraw, and getBalance.
  2. Define a remote interface (BankAccount) with the same methods.
  3. Package the EJB into a .jar file with an ejb-jar.xml descriptor, as shown above.
  4. Deploy the .jar to TomEE and test it using a client program (BankAccountClient) that performs sample transactions.
  5. Verify the results in the TomEE logs.
This exercise reinforces the deployment process and introduces you to building practical EJB applications. In the next lesson, you will explore advanced EJB features like transaction management.
[1] JAR file: A Java archive file that packages classes, resources, and metadata, similar to a ZIP file, used for deploying EJBs or other Java components.

SEMrush Software