Jump to content

Session Beans: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Tag: section blanking
Reverted 1 edit by 101.212.101.162 (talk): Please do not randomly blank things. (TW)
Line 7: Line 7:


A session bean performs operations, such as calculations or database access, for the client. Although a session bean can be transactional, it is not recoverable should a system crash occur. Session bean objects either can be stateless or can maintain [[conversational state (Java EE)|conversational state]] across methods and transactions. If a session bean maintains state, then the EJB container manages this state if the object must be removed from memory. However, the session bean object itself must manage its own persistent data.
A session bean performs operations, such as calculations or database access, for the client. Although a session bean can be transactional, it is not recoverable should a system crash occur. Session bean objects either can be stateless or can maintain [[conversational state (Java EE)|conversational state]] across methods and transactions. If a session bean maintains state, then the EJB container manages this state if the object must be removed from memory. However, the session bean object itself must manage its own persistent data.

==Stateless Session Beans==

A '''stateless session bean''' is an object that does not have an associated conversational state, but may have instance state. It does not allow concurrent access to the bean. The contents of instance variables are not guaranteed to be preserved across method calls. All instances of a stateless session bean should be considered identical by the client.

Local Stateless SessionBean [[Hello world program|Hello World]] example:

=== Java EE 6 ===

<source lang="java">
import javax.ejb.Stateless;

@Stateless
public class HelloWorldBean {
public String getHello() {
return "Hello World !";
}
}
</source>

<source lang="java">
import java.io.*;
import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet {
@EJB
private HelloWorldBean helloWorld;

public void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println(helloWorld.getHello());
}
}
</source>

Remote Stateless SessionBean [[Hello world program|Hello World]] example:

=== Java EE 5 ===

<source lang="java">
import javax.ejb.Remote;

@Remote
public interface HelloWorld {
String getHello();
}
</source>

<source lang="java">
import javax.ejb.Stateless;

@Stateless
public class HelloWorldBean implements HelloWorld {
public String getHello() {
return "Hello World !";
}
}
</source>

<source lang="java">
import java.io.*;
import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet {
@EJB
private HelloWorld helloWorld;

public void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println(helloWorld.getHello());
}
}
</source>

=== J2EE 1.4 ===

Remote interface, declares methods clients can invoke on the EJB:

<source lang="java">
import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface HelloWorld extends EJBObject {
public String getHello() throws RemoteException;
}
</source>

Home interface, declares create, destroy and finder methods for the EJB depending on type:

<source lang="java">
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.*;

public interface HelloWorldHome extends EJBHome {
// Create method used by the Container to create the EJB
// must return remote interface of EJB
public HelloWorld create() throws RemoteException, CreateException;
}
</source>

The implementing EJB class:

<source lang="java">
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.rmi.*;

public class HelloWorldEJB implements SessionBean {
private SessionContext con;

// Implementation of method declared in remote interface
public String getHello() {
return "Hello World!";
}
// Used by the EJB Container
public void setSessionContext (SessionContext con) {
this.con = con;
}

public void ejbCreate() {
}

public void ejbRemove() {
}

public void ejbActivate() {
}

public void ejbPassivate() {
}
}
</source>

A simple client:

<source lang="java">
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

public class Client {

private HelloWorld hello = null;

public String sayHello() throws Exception {
private InitialContext init = new InitialContext();

// Looking up the EJB based on its name in JNDI
Object objref = init.lookup("HelloWorld");
HelloWorldHome home = (HelloWorldHome)PortableRemoteObject.narrow (objref, HelloWorldHome.class);
hello = home.create();

return hello.getHello();
}

public static void main (String[] args) {
Client client = new Client();

try {
System.out.println(client.sayHello());
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
}
</source>


==Stateful Session Beans==
==Stateful Session Beans==

Revision as of 16:46, 11 September 2012


In the Java Platform, Enterprise Edition specifications, a Session Bean is a type of Enterprise Bean. The only other type is the Message-driven bean. Legacy EJB versions from before 2006 (EJB3) had a third type of bean, the Entity Bean. In EJB 3.0 (Java EE 5) those Entity Beans have been replaced by Java Persistence API entities.

Contrary to JPA Entities, which represent persistent data maintained in a database, a Session Bean implements a business task and is hosted by an EJB container.

A session bean performs operations, such as calculations or database access, for the client. Although a session bean can be transactional, it is not recoverable should a system crash occur. Session bean objects either can be stateless or can maintain conversational state across methods and transactions. If a session bean maintains state, then the EJB container manages this state if the object must be removed from memory. However, the session bean object itself must manage its own persistent data.

Stateless Session Beans

A stateless session bean is an object that does not have an associated conversational state, but may have instance state. It does not allow concurrent access to the bean. The contents of instance variables are not guaranteed to be preserved across method calls. All instances of a stateless session bean should be considered identical by the client.

Local Stateless SessionBean Hello World example:

Java EE 6

import javax.ejb.Stateless;

@Stateless
public class HelloWorldBean {
    public String getHello() {
        return "Hello World !";
    }
}
import java.io.*;
import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet {
    @EJB
    private HelloWorldBean helloWorld;

    public void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	resp.getWriter().println(helloWorld.getHello());
    }
}

Remote Stateless SessionBean Hello World example:

Java EE 5

import javax.ejb.Remote;

@Remote
public interface HelloWorld {
    String getHello();
}
import javax.ejb.Stateless;

@Stateless
public class HelloWorldBean implements HelloWorld {
    public String getHello() {
        return "Hello World !";
    }
}
import java.io.*;
import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet {
    @EJB
    private HelloWorld helloWorld;

    public void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	resp.getWriter().println(helloWorld.getHello());
    }
}

J2EE 1.4

Remote interface, declares methods clients can invoke on the EJB:

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface HelloWorld extends EJBObject {
    public String getHello() throws RemoteException;
}

Home interface, declares create, destroy and finder methods for the EJB depending on type:

import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.*;

public interface HelloWorldHome extends EJBHome {
    // Create method used by the Container to create the EJB
    // must return remote interface of EJB
    public HelloWorld create() throws RemoteException, CreateException;
}

The implementing EJB class:

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.rmi.*;

public class HelloWorldEJB implements SessionBean {
    private SessionContext con;

    // Implementation of method declared in remote interface
    public String getHello() {
        return "Hello World!";
    }
      
    // Used by the EJB Container
     public void setSessionContext (SessionContext con) {
       this.con = con;
    }

    public void ejbCreate() {
    }

    public void ejbRemove() {
    }

    public void ejbActivate() {
    }

    public void ejbPassivate() {
    }
}

A simple client:

import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

public class Client {

    private HelloWorld hello = null;

    public String sayHello() throws Exception {	
        private InitialContext init = new InitialContext();

        // Looking up the EJB based on its name in JNDI
        Object objref = init.lookup("HelloWorld");
        HelloWorldHome home = (HelloWorldHome)PortableRemoteObject.narrow (objref, HelloWorldHome.class);
        hello = home.create();

        return hello.getHello();
    }

    public static void main (String[] args) {
        Client client = new Client();

        try {
            System.out.println(client.sayHello());
        } catch (Exception e) {
            System.err.println("Error: " + e);
        }
    }
}

Stateful Session Beans

The state of an object consists of its instance variables. In a stateful session bean, the instance variables represent the state of unique client-bean sessions. The interaction is of the client with bean is called as conversational state.

See also