Friday, October 24, 2008

Giving EJB 3.1 a Test Drive

Although not yet final, project GlassFish features already an alpha implementation of EJB 3.1. TheServerSide posted a nice series of articles on what you can expect to come - but honestly, wouldn't it be much more fun to actually get some hands dirty? Here's a quick description on how you can give it a spin. We will demonstrate the following new features:
  • Singleton Beans
  • Simplified EJB packaging
  • Simplified EJB without Interfaces
  • Unified JNDI naming
Start with downloading the latest release of GlassFish v3. The Windows installer guides you through the installation. Use the default settings except for the authentification - choose a username and a password (this used to default to "admin/adminadmin"). At the end you will be prompted by the Updatetool - start it and install the Add-Ons shown in the image below:

Beside the EJB Container we'll also install the new JAX-RS REST API to access the EJB. The JAX-RS JSR has just recently gone final, and is definitely also worth a look. Once the installation is done, we'll switch to the IDE to code our beans. I'm using Eclipse with the Maven 2 plugin to get started quickly.

Choose New Maven Project and select the maven-archetype-webapp archetype as shown in the screenshot below. We're naming our project "ejbtestdrive" - note the name as it will be used a couple of times in the further process. Finish the create wizard and add the src/main/java folder as a source folder in Eclipse after creating it.

To resolve our dependencies, lets first configure the Maven repositories to download the EJB 3.1 as well as the JAX-RS API. Add the following lines to your pom.xml:
<name>Glassfish Repository</name>
<name> Repository</name>

Which allows us to download following dependencies:

Almost done with the Maven setup. We'll just add the GlassFish Maven plugin to our build and set the compiler to deal with Java 6 (Java 5 should be fine too):

The referenced properties should be defined in a Maven profile, but for simplicity we'll just add them now to our POM (just replace the values with your installation location and your username/password):

You're now ready to run the WAR file on GlassFish. Just run
mvn package glassfish:deploy

to start GlassFish and get your WAR file deployed (Note: If the Maven plugin complains about asadmin not be a Win32 application, just remove the UNIX script so the .bat will be taken).

Finally, time to write some code! Go back to Eclipse and create a package. Our EJB is going to be a (surprise surprise) HelloWorld bean. Create a new class which looks like the following:
package com.ctp.ejb;

import java.util.LinkedHashSet;
import java.util.Set;

import javax.ejb.Singleton;

* The friendly first EJB 3.1 bean.
* @author [you!]
public class HelloWorldBean {

private Set<String> names = new LinkedHashSet<String>();

public String hello(String name) {
return "Hello " + names;
In case Eclipse complains about the compiler compliance, just set the Java project facet in the project's properties to something > 1.4. You've now created one of the new Singleton EJBs. The bean is going to remember everybody who said "hello" and give back a friendly greeting. Note that there is no local or remote interface at all! Also using a global state in a bean is something nasty to achieve before EJB 3.1. Run
mvn package glassfish:redeploy

to deploy your bean - noteably in just a simple WAR file! Did you notice how fast this deploys?

Now it's time to access our bean over JAX-RS. Create another class which looks like this:
package com.ctp.ejb;

import javax.naming.InitialContext;

* A RESTful bean being hopefully quite busy!
* @author [you!]
public class HelloWorld {

public String hello(@PathParam("username") String name) throws Exception {
HelloWorldBean hello = (HelloWorldBean)
new InitialContext().lookup("java:global/ejbtestdrive/HelloWorldBean");
return hello.hello(name);

Simple enough, right? This is taking requests under /hello/... with a username, looking up the bean in JNDI and call it's hello method. Did you notice the format of the JNDI lookup? Yes, this is finally standardized, making your applications much more portable! In case you named your application differently, make sure the names match between global/ and /HelloWorldBean.
Further details on the JAX-RS functionalities can be found here.

As a last step, we need to add the Jersey servlet to our web.xml. Modify it to match this snippet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
<servlet-name>Jersey Web Application</servlet-name>
<servlet-name>Jersey Web Application</servlet-name>

Check that your package name matches in the Servlets init param. Now package and redeploy - and we're ready to go! Just go to http://localhost:8080/ejbtestdrive/hello/[your name] to see you greeted by your new EJB! Experiment with different names and browser sessions and see the global state preserved.

Hope you enjoyed this little test drive. Let us know on how you're using EJB 3.1 and what your experience is with it!


Balarini said...
This comment has been removed by the author.
Balarini said...

Hello Thomas,

First of all, thanks for the tutorial. I had some problems with it, but i managed to finish it succefully.

I was using Eclipse 3.5.2, first downloaded Maven2 plugin, then started a Dinamic Web Project and put Maven dependency on it.

Eclipse creates a sun-web-xml file, that contains a contextroot tag, and THAT value in the contextroot must be the .lookup("java:global/ValueOfContextRoot/HelloWorldBean") in order to work.

Great! Thanks again!