Reference EJB in another EJB of different applications
This is an example of how to create a reference of an EJB in another EJB of different applications. Enterprise beans are Java EE components that implement Enterprise JavaBeans (EJB) technology. Enterprise beans run in the EJB container, a runtime environment within the Application Server (see Container Types). Although transparent to the application developer, the EJB container provides system-level services such as transactions and security to its enterprise beans. These services enable you to quickly build and deploy enterprise beans, which form the core of transactional Java EE applications.
Here, we have created two EJB implementations, the CalculatorService
and the EmployeeService
that has a reference of CalculatorService
that belongs to a different application, as described below:
The CalculatorService EJB implementation class
The CalculatorService
is an EJB implementation class that is a stateless session bean that implements four methods. It is annotated with the javax.ejb.Stateless
annotation. The class must have a default public constructor. The CalculatorService
has a local interface that defines the bean’s business and life cycle methods, decorated with the @Local
annotation. It also has a remote interface decorated with the @Remote
annotation, that can run on a different machine and a different Java virtual machine (JVM) than the CalculatorService
.
The CalculatorService
EJB implementation class:
package com.javacodegeeks.snippets.enterprise; import javax.ejb.Stateless; @Stateless public class CalculatorService implements CalculatorServiceLocal, CalculatorServiceRemote { public CalculatorService() { } @Override public long add(long i, long j) { return (i + j); } @Override public double divide(long i, long j) { return ((double)i / j); } @Override public long multiply(long i, long j) { return (i * j); } @Override public long subtract(long i, long j) { return (i - j); } }
The Calculator service EJB local interface (suitable for in VM communication):
package com.javacodegeeks.snippets.enterprise; import javax.ejb.Local; @Local public interface CalculatorServiceLocal { public long add(long i, long j); public long subtract(long i, long j); public long multiply(long i, long j); public double divide(long i, long j); }
The Calculator service EJB remote interface (suitable for intra VM communication):
package com.javacodegeeks.snippets.enterprise; import javax.ejb.Remote; @Remote public interface CalculatorServiceRemote { public long add(long i, long j); public long subtract(long i, long j); public long multiply(long i, long j); public double divide(long i, long j); }
The application.xml file describing the modules in the Calculator application .ear archive:
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" version="1.4"> <display-name>Calculator Application</display-name> <module> <ejb>calculatorServiceEJB3.jar</ejb> </module> </application>
The structure of the .ear archive:
calculatorApp.ear | |_calculatorServiceEJB3.jar | |_com | | |_javacodegeeks | | |_snippets | | |_enterprise | | |_CalculatorService.class | | |_CalculatorServiceLocal.class | | |_CalculatorServiceRemote.class | |_META-INF | |_META-INF |_application.xml
The Data Transfer (Domain) object
Class Employee
is an entity, that is a lightweight persistence domain object. Typically an entity represents a table in a relational database, and each entity instance corresponds to a row in that table. The persistent state of an entity is represented either through persistent fields or persistent properties. These fields or properties use object/relational mapping annotations to map the entities and entity relationships to the relational data in the underlying data store. The class is annotated with the javax.persistence.Entity
annotation, it has a public or protected, no-argument constructor and it implements the Serializable interface.
package com.javacodegeeks.snippets.enterprise; import java.io.Serializable; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Employee implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private String name; private String surname; private String title; private Double salary; private Date created; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", surname=" + surname + ", title=" + title + ", salary="+salary+ ", created=" + created+"]"; } }
The EmployeeService EJB implementation class
The EmployeeService
EJB implementation class is also an EJB implementation class that is a stateless session bean and has a reference of the CalculatorService
EJB. It uses the javax.persistence.EntityManager
interface, that creates and removes persistent entity instances, finds entities by the entity’s primary key, and allows queries to be run on entities. The EntityManager
is annotated with the javax.persistence.PersistenceContext
annotation to express a dependency on the container-managed EntityManager
and its associated persistence context, using the name of the persistence unit as defined in the persistence.xml
file. The SessionContext
interface provides access to the runtime session context that the container provides for a session bean instance. It is annotated with the @Resource
annotation, so that the container will inject an instance of the requested resource into the SessionContext
when the SessionContext
is initialized. The EmployeeService
also has a local and a remote interface.
Using the lookup(Name name)
API method of Context
the EJB retrieves the CalculatorService
, using its local interface. Then, its methods are used in the EmployeeService
, so that an Employee
object is created. Through the EntityManager
API the Employee
objects are written to and retrieved from the database.
package com.javacodegeeks.snippets.enterprise; import java.util.Collection; import java.util.Date; import javax.annotation.Resource; import javax.ejb.SessionContext; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; @Stateless public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote { @PersistenceContext(unitName = "TutorialPU") EntityManager entityManager; @Resource SessionContext context; public EmployeeService() { } public Employee createEmployee(String name, String surname, String title, double salary) { // We inject a reference of an EJB declared at another application CalculatorServiceLocal calculatorService = (CalculatorServiceLocal) context.lookup("calculatorApp/CalculatorService/local"); long tax = 100; double finalSalary = calculatorService.subtract((long)salary, tax); Employee employee = new Employee(); employee.setName(name); employee.setSurname(surname); employee.setTitle(title); employee.setSalary(finalSalary); employee.setCreated(new Date()); entityManager.persist(employee); return employee; } public void removeEmployee(long id) { Employee employee = findEmployee(id); if (employee != null) { entityManager.remove(employee); } } public Employee promoteEmployee(long id, String newTitle, double newSalary) { Employee employee = entityManager.find(Employee.class, id); if (employee != null) { // We inject a reference of an EJB declared at another application CalculatorServiceLocal calculatorService = (CalculatorServiceLocal) context.lookup("calculatorApp/CalculatorService/local"); long tax = 100; double finalSalary = calculatorService.subtract((long)newSalary, tax); employee.setTitle(newTitle); employee.setSalary(finalSalary); } return employee; } public Employee findEmployee(long id) { return entityManager.find(Employee.class, id); } public Collection<Employee> findAllEmployees() { Query query = entityManager.createQuery("SELECT e FROM Employee e"); return (Collection<Employee>) query.getResultList(); } }
The Employee service EJB local interface (suitable for in VM communication)
package com.javacodegeeks.snippets.enterprise; import java.util.Collection; import javax.ejb.Local; @Local public interface EmployeeServiceLocal { public Employee createEmployee(String name, String surname, String title, double salary); public void removeEmployee(long id); public Employee promoteEmployee(long id, String newTitle, double newSalary); public Employee findEmployee(long id); public Collection<Employee> findAllEmployees(); }
The Employee service EJB remote interface (suitable for intra VM communication)
package com.javacodegeeks.snippets.enterprise; import java.util.Collection; import javax.ejb.Remote; @Remote public interface EmployeeServiceRemote { public Employee createEmployee(String name, String surname, String title, double salary); public void removeEmployee(long id); public Employee promoteEmployee(long id, String newTitle, double newSalary); public Employee findEmployee(long id); public Collection<Employee> findAllEmployees(); }
The persistence.xml file driving the JPA framework for the Employee application
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="TutorialPU" > <jta-data-source>java:/DefaultDS</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> </properties> <!-- <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> <property name="hibernate.connection.username" value="sa"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.connection.url" value="jdbc:hsqldb:data/tutorial"/> </properties> --> </persistence-unit> </persistence>
The application.xml file describing the modules in the Employee application .ear archive
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" version="1.4"> <display-name>ExampleEJB3</display-name> <module> <java>exampleEJB3-persistence.jar</java> </module> <module> <ejb>exampleEJB3.jar</ejb> </module> </application>
The structure of the Employee application .ear archive
exampleEJB3.ear | |_exampleEJB3-persistence.jar | |_com | | |_javacodegeeks | | |_snippets | | |_enterprise | | |_Employee.class | |_META-INF | |_persistence.xml | |_exampleEJB3.jar | |_com | | |_javacodegeeks | | |_snippets | | |_enterprise | | |_EmployeeService.class | | |_EmployeeServiceLocal.class | | |_EmployeeServiceRemote.class | |_META-INF | |_META-INF |_application.xml
A simple client example
In EmployeeServiceClient
we connect to JBoss naming service running on local host and on default port 1099. We use the Context to set the configuration for the JBoss server, such as Context.INITIAL_CONTEXT_FACTORY
, Context.PROVIDER_URL
and Context.URL_PKG_PREFIXES
. We get the EmployeeService
EJB using the lookup(Name name)
method of Context to invoke its methods.
package com.javacodegeeks.snippets.enterprise; import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; public class EmployeeServiceClient { public static void main(String[] a) throws Exception { /* * Connecting to JBoss naming service running on local host and on * default port 1099 the environment that should be created is like the * one shown below : */ Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); env.put(Context.PROVIDER_URL, "jnp://localhost:1099"); env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); Context ctx = new InitialContext(env); // We get a reference of the remote EJB interface to invoke its business methods EmployeeServiceRemote employeeService = (EmployeeServiceRemote) ctx.lookup("exampleEJB3/EmployeeService/remote"); Employee employee = employeeService.createEmployee("Byron", "Kiourtzoglou", "Master Software Engineer", 2000d); long employeeId = employee.getId(); System.out.println(employeeService.findEmployee(employeeId)); employeeService.promoteEmployee(employeeId, "Principal Software Engineer", 3000d); System.out.println(employeeService.findEmployee(employeeId)); } }
Output:
Employee [id=1, name=Byron, surname=Kiourtzoglou, title=Master Software Engineer, salary=1900.0, created=2011-12-05 12:52:56.033]
Employee [id=1, name=Byron, surname=Kiourtzoglou, title=Principal Software Engineer, salary=2900.0, created=2011-12-05 12:52:56.033]
This was an example of how to create a reference of an EJB in another EJB of different applications.