+ All Categories
Home > Documents > Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client...

Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client...

Date post: 12-Jan-2016
Category:
Upload: andra-barber
View: 237 times
Download: 0 times
Share this document with a friend
Popular Tags:
30
Session Beans -) stateless -) stateful
Transcript
Page 1: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Session Beans -) stateless -) stateful

Page 2: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Session Beans

A session bean represents a single client inside the J2EE server. To access an application that is deployed on the server, the client invokes the session bean's methods. The session bean performs work for its client, shielding the client from

complexity by executing business tasks inside the server.

At any given time, only one client has access to the bean instance. The state (i.e. the values of the instance variables) of the bean is not persistent, existing only for a short period of time.A session bean can be stateful or stateless.

Page 3: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful session Beans

In a stateful session bean, the instance variables represent the state of a unique client-bean session. This state is often called the conversational state.

The state is retained for the duration of the client-bean session. If the client removes the bean or terminates, the session ends and the state disappears.

Page 4: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless session Beans

A stateless session bean does not maintain a conversational state for a particular client.

When a client invokes the method of a stateless bean, the bean's instance variables may contain a state, but only for the duration of the invocation. When the method is finished, the state is no longer retained.

Page 5: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless vs. stateful session Beans

All instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client. => Stateless session beans can support multiple clients, and offer better scalability for applications that require large numbers of clients. Typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients.

Page 6: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless vs. stateful session Beans

The EJB container can write a stateful session bean to secondary storage.

However, stateless session beans are never written to secondary storage.

Therefore, stateless beans may offer better performance than stateful beans.

Page 7: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful session Beans – examples of use

•The bean needs to hold information about the client across method invocations.

•The bean mediates between the client and the other components of the application, presenting a simplified view to the client.

•Behind the scenes, the bean manages the work flow of several enterprise beans.

Page 8: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless session Beans – examples of use

•In a single method invocation, the bean performs a generic task for all clients. For example, you might use a stateless session bean to send an e-mail that confirms an online order.

•The bean fetches from a database a set of read-only data that is often used by clients.

Page 9: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless Example

Stateless Example: Euro Converter

Page 10: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless example – The Component (Remote) Interface

package statelessDemo;

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

public interface Converter extends EJBObject { public double convert(int lire);}

Page 11: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless example – The Home Interface

package statelessDemo;import java.rmi.*;import javax.ejb.*;

public interface ConverterHome extends EJBHome { public Converter create() throws RemoteException, CreateException;}

Page 12: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless example – The bean

package statelessDemo;import java.rmi.*;import javax.ejb.*;

public class ConverterBean implements SessionBean { private SessionContext sessionContext; public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } private static double ratio=1936.27; public double convert(int lire) { return lire/ratio; }}

Page 13: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless example – The descriptor

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd"><ejb-jar> <description>A simple demo of a stateless Bean</description> <display-name>An Euro Converter Bean</display-name> <enterprise-beans> <session> <description>A converter from Lire to Euro</description> <ejb-name>EuroConverter</ejb-name> <home>statelessDemo.ConverterHome</home> <remote>statelessDemo.Converter</remote> <ejb-class>statelessDemo.ConverterBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans>

Page 14: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless example – The descriptor

<assembly-descriptor> <container-transaction> <method> <ejb-name>EuroConverter</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> <ejb-client-jar /></ejb-jar>

Page 15: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless example – The client

import javax.ejb.*;import javax.naming.InitialContext;

public class ConverterClient { public ConverterClient() { } public static void main(String[] args) { try { InitialContext ctx=new InitialContext(); Object objref=ctx.lookup("L2EConverter");

statelessDemo.ConverterHome home = (statelessDemo.ConverterHome) javax.rmi.PortableRemoteObject.narrow( objref,statelessDemo.ConverterHome.class);

statelessDemo.Converter bean=home.create();

Get naming context and object reference

Cast to correct type

Get a bean instance from container

JNDI name

Page 16: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless example – The client

int lire=100000; System.out.println(lire+" Lire = "+

bean.convert(lire)+" Euro"); } catch (javax.naming.NamingException ex) { System.out.println("NamingException: "+ex); } catch (ClassCastException cc) { System.out.println(" ClassCastException : "+cc);} catch (javax.ejb.CreateException ce) { System.out.println("CreateException: "+ce); } catch (java.rmi.RemoteException re) { System.out.println("RemoteException: "+re); } }}

