Getting Started With EJB 3.0
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.
- Download JBoss AS 4.0.3 and install it with the EJB 3.0 profile selected.
- Download a 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:
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
- Create a new Enterprise EJB Module project named StatelessBeans. Make sure the Server is JBoss and the J2EE version is Java EE 5.0.
- Add a new Session Bean (Ctrl+N - look under the Enterprise category) named CalculatorBean. Set the Package to org.jboss.tutorial.stateless.bean. Select the check boxes to create both Remote and Local interfaces.
- 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
- Now since both the local and remote interfaces will define the same method, create a Java Interface (Ctrl+N) named Calculator 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);
- Then update the local and remote interfaces to extend Calculator.
- 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.
- Add the following to the body of the appropriate method:
return x + y;
return x - y;
- 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 to StatelessBeans.ejb3 and save the file (Ctrl+S).
- 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.
- 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.
- Create a new Java Main Class (Ctrl + N) named Client. Put it in the package org.jboss.tutorial.stateless.client.
- Add the following code to the main method:
InitialContext ctx = new InitialContext();
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));
- Press Alt+Shift+F to fix the imports.
- 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.
- 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.
- 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.