Professional Documents
Culture Documents
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
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
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
V d
Hnh nh mt session bean h tr ngi dng tham gia u gi: gm 2 phn
interface implementation class
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
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); }
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
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);