X

EJBs in a WAR - Simplified packaging defined by EJB 3.1, Available in GlassFish v3

Guest Author


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:



style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">arun@opensolaris:~/glassfishv3-prelude/bin$ style="font-weight: bold;">./pkg 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:



style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">arun@opensolaris:~/glassfishv3-prelude/bin$ style="font-weight: bold;">./pkg install glassfish-ejb
DOWNLOAD                                    PKGS       FILES     XFER (MB)
Completed                                    1/1       11/11     0.45/0.45
PHASE                                        ACTIONS
Install Phase                                  24/24




And verify as ...



style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">arun@opensolaris:~/glassfishv3-prelude/bin$ style="font-weight: bold;">./pkg list
NAME (AUTHORITY)                              VERSION         STATE      UFIX
felix                                         1.2.2-0         installed  ----
glassfish-amx                                 3.0-28.3        installed  ----
glassfish-api                                 3.0-28.3        installed  ----
glassfish-common                              3.0-28.3        installed  ----
glassfish-ejb                                 3.0-28.3        installed  ----
glassfish-grizzly                             1.8.6.2-0       installed  ----
glassfish-gui                                 3.0-28.3        installed  ----
glassfish-hk2                                 3.0-28.3        installed  ----
glassfish-jca                                 3.0-28.3        installed  ----
glassfish-jdbc                                3.0-28.3        installed  ----
glassfish-jdbc-gui                            3.0-28.3        installed  ----
glassfish-jdbc-management                     3.0-28.3        installed  ----
glassfish-jpa                                 3.0-28.3        installed  ----
glassfish-jsf                                 1.2.10-1        installed  u---
glassfish-jta                                 3.0-28.3        installed  ----
glassfish-management                          3.0-28.3        installed  ----
glassfish-nucleus                             3.0-28.3        installed  ----
glassfish-registration                        3.0-28.3        installed  ----
glassfish-scripting                           3.0-28.3        installed  ----
glassfish-web                                 3.0-28.3        installed  ----
glassfish-web-gui                             3.0-28.3        installed  ----
glassfish-web-management                      3.0-28.3        installed  ----
javadb                                        10.2.2.1-0      installed  ----
pkg                                           1.0.7-15.1269   installed  ----
pkg-java                                      1.0.7-15.1269   installed  ----
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".



  1. Create a POJO, choose the class name as "HelloEJB" and
    package as "server" as shown

    alt=""
    src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/8f031d1e77045aceafe479bd4469761a/ejbwar_pojo_ejb.png">
  2. Declare a public method "sayHello" and add
    @javax.ejb.Stateless annotation to mark it a stateless EJB as shown

    alt=""
    src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/5b86b27bb04511f81a80a447d548d462/ejbwar_pojo_ejb_stateless.png">
  3. Create a new Servlet by selecting the option as shown

    alt=""
    src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/1fb30991a955029297570361fe930cd3/ejbwar_new_servlet.png">


    and specify the name as "EJBClient" in "client" package

    alt=""
    src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/25f7af7694193656d62dd3427b4e8325/ejbwar_new_servlet_name.png">


    and click on "Finish".
  4. In the generated Servlet, declare a dependency on the EJB
    using @javax.ejb.EJB as shown

    alt=""
    src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/7a483486cb4883e4b61f45f2829c3ad2/ejbwar_ejb_annotation.png">
  5. Invoke the EJB by uncommenting the code in "processRequest"
    method and add "ejbClient.sayHell("Duke")" invocation as shown:

    alt=""
    src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/a9a21ea1583ed0177631ac7f7ab08d69/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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/d679b5397951040744a7c53a81cc7425/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

Join the discussion

Comments ( 10 )
  • Michael Johann Monday, January 19, 2009

    Arun,

    this is cool stuff. Thank you for writing it down.

    Greetings

    Michael


  • Jacek Thursday, January 22, 2009

    What, no XML configuration? It can't be for the enterprise then. \*grin\*

    Just kidding, great stuff. The EJB 3.1 & Glassfish team is really on to something here.


  • Wholesale Monday, February 2, 2009

    Good page!


  • Cay Horstmann's Blog Monday, February 2, 2009
    [Trackback] Java EE 6 makes it pretty straightforward to crunch out a basic web + database application. This semester, my software engineering class is building web apps, and I decided it is simpler to have them use JSF + JPA in Glassfish v3 rather than some techn...
  • Cay Horstmann's Blog Tuesday, February 10, 2009
    [Trackback] Java EE 6 makes it pretty straightforward to crunch out a basic web + database application. This semester, my software engineering class is building web apps, and I decided it is simpler to have them use JSF + JPA in Glassfish v3 rather than some techn...
  • sinema izle Tuesday, March 10, 2009

    You were given good information. thank you


  • Warhammer Online Gold Wednesday, March 11, 2009

    What, no XML configuration? It can't be for the enterprise then. \*grin\*

    Just kidding, great stuff. The EJB 3.1 & Glassfish team is really on to something here.


  • Arun Gupta's Blog Tuesday, May 19, 2009
    [Trackback] EJB 3.1 (JSR 318) and Servlet 3.0 (JSR 315) are the two new JSRs in Java EE 6 (JSR 316). The EJB 3.1 specification provides multiple new features such as WAR packaging, Optional Local Business Interfaces, EJB.lite, Portable Global...
  • Arun Gupta's Blog Wednesday, May 20, 2009
    [Trackback] EJB 3.1 (JSR 318) and Servlet 3.0 (JSR 315) are the two new JSRs in Java EE 6 (JSR 316). The EJB 3.1 specification provides multiple new features such as WAR packaging, Optional Local Business Interfaces, EJB.lite, Portable Global...
  • Chester Chen Thursday, July 16, 2009

    Hi, Thanks for the example.

    I am a newbie to the glassfish v3 prelude, and trying to see if I can follow your example in my own development env.

    The code itself is pretty simple, but I am having trouble to figure out what are the jar files needed to make the code to compile.

    In prev. glassfish version (v2.1), I can include javaee.jar and appsver-rt.jar make my both server and client code happy.

    Now with v3 prelude, it seems to me that appsver-rt.jar is gone.

    What should I use to make the project build. BTW, I don't use NetBean.

    If you can publish the simple ant build scripts, that will help to clarify my questions.

    thanks

    Chester


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.