Your First EJB3 Session Bean

In Java, JavaEE, Programming by timfanelliLeave a Comment

I just recently started playing with EJB3, and found it extremely tricky to get through my very first EJB3 Session Bean. While significantly simpler than EJB 2, the online resources just aren’t there. Hopefully this simple Hello, EJB3! will help get you started.

Note: Your mileage may vary based on your application server platform. I do my development work on WebSphere Application Server 6.1.0.9 with the EJB3 Feature Pack Beta installed.

Write Your Session Bean

The first thing I did was create a new, empty Java Project – not a J2EE project, not an EJB project, just a blank project space. This avoids the built in wizards in your environment from creating deployment descriptors and other such silly EJB 2 nonsense.

Next, create a new interface called HelloEJB3, like so:

package com.timfanelli.ejb3;

import javax.ejb.Stateless;

public interface HelloEJB3 {
   public String sayHello();
}

This will be the local business interface for our session bean. If you want a remote business interface, annotate the interface with @Remote.

Next, create the bean implementation class, like so:

package com.timfanelli.ejb3;

import java.ejb.Stateless;

@Stateless
public class HelloEJB3Bean implements HelloEJB3 {
   private String greeting = "Hello, EJB3!";

   public String sayHello() { return greeting; }
}

And that’s it! You have yourself an EJB3 stateless session bean with a local (or remote, if you annotated) business interface!

EJB3 eliminated the need for several complicated things noone liked, such as home interfaces, deployment descriptors or complicated EJB interfaces. Pretty neat. JAR it up and you’re ready to roll on to writing a test client.

Oh – it’s worth noting for backward compatibility, the @Stateless annotation tells your container that this is a stateless session bean, and it will be bound to java:comp/env/ejb/HelloEJB3.

EJB3 Session Bean Client

For our client, we’ll create a new servlet application. The application will have a single servlet, HelloWeb. We’ll use the new EJB3 dependency injection to obtain an instance of our bean, and use its business interface to execute methods on it; like so:

package com.timfanelli.ejb3;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	
	 @EJB
	 private HelloEJB3 hellobean;
	 
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.getWriter().write(hellobean.sayHello());
   }  	
	
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// TODO Auto-generated method stub
   }   	  	    
}

The important parts are in bold for you. Note the line annotated with @EJB. This is all you need to do to perform the JNDI lookup for your EJB bean. The container will automatically lookup java:comp/env/ejb/HelloEJB3 from the JNDI context for you.

Then, all you need to do is use its business methods, as shown in the doGet() method of the servlet.

Don’t forget to add the name of the jar file that holds your HelloEJB3 interface and HelloEJB3Bean class to your web applications MANIFEST. Otherwise, you’ll hit runtime classpath errors.

Deployment Setup

Here’s where the really tricky part came in for me. I had to create an EAR project to add my HelloEJB3 project to, and also my web application project. Since I’m using Rational Application Developer 7, which does not yet have tooling support for EJB3, this was a little difficult.

In my EAR, I have added the WAR file as a war module – no surprise there. I then added my java project, called HelloEJB3.jar, as a utility jar and an ejb-module. Since I’m not overly familiar with many other tools, I don’t know what that means for the rest of you. In any event, here’s my application.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<application id="Application_ID" version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
	<display-name>
	HelloEJB3EAR</display-name>
	<module>
		<web>
			<web-uri>HelloTestWeb.war</web-uri>
			<context-root>HelloTestWeb</context-root>
		</web>
	</module>
	<module><ejb>HelloEJB.jar</ejb></module>
</application>

That’s all there really is to it. You should be able to deploy to your EJB3 ready application server, access your servlet, and see your hello message!

Best of luck. And remember, stateless session beans are only the tip of the iceberg. I’m also investigating the Java Persistence API to replace my EJB2 Entity Beans… will post migration stories later!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.