²é¿´: 550  |  »Ø¸´: 4
µ±Ç°Ö÷ÌâÒѾ­´æµµ¡£
µ±Ç°Ö»ÏÔʾÂú×ãÖ¸¶¨Ìõ¼þµÄ»ØÌû£¬µã»÷ÕâÀï²é¿´±¾»°ÌâµÄËùÓлØÌû

wswswws

Òø³æ (ÕýʽдÊÖ)

[½»Á÷] ¡¾»î¶¯3¡¿EJB¼¼Êõ¼ò½é

EJB¼¼Êõ¼ò½é

EJBµÄÈ«³ÆÊÇEnterprise java bean¡£ÊÇJAVAÖеÄÉÌÒµÓ¦ÓÃ×é¼þ¼¼Êõ¡£EJB½á¹¹ÖеĽÇÉ« EJB ×é¼þ½á¹¹ÊÇ»ùÓÚ×é¼þµÄ·Ö²¼Ê½¼ÆËã½á¹¹£¬ÊÇ·Ö²¼Ê½Ó¦ÓÃϵͳÖеÄ×é¼þ¡£Ò»¸öÍêÕûµÄ»ùÓÚEJBµÄ·Ö²¼Ê½¼ÆËã½á¹¹ÓÉÁù¸ö½ÇÉ«×é³É£¬ÕâÁù¸ö½ÇÉ«¿ÉÒÔÓɲ»Í¬µÄ¿ª·¢ÉÌÌṩ£¬Ã¿¸ö½ÇÉ«Ëù×÷µÄ¹¤×÷±ØÐë×ñÑ­Sun¹«Ë¾ÌṩµÄEJB¹æ·¶£¬ÒÔ±£Ö¤±Ë´ËÖ®¼äµÄ¼æÈÝÐÔ¡£ÕâÁù¸ö½ÇÉ«·Ö±ðÊÇEJB×é¼þ¿ª·¢Õß(Enterprise Bean Provider) ¡¢Ó¦ÓÃ×éºÏÕß(Application Assembler)¡¢²¿ÊðÕß(Deployer)¡¢EJB ·þÎñÆ÷ÌṩÕß(EJB Server Provider)¡¢EJB ÈÝÆ÷ÌṩÕß(EJB Container Provider)¡¢ÏµÍ³¹ÜÀíÔ±(System Administrator)¡£

[ Last edited by »ÃÓ°ÎÞºÛ on 2006-11-18 at 07:40 ]
»Ø¸´´ËÂ¥

» ²ÂÄãϲ»¶

ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

cremaster

Òø³æ (СÓÐÃûÆø)

EJBµÄÈëÃŽ̲Ä
ÒýÓûØÌû:
1¡¢EJBµÄ¿ª·¢

ÏÈ·º·º¶øÂÛ£¬½²Ò»½²EJBµÄ¿ª·¢²½Öè¡£

1.1 SessionBeanµÄ¿ª·¢
µÚÒ»²½£¬ дԶ³Ì½Ó¿Ú£¨remote interface£©,¼Ì³ÐEJBObject½Ó¿Ú£¬°ÑÐèÒªµ÷ÓõÄpublic·½·¨Ð´ÔÚÀïÃæ(ÕâЩ·½·¨½«ÔÚSessionBeanÖÐʵÏÖ)£¬×¢ÒâÒªÉùÃ÷throws java.rmi.RemoteException¡£
ÀýÈ磺

package jsper.ejb;

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

public interface MyEJB extends EJBObject
{

public String sayHello() throws java.rmi.RemoteException;

}

µÚ¶þ²½£¬ дHome½Ó¿Ú£¨Éú³ÉEJBObjectÒýÓõÄfactory)
ÖÁÉÙÉú³ÉÒ»¸öcreate·½·¨, ×¢ÒâÒªÉùÃ÷throws java.rmi.RemoteExceptionºÍjavax.ejb.CreateException¡£
±ÈÈ磺



package jsper.ejb;

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


public interface MyEJBHome extends EJBHome
{

MyEJB create() throws java.rmi.RemoteException, javax.ejb.CreateException;

}

µÚÈý²½£¬ Ð´ÕæÕýµÄSession BeanµÄʵÏÖ(ʵÏÖ¶¨ÒåÔÚÔ¶³Ì½Ó¿ÚÖеķ½·¨)£¬ ÐèҪʵÏÖjavax.ejb.SessionBean½Ó¿Ú
×¢Ò⣺²»ÄÜÓÃimplents MyEJBµÄ·½Ê½Ö±½ÓʵÏÖÔ¶³Ì½Ó¿Ú,´Ë´¦²»ÓÃÅ׳öRemoteException
package jsper.ejb;

