EJB 3.1 in GlassFish V3 TP2
In this blog, I will describe some of the EJB 3.1 features that are available in GlassFish V3.
For a full list of what is planned in EJB3.1 please refer to Ken's blog
Note: Before, you run any of the EJB 3.1 applications ensure that you follow the steps outlined in Installing EJB container in GlassFish V3
What EJB features are available in GlassFish V3 TP2
Only Stateless Session beans with local interfaces are supported. Stateful, Message driven and EJB 2.x entity beans are not supported. Remote interfaces and Remote business interfaces for any of the bean type are not supported yet. Timer Service is supported, but a little bit of configuration is needed to enable it. We will be blogging about how to enable TimerService in GlassFish V3 shortly.
Support for other types of beans will be available soon.
Note: TP2 gives you an early look at GlassFish v3. It is not a full, feature-complete application server and is not suitable for production deployments. It is suitable for experimentation and exploration. Experiment, take a look at the new approach being taken in GlassFish v3, and then let us know what you think
Using GlassFish V3 server
To run the sample application provided, follow the steps mentioned in How to run the hello.war
Quick start guide provides more details on how to use the GlassFish V3 server.
Optional Local Business Interfaces
Recall that even though EJB 3.1 simplified the EJB development by introducing the business interfaces, the bean developer must still write at least one interface. For most of the applications this is again an overhead. In EJB 3.1 an EJB need not implement any interface as long as it contains one of the component defining annotations (or the XML equivalent). So a simple EJB 3.1 HelloBean looks like this:
@Stateless
public class HelloBean {
public String sayHello() {
return "Hello, World!!";
}
}
Note: The client still has to perform a JNDI lookup or inject a reference of the bean. More specifically, it cannot use the new operator to construct the bean.
So a Servlet that use the HelloBean will be coded like the following: public class HelloServlet { @EJB private HelloBean hello; .... hello.sayHello(); .... }
Simplified packaging
JavaEE 5 greatly improved the ease of use by providing a bunch of annotations that that obviated the need for XMLs. However, it still required that Servlets/JSPs be packaged in a .war file and EJBs be packaged (in possibly multiple) .jar files. These files must further be packaged inside a .ear file. For simple web applications that wanted to use EJBs, the above packaging restrictions was a bit of an overkill
Another cool feature that is introduced in EJB 3.1 is the simplification of packaging requirements of EJBs. Now, EJB classes can be packaged inside the .war file itself!! The classes must reside under WEB-INF/classes.
Because of the above two features, the structure of our hello.war looks like this.
META-INF/ META-INF/MANIFEST.MF WEB-INF/ WEB-INF/classes/ WEB-INF/classes/com/ WEB-INF/classes/com/sun/ WEB-INF/classes/com/sun/v3/ WEB-INF/classes/com/sun/v3/demo/ WEB-INF/classes/com/sun/v3/demo/HelloEJB.class WEB-INF/classes/com/sun/v3/demo/HelloServlet.class WEB-INF/web.xml index.jsp
How to run hello.war
Download the attachments provided. HelloEJB31.war contains the application that can be deployed. HelloEJB31.zip contains the sources.
- Start the server by running: <install_dir>/asadmin start-domain
- Deploy the application by running: <install_dir>/asadmin deploy hello.war
- Open a browser and go to: http://localhost:8080/HelloEJB31/HelloServlet
See http://blogs.sun.com/marina/entry/enabling_ejb_timers_in_glassfish for information on how to enable TimerService in GlassFish V3
Posted by Marina on May 06, 2008 at 10:06 PM PDT #
Hi Mahesh, great article, let me ask you, did you try to test an asynchronous method ?
Regards
Wagner
Posted by Wagner Santos on May 23, 2008 at 12:10 AM PDT #