Thursday Jul 30, 2009

TOTD #87: How to fix the error undefined method `new' for "Rack::Lock":String caused by Warbler/JRuby-Rack ?

If you are using Warbler to create a WAR file of your application and deploying on GlassFish or any other Servlet container, then you are likely seeing the following error during deployment:

[/session]unable to create shared application instance
org.jruby.rack.RackInitializationException: undefined method `new' for "Rack::Lock":String
        from /Users/arungupta/tools/glassfish/v2.1/glassfish/domains/domain1/applications/j2ee-modules/session/WEB-INF/gems/gems/actionpack-2.3.2/lib/
action_controller/middleware_stack.rb:116:in `inject'
        from /Users/arungupta/tools/glassfish/v2.1/glassfish/domains/domain1/applications/j2ee-modules/session/WEB-INF/gems/gems/actionpack-2.3.2/lib/
action_controller/middleware_stack.rb:116:in `build'
        from /Users/arungupta/tools/glassfish/v2.1/glassfish/domains/domain1/applications/j2ee-modules/session/WEB-INF/gems/gems/actionpack-2.3.2/lib/
action_controller/dispatcher.rb:82:in `initialize'

. . .

This is a known issue as reported at JRUBY-3789 and JRUBY_RACK-18.

As the bug report indicates, this is actually an issue with jruby-rack-0.9.4 and is fixed in jruby-rack-0.9.5. The 3-step workaround is described here and explained below for convenience:
  1. Do "warble war:clean" to clean up the .war file and staging area. This basically removes previous version of jruby-rack.jar.
  2. Download the latest jruby-rack-0.9.5 snapshot (complete list) and copy in the "lib" directory of your application.
  3. If "config/warble.rb" does not exist then generate it using "jruby -S config warble". Edit "config/warble.rb" such that it looks like:

      # Additional Java .jar files to include. Note that if .jar files are placed
      # in lib (and not otherwise excluded) then they need not be mentioned here.
      # JRuby and JRuby-Rack are pre-loaded in this list. Be sure to include your
      # own versions if you directly set the value
      # config.java_libs += FileList["lib/java/\*.jar"]
      config.java_libs.delete_if {|f| f =~ /jruby-rack/ }
      config.java_libs += FileList["lib/jruby-rack\*.jar"]

    This will pack jruby-rack-0.9.5 snapshot instead of the one bundled with Warbler.

    Now warbler 1.0.0 bundles "jruby-complete-1.3.0RC1.jar". Optionally, you can also download the latest jruby-complete (jruby-complete-1.3.1.jar as of this writing) and copy in the "lib" directory of your application. In that case, modify the above fragment to:

      # Additional Java .jar files to include. Note that if .jar files are placed
      # in lib (and not otherwise excluded) then they need not be mentioned here.
      # JRuby and JRuby-Rack are pre-loaded in this list. Be sure to include your
      # own versions if you directly set the value
      # config.java_libs += FileList["lib/java/\*.jar"]
      config.java_libs.delete_if {|f| f =~ /jruby-rack/ || f =~ /jruby-complete/ }
      config.java_libs += FileList["lib/jruby-complete\*.jar"]
      config.java_libs += FileList["lib/jruby-rack\*.jar"]

    This packs the "jruby-complete-1.3.1.jar" in your .war file.
And now follow your regular procedure of creating the .war file using "jruby -S warble" and happily deploy your Rails/Sintara/Merb applications on GlassFish.

There are several users who are already using Rails on GlassFish in production environment and they are listed at rubyonrails+glassfish+stories. Drop a comment on this blog if you are using it too :)

Technorati: jruby rack glassfish war servlet rubyonrails

Sunday Jan 18, 2009

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 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 "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:

arun@opensolaris:~/glassfishv3-prelude/bin$ ./pkg

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.
Installing: [pkg:/pkg@1.0.7,0-15.1269:20081008T211255Z,
Initialization complete.

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)
Completed                                    1/1       11/11     0.45/0.45
PHASE                                        ACTIONS
Install Phase                                  24/24

And verify as ...

arun@opensolaris:~/glassfishv3-prelude/bin$ ./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                          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                                    installed  ----
pkg                                           1.0.7-15.1269   installed  ----
pkg-java                                      1.0.7-15.1269   installed  ----
python2.4-minimal                    installed  ----

As shown above, "glassfish-ejb" module with version "" 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

  2. Declare a public method "sayHello" and add @javax.ejb.Stateless annotation to mark it a stateless EJB as shown

  3. Create a new Servlet by selecting the option as shown

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

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

  5. 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

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.

Java EE 7 Samples

Stay Connected


« April 2014