Skip to main content

Embedding GlassFish v3 with Grails

Posted by vivekp on May 1, 2008 at 4:52 PM PDT

In my blog on running Grails application on GlassFish v3, I need to create a war of my application and then deploy that war.
This was a serious bottleneck in my development process as I wanted to test it out this application on GlassFish v3.

I really wanted to run my Grails application with GlassFish v3 as embedded server in the same fashion as running grails
run-app
, that is without creating a war file and to speed up my development. By default Grails uses
Jetty web server to run the applications. But I wanted to use V3, it is very modular, extensible and embeddable and and is
going to allow me to get the JavaEE features that I may like to use at some point in time.

One possible way was that GlassFish v3 is an OSGi
container
and if Grails has an OSGi container I could easily replace jetty with v3. So I mentioned this problem
to Kohsuke and came to know that he is working on
GlassFish v3 embed APIs. So I gave it a try and guess what? It was darn simple to write few lines of groovy script to
get it embedded with Grails and run my application.

Here is the groovy script that starts GlassFish v3 server and deploys the grails application. Please note that the
API is using scattered war in literal sense - there is no WAR file structure here.

        

    appName = new File("${grailsAppName}");
    webxml = new File(webXmlFile.absolutePath)
    resource = new File("${basedir}/web-app")
    ScatteredWar war = new ScatteredWar(
                "${grailsAppName}",
                resource,
                webxml,
                Collections.singleton(new File(appName, classesDir.getAbsolutePath()).toURI().toURL()))

    grailsServer = new GlassFish(serverPort)/a>
    grailsApp = grailsServer.deploy(war)
       

   

I called this script RunaAppGf.groovy and after adding V3 Embed API and other v3 dependent jars (web-all.jar) to the
Grails classpath, copied it to $GRAILS_HOME/script directory. With this I was ready to run my grails app and so I did
by invoking grails run-app-gf inside my grails application directory.

    

vivekmz@boson(653)> grails run-app-gf

Welcome to Grails 1.0.1 - http://grails.org/
Licensed
under Apache Standard License 2.0
Grails home is set to: /tools/grails

Base Directory: /myhome/vivekmz/dev/grails/hello
Environment set to development
Note: No plugin scripts found
Running script /tools/grails/scripts/RunAppGf.groovy
Starting GlassFish embedded server...Running Grails application..
Application name : hello
Web App Root :/myhome/vivekmz/dev/grails/hello/web-app
web.xml:/myhome/vivekmz/.grails/1.0.1/projects/hello/resources/web.xml
May 1, 2008 1:46:45 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: HK2 initialized in 380 ms
May 1, 2008 1:46:45 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: com.sun.enterprise.naming.impl.ServicesHookup@304648 Init done in 391 ms
May 1, 2008 1:46:45 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: com.sun.enterprise.v3.server.Globals@6c9f0f Init done in 393 ms
May 1, 2008 1:46:45 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: com.sun.enterprise.v3.server.SystemTasks@1cdc190 Init done in 400 ms
May 1, 2008 1:46:45 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: com.sun.enterprise.v3.services.impl.HouseKeeper@d9b071 Init done in 403 ms
May 1, 2008 1:46:45 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: com.sun.enterprise.v3.services.impl.CmdLineParamProcessor@19f9c7a Init done in 407 ms
JMXMP connector server URL = service:jmx:jmxmp://localhost:8888
May 1, 2008 1:46:45 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Listening on port 8080
May 1, 2008 1:46:45 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: com.sun.enterprise.v3.services.impl.GrizzlyService@3a4822 startup done in 718 ms
May 1, 2008 1:46:45 PM com.sun.enterprise.v3.services.impl.ApplicationLoaderService postConstruct
INFO: loader service postConstruct started at 1209678405813
May 1, 2008 1:46:45 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: Application Loader startup done in 764 ms
May 1, 2008 1:46:45 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: Glassfish v3 started in 765 ms
May 1, 2008 1:46:51 PM com.sun.enterprise.web.WebModuleContextConfig authenticatorConfig
SEVERE: webModuleContextConfig.missingRealm
[0] commons.DefaultGrailsApplication The class [UrlMappings] was not found when attempting to load Grails application. Skipping.
[3] commons.DefaultGrailsApplication The class [Config] was not found when attempting to load Grails application. Skipping.
[5] commons.DefaultGrailsApplication The class [BootStrap] was not found when attempting to load Grails application. Skipping.
[8] commons.DefaultGrailsApplication The class [DataSource] was not found when attempting to load Grails application. Skipping.
[10] commons.DefaultGrailsApplication The class [resources] was not found when attempting to load Grails application. Skipping.
[1426] spring.GrailsWebApplicationContext Refreshing org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@1e5052b: display name [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@1e5052b]; startup date [Thu May 01 13:46:53 GMT-08:00 2008]; parent: org.springframework.web.context.support.XmlWebApplicationContext@13cb2c6
[1426] spring.GrailsWebApplicationContext Bean factory for application context [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@1e5052b]: org.springframework.beans.factory.support.DefaultListableBeanFactory@192974
Server running. Browse to http://localhost:8080/hello
   

The next steps are really to provide the required V3 jars, V3 Embed Jars and this script as a bundle so that you can
easily install over the Grails installation and enjoy modularity, extensibility and embeddability of GlassFish v3. I plan to talk about it more during JavaOne and Kohsuke and me are trying to see if we could have something ready in another day or so.

Related Topics >>

Comments

Great post. Do you have any idea how to set up a datasource with the embedded glassfish instance?

Hi Guillaume,
Yes, I am looking forward to have a discussion on it so that Grails can provide pluggable servlet containers.

While at JavaOne, it could be interesting to discuss how we could make the servlet engine configurable in the standard Grails run-app script, and use this script and GFv3 for those who want to try their app in different containers, or target GF as their target deployment platform.