import java.rmi.RemoteException;
import javax.ejb.*;
public class MyEJBClass implements SessionBean {


¡¡ public MyEJBClass() {
¡¡ }
¡¡ //¶¨ÒåÔÚSessionBean Öеķ½·¨
¡¡ public void ejbCreate() throws RemoteException, CreateException {
¡¡ }

¡¡ public void ejbActivate() throws RemoteException {
¡¡ }

¡¡ public void ejbPassivate() throws RemoteException {
¡¡ }

¡¡ public void ejbRemove() throws RemoteException {
¡¡ }

public void setSessionContext(SessionContext ctx)
throws RemoteException {

¡¡ }

¡¡ //´Ë´¦ÊǾßÌåµÄʵÏÖ

¡¡ public String sayHello()

¡¡ {

¡¡¡¡ System.out.println("Hello";
¡¡ }
}
µÚËIJ½£¬Ð´Ò»¸ö·¢²¼ÓõÄÅäÖÃÎļþejb-jar.xml
ÐèÒªÌṩµÄÐÅÏ¢£º
Bean Home name -- The nickname that clients use to lookup your bean's home object.
Enterprise bean class name -- The fully qualified name of the enterprise bean class.
¡¡ Home interface class name
¡¡ Remote interface class name
Re-entrant -- Whether the enterprise bean allow re-entrant calls. This setting must be false for session beans(it applies to entity beans only)
¡¡ stateful or stateless
¡¡ Session timeout --¡¡The length of time (in seconds) before a client should time out when calling methods on your bean.
×îºóÄ㻹¿ÉÒÔÌṩÊôÓÚ×Ô¼ºµÄÅäÖÃÐÅÏ¢¹©×Ô¼º¿ØÖÆEJBµÄ¹¤×÷·½Ê½¡£

Àý×Ó£º

helloEjb
com.jsper.ejb.MyEJBHome
com.jsper.ejb.MyEJB
com.jsper.ejb.MyEJBClass
Stateless
Container



µÚÎå²½£¬½«ÄãµÄËùÓÐÎļþÓÃjar¹¤¾ßÉú³ÉjarÎļþ

ejb-jar.xmlÐëÔÚ¶¥¼¶µÄMETA-INF×ÓĿ¼
Õâ¾ä»°±È½ÏÒ§×죬 ¾Ù¸öÀý×Ó

mylib----META-INF--*.XML
|
|com--coucouniu--ejb---EJBClass
¡¡¡¡¡¡ |-EJBHome
¡¡¡¡¡¡ |-EJB

ÔÚÉú³É.jarÎļþʱ

sh>cd mylib¡¡¡¡//×¢Òâ´Ë´¦ËùÔÚĿ¼
sh>jar cv0f myejb.jar *

Çë×¢Ò⣺ µ½ÕâÒ»²½ÎÒÃÇ×ö³öµÄ¶«Î÷¶¼ÊǺͺÍÌØ¶¨µÄEJB ServerÊÇÎ޹صģ¬ Ö»ÊǺÍ×ñÑ­EJBµÄ±ê×¼ÓйØ

µÚÁù²½£¬Ê¹ÓÃÌØ¶¨Æ½Ì¨µÄ·¢²¼¹¤¾ßÉú³É·¢²¼Ê¹ÓõÄjarÎļþ¡£
²»Í¬µÄÖмä¼þ²úÆ·´Ë²½Öè·Ç³£²»Í¬£¬ ²úÉúµÄ½á¹û¶¼ÊÇÉú³ÉÖ»ÓÐ×Ô¼ºµÄEJB ServerÄÜÀí½âµÄÔ¶³Ì½Ó¿ÚºÍHome½Ó¿ÚʵÏֵȵȶ«Î÷£¬´ò°üÔÚÒ»¸öjarÎļþÖÐ
Ò»°ãÊǺܼòµ¥µÄ
µÚÆß²½£¬°Ñ.jarÎļþ·¢²¼µ½EJB Server

¸ù¾Ý²»Í¬µÄÖмä¼þ²úÆ·´Ë²½Öè·Ç³£²»Í¬£¬ ¿ÉÒÔ·ÖΪÆô¶¯Ê±·¢²¼ºÍÔËÐÐʱ·¢²¼Á½ÖÖ£¬Ò»°ãÊǺܼòµ¥µÄ, ÒÔweblogicΪÀý£º
1¡¢ÔÚweblogic.properties ÎļþÖÐÅäÖÃʹweblogic Æô¶¯Ê±×Ô¶¯×°ÔØ¡£
Ìí¼ÓÒ»¸öÌõÄ¿±ÈÈ磺
weblogic.ejb.deploy=C:/weblogic510/myserver/ejb_basic_beanManaged.jar£¬\
¡¡¡¡ C:/weblogic510/myserver/ejb_basic_test.jar

2¡¢Ê¹ÓÃdeploy»òDeployerTool¶¯Ì¬×°ÔØ/Ð¶ÔØ/¸üÐÂ

µÚ°Ë²½£¬Ð´¿Í»§¶ËµÄ³ÌÐò£¨ÎÒÆù½ñΪֹµÄÀí½â£©
ÔÚÎÒÃÇʹÓ÷¢²¼¹¤¾ß°ÑEJB·¢²¼µ½EJB ContainerµÄ¹ý³ÌÖУ¬»á°ó¶¨Ò»¸öÃû×Öµ½ContainerµÄĿ¼·þÎñÖУ¬ÏÖÔÚÎÒÃÇÒªµ÷ÓÃʱ´ÓÕâ¸öĿ¼·þÎñÖаÑEJBHome¶ÔÏóÈ¡³ö£¬ ÕâÀï·ÖΪ´Ó±¾µØºÍÍⲿÁ½ÖÖÇé¿ö£º
Ò»ÖÖÊǿͻ§¶Ë±¾µØµ÷ÓÃEJB¡£ ±ÈÈçºÍEJBÒýÇæºÍServletÒýÇæÊÇÕûºÏÔÚͬһ¸öApplication ServerÖУ¬ Õâʱµ±Ò»¸öServletÒªµ÷ÓÃEJBʱÎÞÐëÑéÖ¤£¬¼´¿ÉµÃµ½EJBHome½Ó¿ÚµÄʵÏÖ
¡¡ Context ic = new InitialContext();
¡¡¡¡¡¡ System.out.println("Looking for the EJB published as 'hello'";
¡¡¡¡¡¡ com.jsper.ejb.MyEJBHome homeInterface = (com.jsper.ejb.MyEJBHome) ic.lookup(¡°hello¡±); //·¢²¼Ê±°ó¶¨µÄÃû×ÖÊÇhello
¡¡ ÕâÑù¾Í¿É´ÓĿ¼·þÎñÖеõ½Home½Ó¿ÚµÄʵÏÖ£¬ Ò²ÊÇÎÒÃÇ×î³£Óõķ½Ê½£¬ ¿ÉÒÆÖ²ÐԺܺÃ

Íⲿµ÷ÓõϰÊ×ÏÈÒª¾­¹ýÉí·ÝÑéÖ¤£¬
±ÈÈçOracle8i £º

String ejbUrl = "sess_iiop://localhost:2481:ORCL/test/MyEJB";
¡¡¡¡ String username = "scott";
¡¡¡¡ String password = "tiger";

¡¡¡¡ // Setup the environment
¡¡¡¡ Hashtable environment = new Hashtable();
¡¡¡¡ // Tell JNDI to speak sess_iiop
¡¡¡¡ environment.put(javax.naming.Context.URL_PKG_PREFIXES, "oracle.aurora.jndi";
¡¡¡¡ // Tell sess_iiop who the user is
¡¡¡¡ environment.put(Context.SECURITY_PRINCIPAL, username);
¡¡¡¡ // Tell sess_iiop what the password is
¡¡¡¡ environment.put(Context.SECURITY_CREDENTIALS, password);
¡¡¡¡ // Tell sess_iiop to use credential authentication
environment.put(Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN);
// Lookup the URL
¡¡¡¡ com.jsper.ejb.MyEJBHome homeInterface = null;
¡¡¡¡ try {
¡¡¡¡¡¡ System.out.println("Creating an initial context";
¡¡¡¡¡¡ Context ic = new InitialContext(environment);
¡¡¡¡¡¡ System.out.println("Looking for the EJB published as 'test/MyEJB'";
¡¡¡¡¡¡ homeInterface = (com.jsper.ejb.MyEJBHome) ic.lookup(ejbUrl);
¡¡¡¡ }
¡¡¡¡ catch (ActivationException e) {
¡¡¡¡¡¡ System.out.println("Unable to activate : " + e.getMessage());
¡¡¡¡¡¡ e.printStackTrace();
¡¡¡¡¡¡ System.exit(1);
¡¡¡¡ }
ÔÙ±ÈÈçweblogicµÄµ÷Ó÷½Ê½£º
try
{
¡¡¡¡ // Get an InitialContext
¡¡¡¡ String url="t3://localhost:7001";
¡¡¡¡ Properties h = new Properties();
¡¡¡¡ h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory";
¡¡¡¡ h.put(Context.PROVIDER_URL, url);

Context ctx =¡¡new InitialContext(h);

¡¡¡¡ System.out.println("Getting the EJBHome object¡­";
¡¡¡¡¡¡ com.jsper.ejb.EJBHome tmp= (com.jsper.ejb.EJBHome)ctx.lookup("hello";

//create three element array of COUNT object
¡¡¡¡ EJB ejb =tmp.create();
¡¡¡¡ System.out.println(ejb.sayHello());
}
catch(Exception e)
{
e.printStackTrace();
}
ÓÉÓں;ßÌåµÄĿ¼·þÎñ¡¢Ð­ÒéÏà¹Ø£¬ÎªÁË´ïµ½¿ÉÒÆÖ²µÄÄ¿µÄ£¬Ö»ºÃ¶à×öһЩ¹¤×÷£¬ÐÒºÃÒ»°ã²»ÐèÒª×öÕâЩ¹¤×÷¡£

3Â¥2005-07-02 23:38:56
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
²é¿´È«²¿ 5 ¸ö»Ø´ð

cremaster

Òø³æ (СÓÐÃûÆø)

0.5

¡ï ¡ï ¡ï ¡ï ¡ï ¡ï ¡ï ¡ï
yuefour(½ð±Ò+8):лл²¹³äÌÖÂÛ£¬3¸ö²¹³äÌù¸øÄã¼ÓÒ»ÆðÁË£¬^_^
ʲôÊÇEJB
ÒýÓûØÌû:
1.ʲôÊÇ EJB?
Ò»¸ö¼¼Êõ¹æ·¶£ºEJB ´Ó¼¼ÊõÉ϶øÑÔ²»ÊÇÒ»ÖÖ"²úÆ·"
EJB ÊÇÒ»ÖÖ±ê×¼ÃèÊöÁ˹¹½¨Ó¦ÓÃ×é¼þÒª½â¾öµÄ:
¿ÉÀ©Õ¹ (Scalable)
·Ö²¼Ê½ (Distributed)
ÊÂÎñ´¦Àí (Transactional)
Êý¾Ý´æ´¢ (Persistent)
°²È«ÐÔ (Secure)

2.Sun ¶Ô EJB µÄÆÚÍû
Ìṩһ¸ö±ê×¼µÄ·Ö²¼µÄ¡¢»ùÓÚ OO µÄ×é¼þ¼Ü¹¹
ÆÁ±Î¸´ÔÓµÄϵͳ¼¶¹¦ÄÜÐèÇó
Write once, run anywhere
Óë·Ç Java Ó¦ÓÃÖ®¼äµÄ»¥²Ù×÷ÄÜÁ¦
¼æÈÝ CORBA ±ê×¼

3.ΪʲôѡÔñ EJB?

EJB ·þÎñÆ÷Íê³É"·±ÔÓ"µÄ¹¤×÷£ºÓ¦Óÿª·¢ÈËÔ±¹Ø×¢ÓÚÒµÎñÂß¼­µÄʵÏÖ¶ø²»ÊǵײãµÄʵÏÖ»úÖÆ(ÀàËÆÓÚ 4GL ÓïÑÔÉè¼ÆµÄÄ¿±ê)
Ö§³ÖÊÂÎñ´¦Àí
¶à¸öÒµÎñ²Ù×÷ͬʱ³É¹¦£¬»òÈ«²¿Ê§°Ü
¿ÉÒÔͨ¹ýÔÚ´úÂëÍâµÄÃèÊöÀ´¶¨ÒåÊÂÎñ´¦Àí¼¶±ð
¿ÉÀ©Õ¹ÐÔ
EJB ¿ÉÒÔ¸ù¾ÝÄúÓ¦ÓõÄÔö³¤¶øÀ©Õ¹
EJB ·þÎñÆ÷ÍùÍù»¹ÌṩÁ˸ºÔؾùºâºÍ
°²È«ÐÔ£ºÓÉ EJB ·þÎñÆ÷Ìṩ×ÊÔ´µÄ·ÃÎÊȨÏÞ¿ØÖÆ

4.EJB ¼Ü¹¹

ΪÁËÂú×ã¼Ü¹¹µÄÄ¿±ê£¬¹æ·¶ÖÐÃèÊöÁË
·þÎñÆ÷ (Server)
ÈÝÆ÷ (Container)
Àà (Class) ºÍʵÀý (Instance)
Home ºÍ Remote ½Ó¿Ú
¿Í»§¶Ë (Client)

5. ¼ò»¯µÄ±à³ÌÄ£ÐÍ

¹Ø×¢ÓÚÒµÎñÂß¼­ÊµÏÖ£ºEJB ¸ºÔðÉúÃüÖÜÆÚ (lifecycle), Êý¾Ý´æ´¢ (persistence), ÊÂÎñ´¦ÀíÓïÒå (transactional semantic), °²È«(security), ...
ͨÓõıà³ÌÄ£ÐÍ£º¸÷ÖÖ·þÎñµÄ¸ß²ã API
Java ÊÇÆä±à³ÌÓïÑÔ

1.EJB ÌØµã

ÓÉÒ»¸ö EJB ÈÝÆ÷ÔÚÔËÐÐʱ´´½¨ºÍ¹ÜÀí EJB
ÔÚ²¿Êð EJB ʱ¶¨ÖÆÆäÔËÐз½Ê½
ÓÉ EJB ÈÝÆ÷ºÍ·þÎñÆ÷À´Ð­µ÷¿Í»§¶ËµÄ·ÃÎÊ
¿ÉÒÔ²¿Êðµ½ÈκμæÈÝµÄ EJB ÈÝÆ÷ÖÐ
¿Í»§¶Ë¶Ô EJB µÄÊÓͼÊÇÓÉ Bean ¿ª·¢ÈËÔ±¾ö¶¨µÄ

2.EJB ·þÎñÆ÷

¹ÜÀí EJB ÈÝÆ÷ (Ëü¹ÜÀí Bean)
Ìṩ¶Ô²Ù×÷ϵͳ·þÎñµÄ´æÈ¡
Ìṩ Java Ïà¹ØµÄ·þÎñ£¬ÓÈÆäÊÇ
ͨ¹ý JNDI ·ÃÎÊÃüÃû¿Õ¼ä
»ùÓÚ OTS µÄÊÂÎñ´¦Àí·þÎñ

3.EJB ÈÝÆ÷

¹ÜÀí Bean ÉúÃüÖÜÆÚ£º½« EJB ·þÎñÆ÷ÌṩµÄ·þÎñ´«µÝ¸ø Bean
Éú³É´úÂëÀ´ÊµÏÖ¶Ô Bean µÄ´æÈ¡·ÃÎÊ
Ç¿ÖÆÊÂÎñ´¦ÀíµÄÏÞÖÆ
´´½¨¡¢³õʼ»¯ºÍ»ØÊÕ Bean
¹ÜÀí³Ö¾ÃÊý¾ÝµÄ´æ´¢
¶Ô¿Í»§¶Ë¶øÑÔ EJB ÈÝÆ÷ÊÇ͸Ã÷µÄ

4.ÔÚÒ»¸ö EJB ·þÎñÆ÷ÖеÄÈÝÆ÷

ĿǰÈÝÆ÷ͨ³£ÊÇÓÉ EJB ·þÎñÆ÷±¾ÉíÌṩµÄ
ÔÚ EJB 1.0 »ò 1.1 ¹æ·¶ÖÐûÓж¨ÒåÈÝÆ÷-µ½-·þÎñÆ÷µÄ½Ó¿Ú
¸÷³§ÉÌ¿ÉÒÔ¸ù¾ÝËûÃǵļû½âÀ´ÊµÏÖ·þÎñÆ÷ºÍÈÝÆ÷µÄ¸÷×ÔÔðÈÎ

5.ÈÝÆ÷Ìṩ·þÎñ: Êý¾Ý´æ´¢

ÈÝÆ÷¾ö¶¨ºÎÊ±ÔØÈë/´¢´æ×´Ì¬
Container-Managed Persistence(ÈÝÆ÷¹ÜÀí´æ´¢/CMP)
ÈÝÆ÷¸ºÔð´æ´¢ÄúµÄ Bean
ÈÝÆ÷Éú³É±ØÒªµÄÀàºÍ´úÂë
Bean-Managed Persistence(Bean ¹ÜÀí´æ´¢/BMP)
Bean ¿ª·¢ÈËÔ±Ìṩ´æ´¢´úÂë
¿ª·¢ÈËÔ±¾ö¶¨ ÈçºÎ´æ´¢, ÈÝÆ÷ÈÔÈ»¾ö¶¨ ºÎʱ½øÐÐ

6.ÈÝÆ÷Ìṩ·þÎñ: ÊÂÎñ´¦Àí

¿ÉÒÔÓÉÈÝÆ÷´úÀíÀ´ÊµÏÖ
ÈÝÆ÷½«µÃµ½ÒµÎñÂß¼­·½·¨µÄÊÂÎñ´¦ÀíÐèÇó
ÈÝÆ÷ÌṩÊÂÎñ¿ØÖÆ´úÂë
Ò²¿ÉÒÔÓɳÌÐòԱͨ¹ý´úÂëʵÏÖ

7.ÈÝÆ÷Ìṩ·þÎñ: ÆäËü·þÎñ

ÆäËü·þÎñ°üÀ¨
̟̞ (Naming)
°²È« (Security)
Ï̹߳ÜÀí (Thread management)
ÕâЩ·þÎñÓÉÈÝÆ÷´úÀíÍê³É½«¼õÉÙÓ¦Óÿª·¢ÈËÔ±µÄ¸ºµ£


8.·Ö²¼Ê½¶ÔÏóÔËËã

Ô¶³Ì¶ÔÏó±»×÷Ϊ±¾µØ¶ÔÏóÀ´´¦Àí£º´«µÝÐÅÏ¢µÄ·½Ê½²»±ä£¬µ«¿ªÏú¸ü´ó
Enterprise JavaBeans ÓÀÔ¶ÔËÐÐÔÚ·þÎñÆ÷ÉÏ£º¶Ô Bean µÄ·ÃÎÊÓÀÔ¶ÊÇÔ¶³Ìµ÷ÓÃ

9.Stub ºÍ Skeleton

ÓÉ EJB Éú³É:
"Stub" ¶ÔÒª´«µÝ³öÈ¥µÄÐÅÏ¢±àÂë
"Tie/Skel" ½«½ÓÊܵ½µÄÐÅÏ¢½âÂë²¢´«µÝ¸øÄ¿±ê¶ÔÏó

10.·ÖÀà: Enterprise JavaBeans

+---Entity Beans--CMP/BMP
Ejb--|
+---Session Beans--Stateful/Stateless

»á»° Bean (Session Bean)£º¸ù¾Ý EJB ¹æ·¶£¬Ò»¸ö»á»° Bean ÊÇ:

´ú±íµ¥¸ö¿Í»§¶ËÀ´Ö´ÐÐ
¿ÉÒÔ²ÎÓëµ½ÊÂÎñ´¦ÀíÖÐ
²»Ö±½Ó´ú±í¹²ÏíÓÚÊý¾Ý¿âÖеÄÊý¾Ý£¬µ«ËüÄÜ·ÃÎʺ͸üÐÂÕâЩÊý¾Ý
Ïà¶Ô¶øÑÔÊǶÌÔÝ´æÔÚµÄ
µ± EJB ÈÝÆ÷ʧЧºó¾Í²»´æÔÚ£­£­£­¿Í»§¶ËÐèÒªÖØÐ½¨Á¢Ò»¸öÐÅеĻỰ¶ÔÏóÀ´¼ÌÐøÔËËã

ʵÌå Bean (Entity Bean)£º¸ù¾Ý EJB ¹æ·¶£¬Ò»¸öʵÌå Bean ÊÇ:

ÌṩÔÚÊý¾Ý¿âÖÐÊý¾ÝµÄ¶ÔÏóÊÓͼ
ÔÊÐí±»¶à¸öÓû§¹²Ïí´æÈ¡·ÃÎÊ
¿ÉÒÔÊdz¤ÆÚ´æÔÚ (Ö»ÒªËü´æÔÚÓÚÊý¾Ý¿âÖÐ)
ʵÌå Bean, ËüµÄÖ÷¼ü¶ÔÏó, ÒÔ¼°ËüµÄÔ¶³ÌÒýÓý«ÄÜ¿ç EJB ÈÝÆ÷µÄå´»ú¶ø´æÔÚ

11.EJB ÀàºÍʵÀý

¹¹½¨ EJB Ó¦ÓðüÀ¨À´×ÔÈý·½µÄ´úÂë
¿ª·¢ÈËÔ±±àдµÄ´úÂë
ÓÉ EJB API ¶¨ÒåµÄÀàºÍ½Ó¿Ú
ÓÉÈÝÆ÷×Ô¶¯Éú³ÉµÄ´úÂë
¿ª·¢ÈËÔ±±àдµÄ´úÂë°üÀ¨
Bean Àà (¶¨ÒåÁËÒµÎñÂß¼­)
Home ½Ó¿Ú (ÈçºÎ²éÕÒ»ò´´½¨ bean)
Remote ½Ó¿Ú (ÈçºÎ´æÈ¡ bean)
ÆäËü×é¼þ£¬¸ù¾Ý bean ʵ¼ÊÒªÇó

12.EJB Home ½Ó¿Ú

ÿ¸ö bean ÓÐÒ»¸ö
ÓÃÓÚ:´´½¨Ð嵀 bean ʵÀý¡¢²éÕÒÏÖ´æµÄ bean (Ö»ÄÜÊÇʵÌå bean)

Remote ½Ó¿Ú£º¶¨Òå bean µÄ¹«¹²½Ó¿Ú£­£­£­Ö»ÓÐÔÚ Remote ½Ó¿ÚÖж¨ÒåµÄ·½·¨²ÅÄܱ»¿Í»§¶Ë·ÃÎÊ

EJB ¿Í»§¶Ë

¿ÉÒÔΪ servlet, JSP, Ó¦ÓóÌÐò»òÆäËü bean
ͨ¹ý JNDI À´²éÕÒ EJB home ½Ó¿Ú£¬²½ÖèΪ:
´´½¨Ò»¸ö JNDI Context (initial context)
ʹÓà JNDI Context À´²éÕÒ bean home ½Ó¿Ú
ʹÓà bean home ½Ó¿ÚÀ´´´½¨/²éÕÒ bean ʵÀý
ʹÓà bean ʵÀýÍê³ÉÒµÎñ²Ù×÷
ʵ¼ÊµÄ´æÈ¡ (¶Ô EJB) ÊÇͨ¹ýÈÝÆ÷Éú³ÉµÄÀàÀ´Íê³É

EJB ¼Ü¹¹

¿Í»§¶Ë¶Ô bean ·ÃÎÊÓÀÔ¶²»ÊÇÖ±½ÓµÄ
EJBObject (tie) ÊÇÓÉÈÝÆ÷×ÔÉíÌṩµÄ£ºÓÃÀ´°ïÖú¹ÜÀí bean µÄÉúÃüÖÜÆÚ

EJB ÖеĽÇÉ«

EJB ·þÎñÆ÷¹©Ó¦ÉÌ: ¿ª·¢²¢ÏúÊÛ EJB ·þÎñÆ÷
EJB ÈÝÆ÷¹©Ó¦ÉÌ: ¿ª·¢²¢ÏúÊÛ EJB ÈÝÆ÷
Enterprise bean ¿ª·¢ÈËÔ±: ¿ª·¢²¢ÏúÊÛ EJB
Ó¦ÓÃ×é×°ÈËÔ±: ½«²»Í¬µÄ EJB ´î½¨³ÉÓ¦ÓÃ
²¿ÊôÈËÔ±: ʹÓÃÏàÓ¦¹¤¾ßÔÚÔËÐл·¾³ÏÂÅäÖà EJB
ϵͳ¹ÜÀíÔ±: ¼àÊÓÔËÐÐʱÇé¿ö

***************
*Äúѧµ½ÁËÄÄЩ?*
***************
EJB Êǹ¹½¨½¡×³£¬¿ÉÀ©Õ¹²¢Ö§³ÖÊÂÎñ´¦ÀíµÄ·Ö²¼Ê½¶ÔÏó¼¼Êõ¹æ·¶
ÓÐÁ½ÖÖÀàÐ굀 EJB: Session Bean ºÍ Entity Bean
Ò»¸ö EJB ·þÎñÆ÷ʹÓà EJB ÈÝÆ÷£»ÈÝÆ÷À´¹ÜÀíÆäËù°üÈÝ bean µÄÉúÃüÖÜÆÚ
ÿ¸ö bean ½«ÓÐÈý¸öÀà: bean Àà, home ½Ó¿ÚºÍ remote ½Ó¿Ú

2Â¥2005-07-02 23:36:57
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

cremaster

Òø³æ (СÓÐÃûÆø)

Àí½âEJBµÄ²ÎÊý´«µÝ
ÒýÓûØÌû:
Understanding EJB argument passing
Scott Oaks
Scott Oaks is a Systems Engineer for Sun Microsystems, where he focuses on practical applications of Java technology. He is the co-author, with Henry Wong, of Java Threads.


TODAY´S TIP REGARDS a performance characteristic that is particularly important to certain Enterprise JavaBean (EJB) applications, although it has applicability to other types of Java applications as well. It revolves around the way in which EJBs (and, more generally, any distributed object system) pass arguments to each other; I´ll discuss how this task is accomplished and when you might want to change the manner in which it is done.
First, some terminology and a brief review. For the most part, arguments that are passed between two Java methods are passed by value. If you are given the code



int i = 3;
someMethod(i);

after invoking someMethod(), we know i will have the value 3, no matter what happens inside the method invocation. i is passed by value. If you are given the code


Point p = new Point(3, 3);
someOtherMethod(p);

after invoking someOtherMethod(), we know p will reference the same object it referenced before the method invocation?the memory location it points to is the same. But the contents of that memory may have changed. If the method calls its parameter q and executes the statement q.x = 4, then when the method returns, p will refer to the point with coordinates (4, 3). In this case, p is passed by reference.
I´ve had semantic arguments with developers who insist that because p points to the same memory location all along, it too is passed by value. If the method executes the statement q = new Point(4, 3), then when the method returns, p will refer to the original point, which will still have the coordinates (3, 3). No matter what you choose to call it, this is the way Java works.

The point is that the contents of an object can change when it is passed between methods in Java. The exception to this rule occurs with RMI and other distributed programming technology. In that case, the two methods in question are running on two different virtual machines. To get the object from one method to another, the object is serialized by the client and deserialized by the server. A side effect of this deserialization is that a copy of the object has been made, and no matter what changes are made to the contents of that object in the server, the contents of that object on the client will not change. In this case, the objects are said to be passed by value (though in those semantic wars, some prefer the term "passed by copy".

This difference in programming semantics takes a little getting used to, but it has a rationale. Clearly, a copy of the object has to be made in the server´s virtual machine as it cannot share physical memory with the client´s virtual machine. The standard Java object semantics could have been preserved if the copy of the object was sent back to the client and somehow overlaid in memory on top of the original object. Such a trick would have been complex and would often have had a severe impact on performance. If the object hasn´t changed, it´s a waste of time to copy it back. It´s far better for developers to be in control of any data that needs to be sent back to the client (and to send it, for example, in the return object).

This brings us to EJBs: a method invocation on an EJB is an RMI call. It is a requirement of the EJB specification that parameters passed during the method call be passed by value so that the EJB receives a copy of any object parameters (and the caller receives a copy of the return object, if applicable). Thus, your EJB client can assume that the contents of an object passed to an EJB server are not changed by the server.

As it turns out, EJBs that call each other are often deployed within the same virtual machine. Think of a session bean that needs to use multiple entity beans?all of the beans are often deployed within the same virtual machine. In this case, the object serialization is not strictly necessary. The client and server both have reference to the original object. But if the server (the entity bean) changes the contents of the parameter object, the client (the session bean) will see those changes. Later, if for scaling (or other) reasons the entity beans are moved onto a different machine, the session beans will no longer see any changes to the content of the parameter object. It would be untenable to have different semantics depending on how the application is deployed; that is why all parameters between EJBs are always passed by value, even when they are within the same virtual machine.

Most application servers are aware of the performance penalty paid by making an unnecessary copy of the object passed between EJBs within the same virtual machine and have an option that can be set to have these objects passed by reference (thereby avoiding the copy). If you turn on that option for an arbitrary EJB application, it is possible that the application will break. If the application is written to assume pass-by-value semantics, then the unexpected change in data contents could have a detrimental effect.

On the other hand, if you write your EJBs so that they make no assumption about these semantics, then you´re ahead of the game. You can gain the performance benefit from passing by reference when the EJBs are deployed within the same virtual machine, and you can still safely deploy them on multiple machines. What this means is that the EJB must never modify any object it receives as a parameter. If it returns an object, it must return a new instance of the object so that it is not inadvertently shared.

The feasibility of this will depend, of course, on your application. I´ve seen EJB applications where enabling pass-by-reference semantics produced a 25% benefit, so if you have an application that makes a large number of EJB calls within the same virtual machine, a little forethought in application design might be to your benefit.

4Â¥2005-07-02 23:41:43
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

cremaster

Òø³æ (СÓÐÃûÆø)

Ìá¸ßEJBÐÔÄܵÄÊ®´ó¼¼ÇÉ
ÒýÓûØÌû:
EJBµ÷ÓÃÊǺÄʱ¡¢·ÑÁ¦µÄ¡£ÔõôÌá¸ßEJBµÄÐÔÄÜ£¿ÎÒÃÇΪ½â¾öÕâÒ»ÎÊÌ⣬¿ªÊ¼±ßд±ßÌÖÂÛ¡£µÈµ½Íê³ÉÁË£¬ÎҲŷ¢ÏÖ£¬ÎÒÃÇËùÓ¦Óõļ¼ÇÉ×ܽáһϣ¬¾¹ÓÐÊ®Ìõ¡£°ÑÌá¸ßEJBÐÔÄܵÄÕâЩ¼¼ÇÉ×ܽáһϣ¬ÎªÒÔºóµÄÏîÄ¿×ö²Î¿¼¡£

1.ÓÃÒ»¸öSession Bean·â×°¶à¸öEntity Bean£¬½«Ô­À´µÄ¶à¸öEntity BeanµÄRemoteµ÷ÓúÍLocalµ÷Ó÷â×°ÔÚÒ»¸öSession BeanÖС£ËùÒÔ½¨Á¢Ò»¸öServerFacade£¬ËüΪ¶à¸ö¶ÔÏóÌṩͳһ»ñÈ¡EJB HomeºÍ»ñÈ¡¶ÔÏóµÄ½Ó¿Ú¡£ServerFacadeΪ³ÌÐòÒªÓõ½µÄËùÓÐEJBµÄhome handleÌṩ»º´æ£¬Ìá¸ß·ÃÎÊJNDI NameµÄʱ¼ä£¬´ïµ½Ìá¸ß·ÃÎÊЧÂʵÄÄ¿µÄ¡£ÒÔºó²éÕÒJNDI NameµÄ·½·¨¶¼Ó¦Ð´ÔÚ½Ó¿ÚÀµ÷ÓÃʱֱ½Ó´Ó½Ó¿Úµ÷Óá£

2.ÔÚEJBµÄRemote½Ó¿ÚÖÐʹÓôÖÁ£¶ÈµÄ·½·¨£¬²»ÍƼöʹÓÃϸÁ£¶È·½·¨¡£

3.Èç¹ûEJBµÄRemote½Ó¿Ú»ñÈ¡³É¹¦£¬Ó¦²»ÔÙʹÓÃRemote½Ó¿Ú£¬¶øÊǽ«Remote½Ó¿Ú¹¹Ôì³ÉÒ»¸öÒ»°ãµÄJAVA¶ÔÏó£¬Í¨¹ýµ÷ÓÃÒ»°ãµÄJAVA¶ÔÏóµÄ·½·¨À´´ïµ½¼õÉÙ¶ÔÍøÂçµÄ·ÃÎÊ¡£

4.Èç¹ûÄ㲿ÊðEJB¿Í»§¶ËºÍEJBÔÚÏàͬµÄJVMÉÏ£¬½¨ÉèʹÓÃEJB2.0¹æ·¶µÄLocal½Ó¿Ú´úÌæRemote½Ó¿Ú¡£

5.ÓÃ"transient"¹Ø¼ü×ÖÉùÃ÷²»±ØÒªµÄÊý¾Ý±äÁ¿£¬Ìæ´úÒÔǰµÄ"public"¡¢"private"µÈ£¬±ÜÃâ²»±ØÒªµÄÊý¾Ý±äÁ¿Õ¼ÓÃÍøÂç×ÊÔ´¡£Ê¾Àý£º

public class DemoCMP implements EntityBean {
transient EntityContext entCtx;
transient InitialContext initCtx;
public String id;
public String description;
¡­
}

6.ÔÚejb-jar.xml²¿ÊðÎļþÖУ¬¶ÔSession BeanÖзÇÊÂÎñµÄ·½·¨£¬½«trans-attributeÊôÐÔ¸³Îª"NotSupported"»ò"Never"


¡­



abookesessionBean
*

NotSupported




7. ÉèÖÃÊÂÎñµÄ³¬Ê±Ê±¼ä£¬ÔÚJBossÖУ¬ÒªÐÞ¸Ä${jboss.home}/server/${jboss.configuration}/conf/jboss-service.xml £¬ÈçÏÂËùʾ£º


¡­
name="jboss:service=TransactionManager">
300

¡­


8.µ±ÊÂÎñËø¶¨Êý¾Ý¿âµÄÐмǼʱ£¬ÊÂÎñÓ¦¿çÔ½¿ÉÄܵÄ×îСµÄʱ¼ä¡£

9.µ÷ÕûEJB ·þÎñÆ÷µÄ¸÷ÖÖ²ÎÊý£¬ÈçÏß³ÌÊý¡¢EJB³Ø´óС¡¢Á¬½Ó³Ø²ÎÊýµÈ¡£ÒÔÔÚJBossÐÞ¸ÄÁ¬½Ó³Ø²ÎÊýΪʾÀý£¬½øÐÐ˵Ã÷¡£Èç¹ûJBossºÍMysqlÏàÁ¬£¬ÅäÖÃ${jboss.home}/server/${jboss.configuration}/deploy/mysql-service.xml£¬À´ÐÞ¸ÄÁ¬½Ó³Ø²ÎÊý£¬°üÀ¨MinSize¡¢MaxSize¡¢BlockingTimeoutMillis¡¢IdleTimeoutMinutes¡¢CriteriaµÈ£¬¸÷²ÎÊýµÄº¬ÒåÈçÏÂËùʾ£º

i.MinSize £ºÁ¬½Ó³Ø±£³ÖµÄ×îСÁ¬½ÓÊý¡£

ii. MaxSize £ºÁ¬½Ó³Ø±£³ÖµÄ×î´óÁ¬½ÓÊý¡£

iii. BlockingTimeoutMillis £ºÅ׳öÒ쳣ǰ×î´óµÄµÈ´ýÁ¬½Óʱ¼ä¡£

iv. IdleTimeoutMinutes £º¹Ø±ÕÁ¬½ÓǰÁ¬½Ó¿ÕÏеÄ×î´óʱ¼ä¡£

v. Criteria £ºÓÐByContainerAndApplication¡¢ByContainer¡¢ByApplicationºÍByNothingµÈÖµ¡£

ÏÂÃæÊÇÒ»¸öÀý×Ó£º



name="jboss.jca:service=LocalTxPool,name=MySqlDS">
0
50
5000
15
ByContainer



10.¶ÔÓÚÊý¾Ý¿âÊÂÎñ£¬Ó¦Ñ¡Ôñ½ÏµÍ³É±¾µÄÊÂÎñµÈ¼¶£¬±ÜÃâÔì³É»µÊý¾Ý¡£µÝÔö³É±¾µÄÊÂÎñµÈ¼¶°üÀ¨£º

TRANSACTION_READ_UNCOMMITED,
TRANSACTION_READ_COMMITED,
TRANSACTION_REPEATABLE_READ,
TRANSACTION_SERIALIZABLE

5Â¥2005-07-02 23:42:44
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[¿¼ÑÐ] µ÷¼Á²ÄÁÏѧ˶ +3 ´ÊÄýY 2026-03-02 3/150 2026-03-02 21:51 by ÑîÑîÑî×Ï
[¿¼ÑÐ] 085602»¯Ñ§¹¤³Ì350£¬µ÷¼Á£¬ÓÐûÓÐ211µÄ +4 ÀûºÃÀûºÃ. 2026-03-02 7/350 2026-03-02 21:46 by sunny81
[¿¼ÑÐ] 0856²ÄÁÏÓ뻯¹¤£¬270Çóµ÷¼Á +11 YXCT 2026-03-01 13/650 2026-03-02 21:38 by sunny81
[¿¼ÑÐ] 0856Çóµ÷¼Á285 +11 ÂÀ×ÐÁú 2026-02-28 11/550 2026-03-02 20:15 by hypershenger
[¿¼ÑÐ] ²ÄÁÏ284Çóµ÷¼Á£¬Ò»Ö¾Ô¸Ö£ÖÝ´óѧӢһÊý¶þר˶ +15 ÏëÉϰ¶µÄÍÁ²¦Êó 2026-02-28 15/750 2026-03-02 20:13 by hypershenger
[¿¼ÑÐ] Öйú¿ÆÑ§¼¼Êõ´óѧ²ÄÁÏÓ뻯¹¤281Çóµ÷¼Á£¬ÓпÆÑкͻñ½±¾­Àú +5 wsxw 2026-03-02 5/250 2026-03-02 20:12 by hypershenger
[¿¼ÑÐ] »¯Ñ§£¬²ÄÁÏ£¬»·¾³ÀàÇóµ÷¼Á +7 ¿¼Ñаæ°ô°ô 2026-03-02 7/350 2026-03-02 19:56 by hypershenger
[¿¼ÑÐ] ¿¼Ñи´ÊÔµ÷¼Á£¬¹ý¹ú¼ÒÏßµÄͬѧ¶¼¿É±¨Ãû +5 ºÚ£¡ÔÚ¸ÉÂï 2026-02-28 6/300 2026-03-02 19:30 by Dismas001
[¿¼ÑÐ] 283Çóµ÷¼Á +5 ¹ĭóÏ 2026-03-02 5/250 2026-03-02 19:19 by zhukairuo
[¿¼ÑÐ] Ò»Ö¾Ô¸ÖÐʯÓÍ£¨»ª¶«£©±¾¿ÆÆë³¹¤Òµ´óѧ +3 ʯÄÜΰ 2026-03-02 3/150 2026-03-02 18:54 by caszguilin
[¿¼ÑÐ] »¯¹¤¾©Çø271Çóµ÷¼Á +6 11ing 2026-03-02 6/300 2026-03-02 18:52 by caszguilin
[¿¼ÑÐ] »¶Ó­²É¿ó¡¢µØÖÊ¡¢ÑÒÍÁ¡¢¼ÆËã»ú¡¢È˹¤ÖÇÄܵÈרҵµÄͬѧ±¨¿¼ +6 pin8023 2026-02-28 8/400 2026-03-02 17:13 by 0854¶×µ÷¼Á
[¿¼ÑÐ] Ò»Ö¾Ô¸»ªÄÏÀí¹¤´óѧ²ÄÁÏÓ뻯¹¤326·Ö£¬Çóµ÷¼Á +3 wujinrui1 2026-02-28 3/150 2026-03-02 16:36 by chuocheng
[¿¼ÑÐ] 291 Çóµ÷¼Á +3 »¯¹¤2026½ì±ÏÒµÉ 2026-03-02 3/150 2026-03-02 12:55 by houyaoxu
[¿¼ÑÐ] ²ÄÁÏѧµ÷¼Á +10 ÌáÉñ¶¹É³°ü 2026-02-28 12/600 2026-03-02 09:26 by ÀîÀÏʦ£¡
[¿¼ÑÐ] 322Çóµ÷¼Á +3 Ðܾ³†´ 2026-03-01 3/150 2026-03-02 08:44 by houyaoxu
[¿¼ÑÐ] 279Çóµ÷¼Á +3 dua1 2026-03-01 4/200 2026-03-02 00:23 by ´óÁ³µ°×Ó
[¿¼ÑÐ] 299Çóµ÷¼Á +3 YÄ«Ã÷ÆåÃîY 2026-02-28 5/250 2026-03-01 21:01 by tangxiaotian
[¿¼ÑÐ] 328Çóµ÷¼Á +3 aaadim 2026-03-01 5/250 2026-03-01 17:29 by njzyff
[˶²©¼ÒÔ°] 2025½ìË«·Ç»¯¹¤Ë¶Ê¿±ÏÒµ£¬É격 +3 ¸ü¶àµÄÊÇ 2026-02-27 4/200 2026-03-01 10:04 by ztg729
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û