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:
- 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
.
- 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);
}
- 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
- Deploy the EJB:
- Test the EJB:
- 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:
- Create a stateless EJB (
BankAccountBean
) with methods for deposit
, withdraw
, and getBalance
.
- Define a remote interface (
BankAccount
) with the same methods.
- Package the EJB into a
.jar
file with an ejb-jar.xml
descriptor, as shown above.
- Deploy the
.jar
to TomEE and test it using a client program (BankAccountClient
) that performs sample transactions.
- 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.
