EJBs in a WAR - Simplified packaging defined by EJB 3.1, Available in GlassFish v3
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
href="http://blogs.sun.com/MaheshKannan/entry/ejb_3_1_in_glassfish">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 href="http://blogs.sun.com/MaheshKannan/entry/installing_ejb_container_in_glassfish">here.
- Or using the "pkg" command which is described below
The "pkg" command shipped with GlassFish is platform-independent and
runs on all the supported platforms. You can use the standard "pkg-get"
command with OpenSolaris but that requires more options to be
specified. For simplicity, we'll use the "pkg" command bundled with
GlassFish as shown below:
cellpadding="2" cellspacing="2">
style="font-weight: bold;">
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:
http://wiki.updatecenter.java.net/Wiki.jsp?page=UsageMetricsUC2
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.
Installing: [pkg:/pkg@1.0.7,0-15.1269:20081008T211255Z,
pkg:/python2.4-minimal@2.4.4.0,0-15.1269:20081008T211307Z]
Initialization complete.
Software successfully installed. You may now re-run this command (pkg).
Install the EJB container as:
cellpadding="2" cellspacing="2">
DOWNLOAD PKGS FILES XFER (MB)<br>Completed 1/1 11/11 0.45/0.45PHASE ACTIONS<br>Install Phase 24/24
And verify as ...
cellpadding="2" cellspacing="2">
NAME (AUTHORITY) VERSION STATE UFIX<br>felix 1.2.2-0 installed ----<br>glassfish-amx 3.0-28.3 installed ----<br>glassfish-api 3.0-28.3 installed ----<br>glassfish-common 3.0-28.3 installed ----<br>glassfish-ejb 3.0-28.3 installed ----<br>glassfish-grizzly 1.8.6.2-0 installed ----<br>glassfish-gui 3.0-28.3 installed ----<br>glassfish-hk2 3.0-28.3 installed ----<br>glassfish-jca 3.0-28.3 installed ----<br>glassfish-jdbc 3.0-28.3 installed ----<br>glassfish-jdbc-gui 3.0-28.3 installed ----<br>glassfish-jdbc-management 3.0-28.3 installed ----<br>glassfish-jpa 3.0-28.3 installed ----<br>glassfish-jsf 1.2.10-1 installed u---<br>glassfish-jta 3.0-28.3 installed ----<br>glassfish-management 3.0-28.3 installed ----<br>glassfish-nucleus 3.0-28.3 installed ----<br>glassfish-registration 3.0-28.3 installed ----<br>glassfish-scripting 3.0-28.3 installed ----<br>glassfish-web 3.0-28.3 installed ----<br>glassfish-web-gui 3.0-28.3 installed ----<br>glassfish-web-management 3.0-28.3 installed ----<br>javadb 10.2.2.1-0 installed ----<br>pkg 1.0.7-15.1269 installed ----<br>pkg-java 1.0.7-15.1269 installed ----<br>python2.4-minimal 2.4.4.0-15.1269 installed ----
As shown above, "glassfish-ejb" module with version "3.0.28.3" 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
alt=""
src="http://blogs.sun.com/arungupta/resource/images/ejbwar-pojo-ejb.png">
- Declare a public method "sayHello" and add
@javax.ejb.Stateless annotation to mark it a stateless EJB as shown
alt=""
src="http://blogs.sun.com/arungupta/resource/images/ejbwar-pojo-ejb-stateless.png"> - Create a new Servlet by selecting the option as shown
alt=""
src="http://blogs.sun.com/arungupta/resource/images/ejbwar-new-servlet.png">
and specify the name as "EJBClient" in "client" package
alt=""
src="http://blogs.sun.com/arungupta/resource/images/ejbwar-new-servlet-name.png">
and click on "Finish". - In the generated Servlet, declare a dependency on the EJB
using @javax.ejb.EJB as shown
alt=""
src="http://blogs.sun.com/arungupta/resource/images/ejbwar-ejb-annotation.png"> - Invoke the EJB by uncommenting the code in "processRequest"
method and add "ejbClient.sayHell("Duke")" invocation as shown:
alt=""
src="http://blogs.sun.com/arungupta/resource/images/ejbwar-invoke-ejb.png">
If the application is pre-deployed then saving this file will
auto-deploy it as shown in
href="http://blogs.sun.com/arungupta/entry/screencast_27_simple_web_application">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:
alt=""
src="http://blogs.sun.com/arungupta/resource/images/ejbwar-invoke-ejb-results.png">
EJBs in a WAR - simple and easy to use :)
Download
href="https://glassfish.dev.java.net/downloads/v3-prelude.html">GlassFish
v3 Prelude and get started!
Technorati:
href="http://technorati.com/tag/glassfish">glassfish
v3
href="http://technorati.com/tag/ejb">ejb
href="http://technorati.com/tag/netbeans">netbeans
war
href="http://technorati.com/tag/ear">ear
- Login or register to post comments
- Printer-friendly version
- arungupta's blog
- 2686 reads






Comments
by felipegaucho - 2009-01-20 01:32
sorry, not "security descriptor" , persistence descriptor + ejb module + web gui, everything inside a war file ?by felipegaucho - 2009-01-20 01:30
Cool feature... I will modify my realm configuration EAR to a war file to see what happens :) I assume the security descriptor can also be packaged inside the war ?