EJBs in a WAR - Simplified packaging defined by EJB 3.1, Available in GlassFish v3
By arungupta on Jan 18, 2009
The EJB 3.1 specification says:
An enterprise bean class with a component-defining annotation defines an enterprise bean component when packaged within the WEB-INF/classes directory or in a .jar file within WEB-INF/lib.
In simple English it means, an EJB can be a POJO annotated with EJB annotations (such as @javax.ejb.Stateless) and bundled within WEB-INF/classes inside a WAR.
This feature is available in GlassFish v3 for some time now.
Imagine the ramifications, you now have Container Managed Persistence, Transacations, Security, and all other standard benefits of EJB - only this time in a WAR file.
The default configuration of GlassFish v3 Prelude does not include an EJB container. Lets first install it!
The EJB container in GlassFish v3 Prelude can be installed in couple of ways:
- Using Update Center as described here.
- Or using the "pkg" command which is described below
The software needed for this command (pkg) is not installed.
When this tool interacts with package repositories, some system information
such as your system's IP address and operating system type and version
is sent to the repository server. For more information please see:
Once installation is complete you may re-run this command.
Would you like to install this software now (y/n): y
Install image: /export/home/arun/glassfishv3-prelude/bin/..
Installing pkg packages.
Software successfully installed. You may now re-run this command (pkg).
Install the EJB container as:
|arun@opensolaris:~/glassfishv3-prelude/bin$ ./pkg install glassfish-ejb
DOWNLOAD PKGS FILES XFER (MB)
And verify as ...
|arun@opensolaris:~/glassfishv3-prelude/bin$ ./pkg list
NAME (AUTHORITY) VERSION STATE UFIX
As shown above, "glassfish-ejb" module with version "184.108.40.206" is now installed.
Now your GlassFish v3 Prelude is ready to serve EJBs!
Next, lets create a simple web application and package EJB there. Using the NetBeans IDE, create a template Web application. Lets say the project is named "ReallySimpleEJB".
- Create a POJO, choose the class name as "HelloEJB" and
package as "server" as shown
- Declare a public method "sayHello" and add
@javax.ejb.Stateless annotation to mark it a stateless EJB as shown
- Create a new Servlet by selecting the option as shown
and specify the name as "EJBClient" in "client" package
and click on "Finish".
- In the generated Servlet, declare a dependency on the EJB
using @javax.ejb.EJB as shown
- Invoke the EJB by uncommenting the code in "processRequest"
method and add "ejbClient.sayHell("Duke")" invocation as shown:
If the application is pre-deployed then saving this file will auto-deploy it as shown in screencast #27. Otherwise right-click on the project and select "Deploy".
And finally invoking the servlet at "http://localhost:8080/ReallySimpleEJB/EJBClient" shows the following output:
EJBs in a WAR - simple and easy to use :)
Download GlassFish v3 Prelude and get started!
Technorati: glassfish v3 ejb netbeans war ear