Can EJB2 and EJB3 coexist in the same application?

Does anyone know if it is possible to iteratively replace EJB2.1 beans with EJB3 beans in a Java EE application?

That is: at one time, remove the 2.1 bean code from the code and add the corresponding EJB3 bean that implements the same behavior without touching the rest of the code (+ be able to embed obsolete EJBs through annotations in the new EJB3).

I am not an expert on EJB specifications (and I only have experience with EJB3), but for me, EJB is just a component with this business interface, which is controlled by the application server. AFAIK EJB3 brought a lot of simplification to how to write a component (without artificial interfaces), and most of the time the xml descriptor can be excluded due to annotations, but the basics are the same. So it seems believable, it might work.

Is there any incompatibility between EJB2.1 and EJB3?

The core of the question is that the migration of EJB2.1 → EJB3 should be a stop-the-world / complete-rewrite operation, or this can be done by adding a new function and fixing errors in an outdated application (so it will mix EJB2.1 and EJB3 for some time in a running application).

EDIT:

  • I'm only interested in the beans session.
  • I am wondering if (and how) the search will work. AFAIK EJB2.1 requires something called a home interface in order to get a link to another EJB, but EJB3 does not have a home interface ...
+8
source share
3 answers

Coexistence of EJB 2.1 and EJB 3 beans

EJB2 and EJB3 beans can coexist in the same enterprise application (.ear), but cannot be in the same ejb jar file (module). Therefore, EJB3 beans must be in a different jar with EJB2 beans.

Call EJB 3 from EJB 2.1

The EJB3 bean does not have a home interface, while EJB 2.1 requires it. To make an EJB3 bean available with EJB2, you need to add a local home interface (or a remote home if a remote call is required) for the EJB3 bean.

Create a home interface:

public interface SystemTimeLocalHome extends EJBLocalHome { SystemTimeLocal create() throws CreateException; } 

Add a home interface to the EJB3 bean:

 @Stateless @Local(TimeServiceLocal.class) @LocalHome(TimeServiceLocalHome.class) public class TimeServiceBean implements TimeServiceLocal { public long getCurrentTimeMillis() { return System.currentTimeMillis(); } } 

Inside the EJB2 bean, the code for calling the EJB3 bean simply follows the EJB2 specifications: it searches for a link, calls the home interface to create a local interface, and then calls the method in the local interface.

 Context ctx = new InitialContext(); TimeServiceLocalHome home = (TimeServiceLocalHome)ctx.lookup("java:comp/env/" + ejbRefName); TimeServiceLocal timeService = home.create(); timeService.getCurrentTimeMillis(); 

Call EJB 2.1 from EJB 3

Dependency injection is used to enter references to EJB 2.1 components in an EJB3 bean. The difference between EJB3 bean injections is that it introduces the EJB2 home interface. Call the create() method on the EJB nested home interface to instantiate the bean class.

 @EJB BankingServiceHome bsHome; BankingService bs = bsHome.create(); bs.getBalance(...); 
+12
source share

Migration is possible, although it is not without hiccups.

Start by updating the descriptor to Java EE 5 schemas. This is typically a flag used by servers to determine whether annotation scans are required.

After updating the descriptor (but otherwise unchanged), you can start migrating your sessions and messages using Beans to Java EE 5 beans. When you convert a bean, you can also remove it from the deployment descriptor if you want.

After completing all of your other Beans, you can proceed to delete the Beans and convert the persistence to JPA entity classes. This is a non-trivial part of the exercise.

+2
source share

Of course. Take a look here at the beginning: http://www.coderanch.com/t/321218/EJB-JEE/java/EJB-call-EJB

0
source share

All Articles