EhCache Standalone Server integration with GlassFish Embedded Web 3.1.2

This one happened to me earlier this year. I was coding a DAO using Hibernate, and I got to the part of configuring a 2nd Cache Level. The whole team decided that it would be a best practice to use Terracotta’s Ehcache Server, which is considered one of the best Java cache Servers out there.

You can check my opened ticket in Terracotta’s JIRA Server for the error log and my new server implementation. Click on Read more for further details.

The problem I found was that their standalone server was not compatible with my Java 6 JAX-WS libraries, as it IS still using an old version of GlassFish Embedded Web Application Server to deploy it. When I tried to run the server I got a pretty messy stack trace containing a duplicated Class Exception like this:

WARNING: duplicate class definition bug occured? Please report this : net/sf/ehcache/server/jaxb/Element$JaxbAccessorM_getValue_setValue_[B
java.lang.ClassFormatError: Illegal class name "net/sf/ehcache/server/jaxb/Element$JaxbAccessorM_getValue_setValue_[B" in class file net/sf/ehcache/server/jaxb/Element$JaxbAccessorM_getValue_setValue_[B

I downloaded the sources of the Standalone Server and customized the original Server class file to make it work with GlassFish embedded 3.1.2 following the Oracle guides:

Server.java

private void startWithGlassfish() {

            try {

                glassfishProperties.setPort("http-listener", httpPort);

                glassfishProperties.setPort("https-listener", httpPort+1);

                glassfish  = GlassFishRuntime.bootstrap().newGlassFish(glassfishProperties);

                glassfish.start();

                Deployer deployer = glassfish.getDeployer();
                deployer.deploy(war, "--contextroot=ehcache", "--enabled=true" ,"--force=true");

                LOG.info("Glassfish server running on httpPort " + httpPort + " with WAR " + war);

            } catch (Exception e) {
                LOG.error("Cannot start server. ", e);
            }
        }

After this, all that’s left is to build the jar file again and put it all together, for which I made myself a handy script:
~/ehcache-standalone-server-1.0.0/sources/ehcache-standalone-server-1.0.0-sources/build-ehcache-ubuntu.sh

rm ehcache-standalone-server-1.0.0.jar
javac -cp ../../lib/commons-daemon-1.0.1.jar:../../lib/glassfish-embedded-web-3.1.2.jar:../../lib/slf4j-api-1.5.8.jar:../../lib/slf4j-jdk14-1.5.8.jar net/sf/ehcache/server/standalone/Server.java
jar cvfm ehcache-standalone-server-1.0.0.jar META-INF/MANIFEST.MF  net/sf/ehcache/server/standalone/*.class
cp ehcache-standalone-server-1.0.0.jar ../../lib/

Place this file under the lib directory and run the startup.sh script under the bin directory:

java -server -Xmx1g -jar ../lib/ehcache-standalone-server-1.0.0.jar 8080 ../war

That’s it, you’ve got the latest GlassFish Embedded running the latest ehcache server.
Regards

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s