Enterprise JavaBeans (EJB) technology is a J2EE technology for developing business components in a component-based, enterprise Java application.

EJB technology is generally viewed as powerful and sophisticated. The technology helps developers build business applications that meet the heavy-duty needs of an enterprise. In particular, applications built using EJB and other J2EE technologies are secure, scale to support very large numbers of simultaneous users, and are transaction-enabled so that data maintains its integrity even though it's processed concurrently by multiple users. 

Despite its power and sophistication, some developers are hesitant to use EJB technology. The major obstacle for these developers is complexity. But there's good news in store.

 Java 5 introduced metadata annotations, which are property settings that mark a piece of code, such as a class or method, as having particular attributes. This is a declarative style of programming, where the developer specifies what should be done and the system adds the code to do it.

In EJB, metadata annotations dramatically simplify development and testing of applications, without having to depend on an external XML configuration file. It allows developers to declaratively add services to EJB components as and when they need.

Here we'll assume you have already a basic knowledge of the three types of Enterprise Java so we'll focus on the innovation introduced by the new release. So let's start from Session Bean.

Stateless Session Bean lifecycle

To define a session bean, you first need to define the service interface containing all its business methods. The session bean interface is just plain old Java interface without any annotation. The client uses this interface to retrieve a stub object of the session bean from the EJB 3.0 container

package demo.ejb3;

public interface SessionDemoItf {

public String doQuery (String param);

}

The @Stateless annotation indicates that this bean is a stateless session bean. Just add this notation and you'll turn your POJO into an EJB !

We've also added the @PostConstruct and @PreDestroy notations, which are not compulsory but it's helpful to understand the lifecycle of the EJB.
 

package demo.ejb3;

import javax.annotation.PostConstruct;
import javax.ejb.*;

@Stateless
public class SessionDemoBean implements SessionDemoItf {

    public String doQuery(String param) {
        return "Query with param " + param + " executed.";
    }

    @PostConstruct
    public void initialize () {
        // Initialize here objects which will be used
        // by the session bean
        System.out.println("SessionDemoBean initialized.");
    } 


    @PreDestory
    public void destroyBean() {
        // Free here resources acquired by the session bean
        System.out.println("SessionDemoBean initialized.");
    } 
}

ejb 3.0 stateless jboss

Picture 1: Stateless Session Bean lifecycle

Compile everything using a JDK 1.5 or greater and remember to include in the classpath jboss-ejb3x.jar which contains the implementations necessary to compile the EJB annotations.

Now it's time to pack your EJB. Simply jar the two files and throw them in the "deploy" folder of JBoss ! Isn't that sooo simple ?? I guess you might be wondering what happened to the EJB configuration, for example without any configuration file of do you code JNDI name ? There's of course an annotation also for this but if you don't use them JBoss will assign one for you, depending if your EJB is packaged in an ear or in a jar file.

What is the default EJB JNDI name ?

 If the application is deployed in a EAR file, the default JNDI name is the EAR-FILE-BASE-NAME/EJB-CLASS-NAME/local for the stub for local interface. For the remote interface (see below), it is EAR-FILE-BASE-NAME/EJB-CLASS-NAME/remote.

 If the bean is deployed in a JAR file, the JNDI names are EJB-CLASS-NAME/local and EJB-CLASS-NAME/remote.

Now a minimal client to test your EJB can be a jsp:

<%@ page import="demo.ejb3.*, javax.naming.*, java.text.*" %> 

<%!

private SessionDemoItf ejb = null;
public void jspInit () {

 try {
  InitialContext ctx = new InitialContext();
   ejb = (SessionDemoItf) ctx.lookup(
    "StatefulExample/SessionDemoBean/local");
 } catch (Exception e) {
  e.printStackTrace ();
 }
}
 %>

EJB invoked  <%=ejb.doQuery("param1")%>

See the JNDI name: since we have packaged it in an ear file named StatefulExample.ear the JNDI name will be StatefulExample/SessionDemoBean/local.

If everything was done correctly you'll display in the jsp page the simple string you see in the bottom. What if you aren't satisfied with default bindings ? use the LocalBinding/RemoteBinding to define your custom JNDI name.

@Local ({SessionDemoLocalItf.class})

@LocalBinding (jndiBinding="SessionDemoBean/local")

@Remote ({SessionDemoRemoteItf.class})

@RemoteBinding (jndiBinding="SessionDemoBean/remote")

0
0
0
s2smodern

Related articles available on mastertheboss.com

JBoss MDB 3.0

Message-driven beans (MDBs) are stateless, server-side, transacti

JBoss EJB 3 BMT

In a Bean Managed Transaction, the code in the EJB or Message Dri

How do you keep your EJB callbacks separated from biz. methods?

JBoss daily recipe

How to set EJB timeout period ?

JBoss recipe of the day

How to add a Selector with MDB 3.0 ?

Message selectors allow an MDB to be more selective about the mes

How to create a MDB 3.0 singleton ?

  EJB3 uses the resource adapter.  When using the resource adapt