does anybody know if it is possible to iteratively replace EJB2.1 beans with EJB3 beans in Java EE application?
That is: at one time remove one 2.1 bean from the co
EJB2 and EJB3 beans can coexist in one enterprise application (.ear), but can’t reside on the same ejb jar file (module). Hence, the EJB3 beans must reside in a different jar to the EJB2 beans.
EJB3 bean hasn't home interface while EJB 2.1 requires it. In order to make EJB3 bean being able to access from EJB2, you need to add local home interface (or remote home if remote call is required) to EJB3 bean.
Create home interface:
public interface SystemTimeLocalHome extends EJBLocalHome {
SystemTimeLocal create() throws CreateException;
}
Add home interface to EJB3 bean:
@Stateless
@Local(TimeServiceLocal.class)
@LocalHome(TimeServiceLocalHome.class)
public class TimeServiceBean implements TimeServiceLocal {
public long getCurrentTimeMillis() {
return System.currentTimeMillis();
}
}
Inside EJB2 bean the code to invoke the EJB3 bean just follows EJB2 specification: looks up the reference, call the home interface to create local interface, then invoke method on the local interface.
Context ctx = new InitialContext();
TimeServiceLocalHome home = (TimeServiceLocalHome)ctx.lookup("java:comp/env/" + ejbRefName);
TimeServiceLocal timeService = home.create();
timeService.getCurrentTimeMillis();
Dependency injection is utilized to inject the EJB 2.1 component references into EJB3 bean. The different to injecting EJB3 bean is that it's the home interface of EJB2 being injected. Call create() method on injected EJB home interface to instantiate the bean class.
@EJB BankingServiceHome bsHome;
BankingService bs = bsHome.create();
bs.getBalance(...);