Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Moving On: Migrating from EJB 2.1 to EJB 3.0 : Page 2

Find out how much you can simplify your EJB code by working through these examples, which show the process for migrating EJBs to version 3.0.


advertisement
Migrating Session Beans
The example EJB 2.1 session bean class, called BookCatalogBean, specifies an ejbCreate method, a business method called getTitle(), and the callback methods shown below:

// BookCatalogBean.java import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class BookCatalogBean implements SessionBean { private SessionContext ctx; public String getEdition(String title) { if(title.equals("Java & XML")) return new String("2nd edition"); if(title.equals("Java and XSLT")) return new String("1st edition"); } public void ejbCreate(){} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext ctx) {this.ctx=ctx;} }

If you don't specify an interface (local or remote) in the bean class, the EJB server automatically generates a local business interface by default.
In EJB 3.0 session beans, you use metadata annotations to specify the bean type— Stateful or Stateless—with the annotations @Stateful and @Stateless respectively. You replace component and home interfaces in a session bean with a business interface. The business interface is a POJI and you can specify it as either local or remote using the annotations @Local and @Remote. A session bean may implement both local and remote interfaces.

If you don't specify an interface (local or remote) in the bean class, the EJB server automatically generates a local business interface by default. You may also specify the interface class in the @Local and @Remote annotations.

The EJB 3.0 session bean shown below is a POJO named BookCatalogBean.java that is a migrated version of the EJB 2.1 stateless session bean shown earlier. Note that it uses the @Stateless annotation, implements a local business interface, and specifies the local interface class name in the @Local annotation.

// BookCatalogBean.java EJB 3.0 Session Bean @Stateless @Local ({BookCatalogLocal.java}) public class BookCatalogBean implements BookCatalogLocal { public String getEdition(String title) { if(title.equals("Java & XML")) return new String("2nd edition"); if(title.equals("Java and XSLT")) return new String("1st edition"); } }

Also note that the preceding EJB 3.0 bean class replaces the component and home interfaces of the EJB 2.1 version with a local business interface (POJI), annotated with the @Local annotation.

Migrating EJB Session Bean Clients
A client for an EJB 2.1 session bean obtains a session bean object using a JNDI name. The client shown below obtains a local home object using the JNDI name BookCatalogLocalHome, and then calls the create() method. Subsequently, the client outputs the edition value for a specified title with the getEdition(String) business method.



import javax.naming.InitialContext; public class BookCatalogClient { public static void main(String[] argv) { try{ InitialContext ctx=new InitialContext(); Object objref=ctx.lookup( "BookCatalogLocalHome"); BookCatalogLocalHome catalogLocalHome = (BookCatalogLocalHome)objref; BookCatalogLocal catalogLocal = (BookCatalogLocal) catalogLocalHome. create(); String title="Java and XML"; String edition = catalogLocal.getEdition(title); System.out.println("Edition for Title: " + title + " " + edition); } catch(Exception e){} }

}

In EJB 3.0 you obtain a reference to a session bean object via dependency injection, which you implement with the @Inject annotation, the @Resource annotation, or the @EJB annotation. The EJB 3.0 session bean client class shown below injects the BookCatalogBean class with the @Inject annotation. It still obtains the edition value for a title using the getEdition(String) business method.

public class BookCatalogClient { @Inject BookCatalogBean; BookCatalogBean catalogBean; String title="Java and XML"; String edition=catalogBean.getEdition(edition); System.out.println("Edition for Title: " + title + " " + edition); }



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap