The Source for Java Technology Collaboration
User: Password:



Kohsuke Kawaguchi

Kohsuke Kawaguchi's Blog

GlassFish v3 just got embeddable

Posted by kohsuke on April 28, 2008 at 02:34 PM | Comments (14)

Embeddable GFv3

I've always liked Jetty's ability to run inside an existing JVM, just as a library of another application. This enabled Jetty to be used in many situations, like mvn jetty:run for debugging a webapp without even running an application server on its own. IMO this contributed to a part of Jetty's usefulness. Almost every Maven documents today use Jetty for web app development, or doing site:run, etc.

So naturally, we wanted to do the same for GlassFish v3, and I'm happy to report that it got to the point that it holds the water. I mean, I can now run Hudson in this embedded GFv3.

Here's how it works — GlassFish v3 can be run as an OSGi appliation as Sahoo reported earlier, but in fact it can also be run without any kind of classloader isolation system at all. Sure, you won't get the isolations, but this means you can just drop a bunch of GFv3 jars in your classpath and run it like that.

So I added a little bit of API around that to make things pretty, and now you have the embeddable GFv3 API, which can be used like this:

GlassFish glassfish = new GlassFish();
// create smallest possible HTTP set up listening on port 8080
glassfish.minimallyConfigure(8080);

GFApplication app = glassfish.deploy(new File("path/to/simple.war"));

...

app.undeploy();
glassfish.stop();

Imagine the possibilities...

Thanks to the extensibility of GFv3, when you embed GFv3 in your JVM, you can plug into any of its extensibility points and tweak the behaviors in ways that you can't do with externally launched GFv3. You could also pick any flavor of GFv3 you want; if you just need the barebone servlet container and get smaller footprint, you can do that. But if you also need EJB functionality or some of our scripting offerings, that's cool with us, too. This is where we have an edge over Jetty, I think.

This is still a work in progress, but just imagine what we can do with this kind of stuff. How about testing your web services end-to-end without ever requring your developers to install GlassFish and set up database and all that. It also gives you interesting deployment options.

Oh, did I mention the start up time? One good thing about using a single classloader to load everything is that classloading overhead becomes much smaller. On my system, the server now starts in 300ms or so, complete with a deployment of a webapp. How many seconds does it take for your application server to start?

Maven-glassfish-plugin

I also wrote Maven glassfish plugin to wrap this as a Maven plugin. This allows you to do mvn glassfish:run to run the web appliation that you are developing on Maven (the equivalent of mvn jetty:run.)

I'm also thinking about quickly putting together Ant tasks.

Conclusions

170x93_Speaker_v4.gif

As I wrote, this is still a work in progress, but please tell us what you think about where we are going.

