You are on page 1of 33

EJB Introduction & Session Bean

o Anh Tun datuan@fit.hcmuns.edu.vn

EJB Introdution nh ngha


Enterprise JavaBeans (EJB) is a platform for building portable, reusable, and scalable business applications using the Java programming language. From a developers point of view, an EJB is a piece of Java code that executes in a specialized runtime environment called the EJB container, which provides a number of component services

Reusable: For example, you can implement the credit cardcharging module as an EJB component that may be accessed by multiple applications Main strength: Multithreading

Cc dch v EJB h tr

Vai tr EJB trong m hnh layers

V tr ca EJB

EJB Types:
Consists of three types:
Session Beans Message-Driven Beans Entity Beans

Trong Session v Message-Driven Beans m nhim vai tr tng Business, Entity Beans m nhim vai tr tng Persistence

J2EE Container
EJB ch c th thc thi trong cc EJB Container J2EE container cung cp cho ngi dng nhiu dch v cao cp khc nhau Trong phm vi mn hc: S dng server JBoss

Bussiness Logic with Session Bean


Mt ng dng thng bao gm rt nhiu nghip v: Kim tra ti khon, rt tin, xut ha n Session Bean ng vai tr ci t cc chc nng nghip v cho ng dng. Ni mt cch n gin: Session bean bao gm mt tp cc hm cho php client truy xut hon thnh mt cng vic no . Cc client c th l mt desktop application, jsp/servlet hoc c th l mt ng dng .NET truy xut theo c ch web service Cc hm ca session bean c th c truy xut t xa, khng nht thit phi nm trn my cc b -> ng dng phn tn (distributed)

Advantages:
Mt s im mnh khi dng session bean: concurrency and thread safety: c th x l nhiu client cng lc m khng ci t cc c ch c bit (do container qun l) remote invocation: c th c triu gi t xa transaction and security management

Cu trc mt session bean:


Gm 2 phn: Bean interfaces v bean implementation class. Client khng c php truy xut trc tip n implementation class m phi truy xut thng qua interface. Interface s triu gi hm tng ng ca implemtation class v tr kt qu cho client.

V d
Hnh nh mt session bean h tr ngi dng tham gia u gi: gm 2 phn
interface implementation class

Phn loi Session Bean


Chia lm 2 loi:
Stateless: Khng lu tr bt k thng tin no t pha client Stateful: Lu gi thng tin (hay cn gi l state) ca client

V d minh ha
Xy dng mt stateless session bean h tr vic cng hai s lp (vi trm ch s - lp BigInteger trong Java) Hm cung cp cho user triu gi t xa c dng BigInteger add (BigInteger a, BigInteger b)

Xy dng Interface
C 3 loi interface:
Local: Client v Bean nm trn cng mt JVM Remote: Client c th nm xa Bean (khc my tnh) Web Service: Triu gi theo c ch web service

Khai bo cc hm cam kt s h tr cho user

V d v remote service
S dng annotation @Remote (nu l local interface s s dng @Local) Ch khai bo hm
import java.math.BigInteger; import javax.ejb.Remote; @Remote public interface AdderRemote { BigInteger add(BigInteger a, BigInteger b); }

Local Interface:
Cng ch khai bo hm
import java.math.BigInteger; import javax.ejb.Local; @Local public interface AdderLocal { BigInteger add(BigInteger a, BigInteger b); }

Stateless Bean class:


L ni ci t hm c th: @Stateless Implements cc interface(s) Mt session bean c th chn ch c remote/local interface hoc c 2.
import javax.ejb.Stateless; @Stateless public class AdderBean implements AdderRemote, AdderLocal { public BigInteger add(BigInteger a, BigInteger b) { return a.add(b); } }

Life Cycle

Stateless: Do khng lu tr thng tin ca client, mt stateless session bean c th phc v bt k client no. Container to sn mt s instance v t trong POOL S lng beans c th t hn s client

Lifecycle - Stateless
Stateless beans ch c 3 trng thi: Bean cha c to ra Container to ra cc beans v t vo pool (idle beans) Khi c 1 client triu gi, ly bean ra khi pool, thc thi lnh (busy) Nu client khng c nhu cu s dng -> tr v pool (idle)

Cc hm callback
C 2 trng hp c bit: Hm cn gi sau khi bean va c khi to: Dng k hiu @PostConstruct trc khai bo hm Hm cn gi trc khi bean c hy: Dng k hiu @PreDestroy trc khai bo hm Khi khng c nhu cu s dng, cc beans c t trong POOL. S lng beans trong POOL s c Container duy tr mc hp l

V d

Life Cycle Stateful Session Beans


Mi khi c 1 client triu gi, s phi pht sinh mt session bean ng vi client .

Cc chng nng pht sinh


Passivation: Khi s lng beans qu nhiu, container c nhu cu ct cc beans ng vi cc client lu khng lin lc li vi container vo a cng Activation: Khi cc client ny lin lc li vi container, phi phc hi bean t a cng tr li b nh Remove: Khi client ngt hon ton lin lc vi container, hy session bean ng vi client v khng th s dng li.

Cc hm callback
@PostConstruct @PreDestroy @PrePassivate @PostActivate @Remove (khng phi hm call back): khi gi hm c nh du remove, client bo cho container bit mnh khng c nhu cu s dng na v bean s b hy.

Cc lut:
a session bean must have at least one business interface The session bean class must be concrete. You cannot define a
session bean class as either final or abstract since the container needs to manipulate it. You must have a no-argument constructor in the bean class Business method names must not start with ejb. You must define all business methods as public, but not final or static If you are exposing a method in a remote business interface of the EJB, then make sure that the arguments and the return type of the method implement the java.io.Serializable interface

Xy dng client
Gi session bean t servlet: Cc bc: S dng JNDI lookup Session bean Interface Gi hm ca bean Interface Container s t ng gi hm tng ng ca bean class. Client khng bao gi tip xc trc tip vi bean class

V d:
Qu trnh lookup: Mc nh chui lookup l BeanName/remote hoc BeanName/local
try { Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); properties.put(Context.PROVIDER_URL, "localhost:1099"); Context ctx = new InitialContext(properties); AdderLocal test=(AdderLocal)ctx.lookup("AdderBean/local");

Lu :
Import cc gi cn thit Import package cha cc interface ca ejb import java.util.*; import javax.naming.*; import ejb.*;

Gi hm t interface
Context ctx = new InitialContext(properties); AdderLocal test=(AdderLocal)ctx.lookup("AdderBean/local"); BigInteger a=new BigInteger("12345"); BigInteger b=new BigInteger("56576"); BigInteger c=test.add(a,b); out.println("c="+c);

You might also like