EJB a Quick Tutorial (Session Beans)


While learning Enterprise Java Beans, I quickly ask myself, what are the advantages of trying to learn it? What can it do to me?.. Well again as part of my growing itch in learning software technologies, I believe learning and understanding EJB will open me up to more JEE complex topics like Web Services, SOA etc.

My first hands-on exercise back then is that basic stuff: Implementing Stateless Session Beans and the Difference between @Local and @Remote. Here’s a quick Stateless Session Bean code I created:

package com.moneymarket.ejb;

import javax.ejb.Stateless;
import moneymarketejbstubs.AccountBeanService;

/**
 *
 * @author Alvin Jay Reyes
 */

@Stateless
public class AccountBean implements AccountBeanService {

    public String sampleBeanRemote() {
        return "SampleBeanRemote";
    }
}

Stateless = means does not save conversational state (well thats why they call it Stateless).

This is just a simple Stateless Session Bean with a method called sampleBeanRemote(). We have to tell the compiler that our Session Bean can be distributed to remote systems. This is done by implementing the AccountBeanService interface:

import javax.ejb.Remote;

/**
 *
 * @author Alvin Jay Reyes
 */
@Remote
public interface AccountBeanService {
    public String sampleBeanRemote();
}

Now a bit of explanation of why is there a @Remote annotation. What’s totally awesome with using Enterprise Java Bean is the it can be completely independent and can be distributed to any Java Application (this includes Stand alone J2SE apps and RIAs). This remote object will act as remote client’s “reference” to the deployed Enterprise Java Bean.

Hence, this reference should exist in a different JAR file and developers must use this JAR file to get a reference of the deployed EJB. Take a look at the screenshot of my sample project below:

The Stub Project above is an Enterprise Application Client. This is the standard deployment if we want to generate custom stubs for out EJB.

Generate the Web Application Project to use the Stub to get a reference of the Session Bean and checkout the following code below for the actual call:

import javax.ejb.EJB;
import moneymarketejbstubs.AccountBeanService;

/**
 *
 * @author Alvin Jay Reyes
 */
public class NewService {

    @EJB
    private AccountBeanService ab;
    /** Creates a new instance of NewService */
    public NewService() {
    }

    public String retMe() {
        return ab.sampleBeanRemote();
    }
}
//Call the retMe() object outside this class (a Session Facade or ManagedBean object).

Yep, all you need is the @EJB annotation and the actual remote reference. Thats what I like with EJB 3.0, it makes life easier. Use the @EJB annotation and you’ll quickly get a reference of the Bean and its services.

This is the most basic stuff for implementing Enterprise Java Beans, note that EJB is wide topic as it has a lot of powerful features (interceptors, timers etc.)

One thought on “EJB a Quick Tutorial (Session Beans)

  1. Pingback: JavaPins

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s