If you read my bio, you'll see that I have a pretty deep background in J2EE technology. However, over the past year I've been focusing on other technologies, like the eBay SDK for Java and J2ME. In the mean time, J2EE, now Java EE, has sped on past. With the forthcoming release of Java EE 5, things are changing dramatically, specifically with regards to EJBs. Last month JSR 220 - Enterprise JavaBeans 3.0 released its Proposed Final Draft, so I thought this would be a good time to reacquaint myself with the technology. Now, how hard could it be? The sole purpose of EJB 3.0 is to "improve the EJB architecture by reducing its complexity from the developer's point of view". Still, EJB 3.0 introduces the developer to lots of new concepts, like annotations, dependency injection, persistence units and entity managers to name a few.

So I started with a simple stateless session bean example from JBoss' web site. Of course, I wanted to check out the new support for EJB 3.0 coming in the next version of NetBeans, so I thought I share with you my experience.

Getting Started

  • Download JBoss AS 4.0.3 and install it with the EJB 3.0 profile selected.
  • Downloada development build of the NetBeans IDE. Choose Java EE 5 as the release version and Daily as the release type.
  • Start the NetBeans IDE and switch to the Runtime tab (Ctrl+5). Right-click the Server's node and add the JBoss Application Server 4.0.3 that you just installed.
  • You now see JBoss Application Server 4.0.3 in the Server's node. Right-click the node and select Start. You'll see the output from the JBoss server log in NetBeans's Output window and the JBoss server node will have green arrow indicating that's it is running:


Fast Track

If you just want to take the project and run, extract StatelessBeans.zip. Open the project in NetBeans. Deploy the project and run Client.java.

Create the Project

  1. Create a new Enterprise EJB Module project namedStatelessBeans. Make sure the Server is JBoss and the J2EE version is Java EE 5.0.
  2. Add a new Session Bean (Ctrl+N - look under the Enterprise category) named CalculatorBean. Set the Package toorg.jboss.tutorial.stateless.bean. Select the check boxes to create both Remote and Local interfaces.
  3. Expand the Enterprise Beans node of your project to see your CalculatorBean. Double-click it to open the EJB in the editor. Note that it's a simple POJO that implements two interfaces, CalculatorRemote and CalculatorLocal. Also note the @Stateless annotation. That's all that's required for the container to recognize this as a stateless session bean

  4. Now since both the local and remote interfaces will define the same method, create a Java Interface (Ctrl+N) namedCalculator that they can both extend and add the following method signatures:
    public int add(int x, int y);
    public int subtract(int x, int y);
  5. Then update the local and remote interfaces to extend Calculator.
  6. Now switch back to the CalculatorBean and place your cursor on the class declaration (with the wavy red line). Right-click the light bulb that appears to implement the local and remote interface methods.

  7. Add the following to the body of the appropriate method:
        return x + y;
        return x - y;
  8. And our EJB is now complete. Before we can deploy it to JBoss however, we need to change the default archive extension name from "jar" to "ejb3". Switch to the Files tab (Ctrl + 2) and open the project.properties file in the nbproject directory. Change the jar.name property from StatlessBeans.jar toStatelessBeans.ejb3 and save the file (Ctrl+S).
  9. We can also delete the generated configuration files as they're empty - the container now knows what to do based on the Annotations. Switch back to the Projects tab (Ctrl+1) and expend the Configuration Files node. Delete ejb-jar.xml and jboss.xml.
  10. Right-click the StatelessBeans project and choose Deploy Project. If all goes well, you'll see the following in the output window:


Create a Test Client

We'll create a simple client to test the EJB.

  1. Create a new Java Main Class (Ctrl + N) namedClient. Put it in the packageorg.jboss.tutorial.stateless.client.
  2. Add the following code to the main method:
        InitialContext ctx = new InitialContext();
        ctx.addToEnvironment("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
        ctx.addToEnvironment("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
        Calculator calculator = (Calculator) ctx.lookup(CalculatorRemote.class.getName());
        System.out.println("1 + 1 = " + calculator.add(1, 1));
        System.out.println("1 - 1 = " + calculator.subtract(1, 1));
  3. Press Alt+Shift+F to fix the imports.
  4. The compiler's still unhappy. Place your cursor on the InitialContext line and right-click the light bulb that appears to add the throws clause for the NamingException.

  5. Now our JBoss plug-in is missing a couple of libraries required to run this class, so we need to add them. Right-click the Libraries node of the StatelessBeans project and choose Add Library. Click the Manage Libraries button and add a New Library named jboss-aop.deployer. Add jboss-aop-jdk50.jar and jboss-aspect-library-jdk50.jar from the jboss-4.0.3\server\default\deploy\jboss-aop.deployer directory. Click OK to return to the Add Library dialog and then Add Library to add the new library to your project. You now have this library available for any future needs.
  6. Run the Client (Shift+F6) and you'll see the following output:


    Note, to get rid of the log4j warnings, save this log4j.xml to your src/java directory and build the project.