Do your business

Page 17: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless example – execution

RUN

100000 Lire = 51.64568990894865 Euro

Page 18: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful Example

Stateful Example: Dollar Converter

Page 19: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful example – The Component (Remote) Interface

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

public interface DollarConverter extends EJBObject {

public double convertInEuro(double dollar) throws java.rmi.RemoteException;

public double convertInDollar(double euro) throws java.rmi.RemoteException;

public void setRate(double euro_dollar_ratio) throws java.rmi.RemoteException;

}

Page 20: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful example – The Home Interface

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

public interface DollarConverterHome extends EJBHome { public DollarConverter create() throws RemoteException,

CreateException;}

Page 21: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful example – The bean

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

public class DollarConverterBean implements SessionBean { private SessionContext sessionContext; public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; }

Page 22: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful example – The bean

private double euro_dollar_ratio=1;

public double convertInEuro(double dollar) throws java.rmi.RemoteException{

return dollar/euro_dollar_ratio; }

public double convertInDollar(double euro) throws java.rmi.RemoteException{

return euro*euro_dollar_ratio; }

public void setRate(double euro_dollar_ratio) throws java.rmi.RemoteException{

this.euro_dollar_ratio=euro_dollar_ratio; }}

Page 23: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful example – The descriptor

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd"><ejb-jar> <description>A simple demo of a stateful Bean</description> <display-name>An Euro to Dollar Converter Bean</display-name> <enterprise-beans> <session> <description>A converter from Euro to Dollar</description> <ejb-name>DollarConverter</ejb-name> <home>DollarConverterHome</home> <remote>DollarConverter</remote> <ejb-class>DollarConverterBean</ejb-class> <session-type>Stateful</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans>

Page 24: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful example – The descriptor

<assembly-descriptor> <container-transaction> <method> <ejb-name>DollarConverter</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> <ejb-client-jar /></ejb-jar>

Page 25: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful example – The client

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

public class ConverterClient { public ConverterClient() {} public static void main(String[] args) { Object objref=null; DollarConverterHome home=null; DollarConverter bean=null; try { InitialContext ctx=new InitialContext(); objref=ctx.lookup("EDC"); home =(DollarConverterHome)PortableRemoteObject.narrow(

objref,DollarConverterHome.class);

bean=home.create();

Get naming context and object reference

Cast to correct type

Get a bean instance from container

JNDI name

Page 26: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful example – The client

bean.setRate(0.978);

double euro=1000; System.out.println(euro+" Euro = "

+bean.convertInDollar(euro)+" Dollar");

double dollar=1000; System.out.println(dollar+" Dollar = "

+bean.convertInEuro(dollar)+" Euro");

} catch (Exception e) { e.printStackTrace(); } }}

Do your business

Page 27: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful example – execution

RUN

1000.0 Euro = 978.0 Dollar1000.0 Dollar = 1022.4948875255624 Euro

Page 28: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Session Beans Lifecycle: client’s view

Does not existIs not referenced

ExistsIs not referenced

ExistsIs referenced

Does not existIs referenced

Start

home.create()

Clientreleasesreference

Crash,Timeout

Clientreleasesreference

Clientobtainshandle

object.remove(),home.remove(),Crash, timeout

Clientinvokesmethod

Client invokes method (NoSuchObject Exception)

Page 29: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateless session Beans Lifecycle

ExistsIn the pool

Does not exist

ejbRemoved()

Client invokes method

Client invokes method (NoSuchObject Exception)

1) newInstance()2) setSessionContext(sc)3) ejbCreate()

Client invokes create()Container executes: Client invokes remove()

Container executes:

Page 30: Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Stateful session Beans Lifecycle

Passive

ejbPassivate()

ejbActivate()

Exists, method ready

in the pool

Does not existClient invokes remove(), or timeout is reachedContainer executes:ejbRemoved()

Client invokes create()Container executes:1) newInstance()2) setSessionContext(sc)3) ejbCreate()

Client invokes non TX method

Exists, method ready

in TX

Client invokes TX method

Client invokes TX methodAfterBegin()

Client invokes commitbeforeCompletion()afterCompletion(true)

Client invokes rollbackafterCompletion(false)


Recommended