Finally, I'll be co-speaking more about this in the upcoming JavaOne technical session "TS-5921 GlassFish Project v3 as an Extensible Server Platform." If you are coming to JavaOne, I hope you'll come to the session.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Do you have a way to wrap everything up into one jar as Hudson does with Winstone? We're using Winstone right now for some stuff, but finding it severely limited in terms of performance and memory usage and we'd like touse something else. But I've been to lazy to write a plugin for Jetty or something else which wraps up my applicaiton.

    Posted by: dandiep on April 28, 2008 at 03:31 PM

  • Yes. There's a distribution of this where the whole thing is bundled into two jars, and it's quite easy to reduce that count to one.

    I probably should add a mojo to maven-glassfish-plugin to do the uberjar-ing; unfortunately Maven assembly's "jar-with-dependencies" won't work nicely with this because certain files (like /META-INF/services/*) need to be merged, instead of overwritten.

    Posted by: kohsuke on April 28, 2008 at 07:21 PM

  • What set of jars are needed to run GF that way?

    Posted by: guillaumelaforge on April 29, 2008 at 12:12 AM

  • It would be great if someone could come up with a class similar to Tomcats org.apache.catalina.startup.Embedded so that you can effectively embed a server and easily deploy/undeploy wars, create virtual hosts etc

    Posted by: buzzheavyyear on April 29, 2008 at 03:37 AM

  • Great ! Will it be possible to point to a development dir (with exploded war) ? And to publish more distinct webapp at the same time (useful for complex applications composed of more webapp) ? Thanks and good work. Bye, Sandro

    Posted by: smartini on April 29, 2008 at 06:14 AM

  • This is a big step in the right deirection, but in order for this to be really usable it needs to be able to instrument the web application from the current class path instead of requiring a war, similar to what can be done in Jetty (see example here).

    I have not yet tried the GlassFish way but I assume it works with tools like cubertura to instrument the entire applications for use in code coverage reports ?.

    Anyways nice to see that GlassFish is propelling forward with stuff like this. I do however think that we need a standard way to write unit tests which depends on embedded servers i.e.:

    public class MyTest extends EJBTest {
         @EJB
         private UserManager userManager
         @PersistenceContext(name = "MyPU")
         private EntityManager entityManager;
    
         // write tests using what ever unit test framework you desire
         :
    }
    
    this should be doable without depending on a specific framework and each vendor can implement it anyway he like

    Posted by: ltackmann on April 29, 2008 at 06:15 AM

  • guillaumelaforge — see documents on the web site and the download section for more details. I also intend to prepare a complete self-contained example in Ant and Maven.

    Posted by: kohsuke on April 29, 2008 at 09:28 AM

  • buzzheavyyear — I thought classes like GlassFish and GFApplication are similar to org.apache.catalina.startup.Embedded. So what I'm showing here is exactly what you are asking.

    Posted by: kohsuke on April 29, 2008 at 09:29 AM

  • smartini — Yes, sir! Thank you for asking the question. The maven-glassfish-plugin doesn't deploy a fully assembled war. Instead, it picks up classes from target/classes, jars from all over your local Maven repositories, and web resources from another place. This is a must-have during development so that you get live feedback for your changes.

    Posted by: kohsuke on April 29, 2008 at 09:31 AM

  • ltackmann — yes, you can deploy servlets loaded in your current classloader without ever assembling that to a war. I pasted the relevant part of the maven plugin source code below so that you can see the capability.
            List<URL> classpath = new ArrayList<URL>();
    
            try {
                for( Artifact a : (Set<Artifact>)project.getArtifacts() ) {
                    classpath.add(a.getFile().toURI().toURL());
                }
                // resources, so that changes take effect in real time
                for (Resource res : (List<Resource>)project.getBuild().getResources()) {
                    classpath.add(new File(res.getDirectory()).toURI().toURL());
                }
                // main artifacts
                classpath.add(new File(project.getBuild().getOutputDirectory()).toURI().toURL());
            } catch (MalformedURLException e) {
                throw new MojoExecutionException("Failed to convert to URL",e);
            }
    
            ScatteredWar war = new ScatteredWar(
                project.getArtifactId(),
                resourcesDirectory,
                webXml,
                classpath
            );
    
            GFApplication app = glassfish.deploy(war);
    

    Also, thanks for the link. I think convenience classes like this is very useful, and we should have the equivalent for GlassFish. With the embeeded GFv3, you take the JVM launch back in your control, so this gives you excellent oppporunity like setting up class file instrumentation for code coverage and etc. That would probably make a good article.

    Finally, an EJB guy sits next to my office, so I'll ask him about your unit test annotation for EJB and see what he says.

    Posted by: kohsuke on April 29, 2008 at 09:38 AM

  • kohsuke - OK, thanks. I'll give it a try. Your example didn't show how to separate the virtual domains and I assumed the worse. Thanks again.

    Posted by: buzzheavyyear on April 29, 2008 at 10:26 AM

  • Dan,

    You should try the jetty-runner (new addition to jetty). It runs your webapp straight from the command line, eg: java -jar jetty-runner.jar my/webapp.war

    Kohsuke, This is an interesting development, downloading now to try it out!

    regards Jan

    Posted by: janbartel on April 30, 2008 at 01:18 AM

  • This made it into JavaLobby

    Posted by: kohsuke on April 30, 2008 at 08:22 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds