+ All Categories
Home > Documents > Middleware Technology (J2EE/EJB) Stateful Session Bean.

Middleware Technology (J2EE/EJB) Stateful Session Bean.

Date post: 29-Dec-2015
Category:
Upload: kevin-norton
View: 231 times
Download: 8 times
Share this document with a friend
Popular Tags:
21
Middleware Technology (J2EE/EJ B) Stateful Session Bean
Transcript
Page 1: Middleware Technology (J2EE/EJB) Stateful Session Bean.

Middleware Technology (J2EE/EJB)Stateful Session Bean

Page 2: Middleware Technology (J2EE/EJB) Stateful Session Bean.

2

Introduction to Session Beans

• Session Bean Lifetime

A session bean is a relatively short-lived component. The length of the client’s session generally determines how long a session bean is in use.

Example, order entry (See also: page 81)

The session beans are in-memory objects, they could not survive application server crashes, nor do they survive machine crashes. Non-persistent

The entity beans are persistent objects, they could live for months or even years.

Page 3: Middleware Technology (J2EE/EJB) Stateful Session Bean.

3

Session Bean Subtypes

• Stateful Session Bean

A stateful session bean is a bean that is designed to service business processes that span multiple method requests or transactions.

Stateful beans retain state on behalf of an individual client.

Examples: online shopping cart, banking application

Page 4: Middleware Technology (J2EE/EJB) Stateful Session Bean.

4

Session Bean Subtypes

• Stateless Session Bean

A stateless session bean is a bean that holds conversations that span a single method call and does not require to maintain it’s state across method invocations.

After each method call, it’s up to container to determine how to deal with instances, such as destroying it, or keeping it. (instance pool)

Examples: complex mathematical operation, credit card verification

Page 5: Middleware Technology (J2EE/EJB) Stateful Session Bean.

5

Stateless Session Beans Pooling (equivalent, indistinguishable)

Page 6: Middleware Technology (J2EE/EJB) Stateful Session Bean.

6

Pooling with Stateful Session Beans

• When a client invokes a method on a bean, the client is starting a conversation with the bean, and the conversational state stored in the bean must be available for that same client’s next method request. Therefore, the container cannot easily pool beans and dynamically assign them to handle arbitrary client method requests. WHY

• Passivation and activation, quite familiar to OS virtual memory.

• LRU (Least Recently Used)HOW WHENAny bean involved in a transaction cannot be passivated until the transaction completes.

Page 7: Middleware Technology (J2EE/EJB) Stateful Session Bean.

7

WHAT should the container to preserve when performing passivation and activation in J2EE.

• Every member variable in a bean is considered to be part of the bean’s conversational state if the following apply:– The member variable is a nontransient primitive type– The member variable is a nontransient Java object

• References to container-implemented objects– EJB object reference– Home object reference– EJB context reference– JNDI naming context

Page 8: Middleware Technology (J2EE/EJB) Stateful Session Bean.

8

instance pool

Client 1

Client 2

Client 3

Client 4

Bean Instance 1

Bean Instance 2

Bean Instance 3

state 1

state 2

state 3

state 4

poolSize=3

Page 9: Middleware Technology (J2EE/EJB) Stateful Session Bean.

9

Activation and Passivation

• Object SerializationThe container use object serialization to convert the bean’s conversational state. (bit-blob -- state)

• ejbPassivateThe container informs the bean that it’s about to perform passivation by calling the bean’s required ejbPassivate() callback method, so that the bean can relinquish held resources.

• ejbActivateThe container informs the bean that it’s about to perform activation by calling the bean’s required ejbActivate() callback method, so that the bean can restore the resources which it released during passivation process.

• ResourceDatabase Connection, Socket Connection, Open Files

Page 10: Middleware Technology (J2EE/EJB) Stateful Session Bean.

10

Passivation of a stateful bean

Page 11: Middleware Technology (J2EE/EJB) Stateful Session Bean.

11

Activation of a stateful bean

Page 12: Middleware Technology (J2EE/EJB) Stateful Session Bean.

12

A Simple Stateful Session Bean

• the Required Methods for Session Bean Classes

setSessionContext()

ejbCreate...(...)

ejbPassivate()

ejbActivate()

ejbRemove()

(See also: J2EE/EJB API)

Page 13: Middleware Technology (J2EE/EJB) Stateful Session Bean.

13

Page 14: Middleware Technology (J2EE/EJB) Stateful Session Bean.

14

Java Sources

(Demo 1: lec7/StatefulSession/readme.txt)• the Remote Interface

(Count.java)

• the Bean Class (CountBean.java)

• the Home Interface (CountHome.java)

(Demo 2: lec7/SessionCount, JSP how to invoke Stateful Session Beans)

Page 15: Middleware Technology (J2EE/EJB) Stateful Session Bean.

15

The life cycle of a stateless session bean

Page 16: Middleware Technology (J2EE/EJB) Stateful Session Bean.

16

The life cycle of a stateful session bean

Page 17: Middleware Technology (J2EE/EJB) Stateful Session Bean.

17

EJB 3.0

• Annotation JNDI Binding

Page 18: Middleware Technology (J2EE/EJB) Stateful Session Bean.

18

Local Binding

• @Stateless

• @LocalBinding(jndiBinding="custom/MySession") public class MySessionBean implements MySession { }

Page 19: Middleware Technology (J2EE/EJB) Stateful Session Bean.

19

Remote Binding

• @Stateless

• @RemoteBinding(jndiName="custom/remote/MySession")

• public class MySessionBean implements MySession { }

Page 20: Middleware Technology (J2EE/EJB) Stateful Session Bean.

20

EJB 2.x vs. EJB 3.0

• EJB 2.x– (1) Bean class 与 Remote/Home 接口没有继承关系– (2) CountHome home =

(CountHome)PortableRemoteObject.narrow(

ctx.lookup("CountHome"), CountHome.class);

Count count = home.create(countVal);– (3) EJB module .jar META-INF/ejb-jar.xml

– (4) Web module .war WEB-INF/web.xml

– (5) J2EE app .ear

– (6) Bean class 必须实现 SessionBean 接口,所以代码中有很多 ejbXXX 方法

Page 21: Middleware Technology (J2EE/EJB) Stateful Session Bean.

21

EJB 3.0

• (1) 类与接口有了明确的继承关系• (2) Bean class 没有实现 SessionBean 接口,而是• 通过标注 @Stateful 告诉容器,所以没有 ejbXXX 方法• (3) 没有 Home 接口,所以也没有 create 方法了• (4) 没有 JNDI 定义,默认为 ejbBeanName/remote

ShoppingCartBean/remote

ejbBeanName/local

• (5) lookup 方法直接类型转换,无须 narrow


Recommended