16 - Stateless Session Beans

Stateless Session Beans

In this section we will discuss how to create and how you can access Stateless Session Beans and the various lifecycle events that are provided by the bean.

Bean Creation

Stateless session beans consists of 1 class, the bean class and 2 interfaces, local and remote. The class is required and the interfaces are optional.

Bean Class

Marking a class as a Stateless session bean requires annotating the class by the annotation @Stateless

Example of bean class:

import javax.ejb.Stateless;

@Stateless
public class ActionLogBean {
    ...
}

The previous lines will create a stateless bean and will be registered in the JNDI trees as “ActionLogBean”. You can override the default name by providing a value to the name attribute on the Stateless annotation, as an example:

import javax.ejb.Stateless;

@Stateless(name="ActionLog")
public class ActionLogBean {
    ...
}

That way the bean will be registered in the JNDI tree with name “ActionLog” instead of “ActionLogBean”.

Local Interface

The local interface is used to declare the bean local view within the container, you can declare the local interface using @Local annotation, as an example:

import javax.ejb.Local;

@Local
public interface ActionLogLocal {

}
@Stateless (name="Action Log")
public class ActionLogBean implements ActionLogLocal {
    ...
}

Also you can directly annotate the bean class with the @Local annotation:

import javax.ejb.Local;
import javax.ejb.Stateless;

@Stateless(name="ActionLog")
@Local(ActionLogLocal.class)
public class ActionLogBean {

}

And In case you are going to have only local interface, you can directly annotate the bean with @Stateless without defining any local interface, that way the local interface of the bean will be the bean public methods.

Remote Interface

The remote interface is used to declare the bean remote/external view outside the container. You can declare the remote interface using @Remote annotation, as an example:

import javax.ejb.Remote;

@Remote
public interface ActionLogRemote {

}

.
.
.

@Stateless(name="ActionLog")
public class ActionLogBean implements ActionLogLocal, ActionLogRemote {

}

Also, you can directly annotate the bean class with the @Remote annotation:

import javax.ejb.Local;
import javax.ejb.Stateless;

@Stateless(name="ActionLog")
@Local(ActionLogLocal.class)
@Remote(ActionLogRemote.class)
public class ActionLogBean implements ActionLogLocal, ActionLogRemote {

}

Stateless Bean Lifecycle

The bean lifecycle is a set of events (callbacks) invoked on different states in the bean lifecycle, the events are normal methods that take no arguments with void return type and throws no checked exceptions:

  • @PostConstruct: This method is called once all dependency injection on the bean class finishes and it indicates that the bean is ready for methods invocation. The method can be used to allocate the resources needed by the bean object to function correctly (i.e. Network connections, database connections... etc.).
  • @PreDdestroy: The method called before destroying the bean object and can be used to release all the reserved resources by the bean object.

The below is a state diagram that visualizes the stateless session bean lifecycle:

An example of the Post/Pre callbacks:

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless(name="ActionLog")
@Local(ActionLogLocal.class)
@Remote(ActionLogRemote.class)
public class ActionLogBean implements ActionLogLocal, ActionLogRemote {

    @PostConstruct
    private void init() {
        // Initalize the bean here
    }    
    @PreDestroy
    private void destroy() {
        // Destroy the bean here
    }
}

 

 

 

Like us on Facebook