blog community
Getting started with JEE 6 - EJB 3.1, JSF 2.0 and JAX-RS on GlassFish V3

JEE 6 is almost there. The final version is expected to be released at JavaOne this year, which is the first week of June. So this is a good moment to try out the new features. Let's first see what's new in JEE 6. After that I will show how to install the new APIs to try them out yourself. To me the most important changes in JEE 6 are the following:

-WebBeans
-EJB 3.1 (Lite)
-JSF 2.0
-JAX-RS
-Servlet 3.0
-JPA 2.0

JEE 6 also introduces the concept of profiles. A profile defines a subset of JEE to be more lightweight and focussed to a specific application type. The only profile at this moment is the web profile. THe web profile will probably be supported by containers such as Tomcat, enabling you to use JEE web technology including EJB Lite, without the need for a full blown app server.

Intstalling EJB 3.1 Lite, JAX-RS and JSF 2.0
The easiest way to get started with JEE 6 is using Glassfish V3. The next version of Glassfish will be released together with JEE 6. After downloading Glassfish you should start the update tool. This tool helps you to easily add features to Glassfish. At this moment, EJB 3.x is not fully supported by Glassfish. Only EJB Lite is supported, which is fine for now. EJB will be fully supported in the final release. Installing EJB Lite and JAX-RS is as easy as selecting them in the update tool and click the install button. Installing JSF 2.0 needs a little trick however. In the list you'll see JSF, but it only enables you to update to the next minor update which is 1.2.x. There is no option to upgrade to 2.0. You can solve this problem by first de-installing JSF. After that, you'll find a JSF 2.0 option in the list.


Usage in NetBeans 6.5
In Netbeans you should now create a Web project. That's right, you shouldn't create a Enterprise Project. If you do, you'll find that it can't be deployed to GlassFish V3. The whole application including EJB Lite Session Beans will be deployed as a WAR file.
Trying out JAX-RS and EJB 3.1 Lite
I started trying out features by creating a RESTful service using JAX-RS. I was hoping that EJB3 style injection would be fully supported by now, but it isn't. You still can't use @PersistenceContext to get hold of a EntityManager. There's still a lot of discussion going on to what degree this must be supported, but I'm really hoping it will make the final release. JAX-RS will be a lot more usable when injection of EJB Session Beans and EntityManagers works.

There is an easy work around however. Using the new type of JNDI names, Global Names, it's easy to find a EJB 3.1 Lite Session Bean. Session Beans do support injection, so all JPA code can be delegated to the Session Bean. The lookup of the Global Name works like this:

PhoneService s = (PhoneService) new InitialContext().lookup("java:global/MobileShopServices/PhoneService");

The name is structured as follows: java:global/ApplicationName/BeanName.
The Session Bean is created as a EJB 3.1 Lite Session bean.

   1: @Stateless 
   2: public class PhoneService {  
   3:    @PersistenceContext 
   4:    private EntityManager em; 
   5:     
   6:    public List<Phone> getPhones() { 
   7:       List<Phone> phones = em.createQuery("from Phone p").getResultList(); 
   8:       return phones; 
   9:    } 
  10: } 

As you can see, there is no interface. Interfaces are now optional for Session Beans. Not something you would always want, but I see it's use for Session Beans used as JSF backing beans (as used in Seam). Injection works well however.

So the only thing to left to do from the JAX-RS resource is call the Session Bean and return it's result as XML. The full method is implemented as follows:

   1: @GET 
   2: @Produces("application/xml") 
   3: public PhonesConverter getXml() { 
   4:    try { 
   5:       PhoneService s = (PhoneService) new InitialContext()
   6:         .lookup("java:global/MobileShopServices/PhoneService"); 
   7: return new PhonesConverter(s.getPhones()); 
   8: } catch (NamingException ex) { 
   9: Logger.getLogger(PhoneResource.class.getName()).log(Level.SEVERE, null, ex); 
  10: throw new RuntimeException(ex); 
  11:  } 
  12: } 

It uses a converter class to convert a list of JPA Entities to XML. The converter basically adds JAXB annotations to the classes.

   1: @XmlRootElement(name = "phones")
   2: public class PhonesConverter {
   3:     private List<PhoneConverter> phones;
   4:  
   5:     public PhonesConverter() {
   6:     }
   7:  
   8:     public PhonesConverter(List<Phone> phones) {
   9:         this.phones = new ArrayList<PhoneConverter>();
  10:         for(Phone phone : phones) {
  11:             this.phones.add(new PhoneConverter(phone));
  12:         }
  13:     }
  14:  
  15:     public List<PhoneConverter> getPhones() {
  16:         return phones;
  17:     }
  18:  
  19:     public void setPhones(List<PhoneConverter> phones) {
  20:         this.phones = phones;
  21:     }    
  22: }

That's all there is to it :-)
There's a lot more to tell about JAX-RS and EJB 3.1 (lite), but I'll get to that in a later post.

JSF 2.0
After JSF 2.0 is installed on GlassFish, you can start to use it immediately. The only thing you'll have to do to tell the container that you're doing JSF 2.0 is change the scheme location of the faces-config.xml.

   1: <faces-config xmlns="http://java.sun.com/xml/ns/javaee"
   2:     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3:     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   4:         http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
   5:     version="2.0"> 

After doing that, you can use @ManagedBean to create Managed Beans and you can use Facelets without any installation or configuration. There's a lot more to tell about JSF 2.0 but i'll go into more details in a later post.

There seems to be no easy way to install WebBeans in GlassFish yet. I'll try to get it working anyway and report on that too.


Posted 15-03-2009 17:24 by Paulb

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Enter code (required)
Powered by Community Server (Commercial Edition), by Telligent Systems