Session Beans

Session Beans

In the Java Platform, Enterprise Edition specifications, a Session Bean is a type of Enterprise Beans. In the J2EE architecture, the other two types are Entity Beans and Message-driven beans. However, in Java EE 5 entity beans have been replaced by Java Persistence API entities.

Contrary to Entity Beans 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 is created by a client and usually exists only for the duration of a single client-server session. 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.

tateless Session Beans

A stateless session bean is a distributed object that does not have an associated conversational state, thus allowing 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 are identical.

Remote Stateless SessionBean Hello World example:

Java EE 5

import javax.ejb.Remote;

@Remotepublic interface HelloWorld { String getHello();}

import javax.ejb.Stateless;

@Statelesspublic 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() throws RemoteException { 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); }

tateful Session Beans

Stateful session beans are distributed objects having a conversational state. The state could be persisted, but access to the bean is limited to only one client.

External links

* [http://java.sun.com/j2ee/1.4/docs/tutorial/doc/EJBConcepts3.html What is a Session Bean ? (Sun's J2EE Tutorial)]
* [http://java.sun.com/j2ee/1.4/docs/tutorial/doc/EJBConcepts9.html Entreprise Beans lifecycle]
* [http://www.unix.org.ua/orelly/java-ent/ebeans/appb_02.htm Life cycle state diagram of session beans]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Session Bean — Enterprise JavaBeans (EJB) sind standardisierte Komponenten innerhalb eines Java EE Servers (Java Enterprise Edition). Sie vereinfachen die Entwicklung komplexer mehrschichtiger verteilter Softwaresysteme mittels Java. Mit Enterprise JavaBeans… …   Deutsch Wikipedia

  • Enterprise Java Beans — Enterprise JavaBeans (EJB) sind standardisierte Komponenten innerhalb eines Java EE Servers (Java Enterprise Edition). Sie vereinfachen die Entwicklung komplexer mehrschichtiger verteilter Softwaresysteme mittels Java. Mit Enterprise JavaBeans… …   Deutsch Wikipedia

  • Enterprise JavaBean — Simple EJB2 Architecture Enterprise JavaBeans (EJB) is a managed, server side component architecture for modular construction of enterprise applications. The EJB specification is one of several Java APIs in the Java EE specification. EJB is a… …   Wikipedia

  • Enterprise JavaBeans — (EJB) sind standardisierte Komponenten innerhalb eines Java EE Servers (Java Enterprise Edition). Sie vereinfachen die Entwicklung komplexer mehrschichtiger verteilter Softwaresysteme mittels Java. Mit Enterprise JavaBeans können wichtige… …   Deutsch Wikipedia

  • Entity Bean — Enterprise JavaBeans (EJB) sind standardisierte Komponenten innerhalb eines Java EE Servers (Java Enterprise Edition). Sie vereinfachen die Entwicklung komplexer mehrschichtiger verteilter Softwaresysteme mittels Java. Mit Enterprise JavaBeans… …   Deutsch Wikipedia

  • Spring Framework — Infobox Software name = Spring Framework caption = developer = [http://www.springsource.com SpringSource] latest release version = 2.5.5 latest release date = release date|2008|06|23 latest preview version = latest preview date = operating system …   Wikipedia

  • Granite data services — Infobox Software name = Granite data services caption = collapsible = author = developer = released = latest release version = latest release date = latest preview version = latest preview date = frequently updated = programming language =… …   Wikipedia

  • Java Persistence API — The Java Persistence API, sometimes referred to as JPA, is a Java programming language framework that allows developers to manage relational data in Java Platform, Standard Edition and Java Platform, Enterprise Edition applications.The Java… …   Wikipedia

  • Apache OpenEJB — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

  • J2EE — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”