Java °æ (¾«»ªÇø)
·¢ÐÅÈË: bali (°¢±¼), ÐÅÇø: Java
±ê Ìâ: EJB 2.0 ÖÐÒýÈËעĿµÄ±ä»¯ÔöÇ¿ÁËÓ¦ÓóÌÐò¿ª·¢µÄÁé»îÐ
·¢ÐÅÕ¾: ¹þ¹¤´ó×϶¡Ïã (2000Äê12ÔÂ30ÈÕ01:04:09 ÐÇÆÚÁù), תÐÅ
È«ÃæÑжÁ EJB 2.0
EJB 2.0 ÖÐÒýÈËעĿµÄ±ä»¯ÔöÇ¿ÁËÓ¦ÓóÌÐò¿ª·¢µÄÁé»îÐԺͿÉÒÆÖ²ÐÔ
Richard Monson-Haefel
OpenEJB Ê×ϯÉè¼Æʦ
2000 Äê 6 ÔÂ
ÄÚÈÝ£º
CMP
³éÏóµÄ³Ö¾ÃÐÔ·½°¸
EJB 2.0 µÄ CMP Ä£ÐÍ
EJB ²éѯÓïÑÔ
Ð嵀 ejbHome ·½·¨
MessageDrivenBean
ʲôÊÇ JMS£¿
EJB 2.0 ÖÐµÄ JMS
×÷Ϊ EJB 2.0 ×ÊÔ´µÄ JMS
JMS Óë MessageDrivenBean
½áÂÛ
²Î¿¼×ÊÁÏ
×÷Õß¼ò½é
Ð嵀 EJB 2.0 ¹æ·¶²»½ö½öÊÇÒ»¸öеĽ׶ÎÐÔ·¢Ðа棬Ëü¼ÓÈëÁËÐí¶àÒýÈËעĿµÄ±ä¶¯£¬°üÀ¨ CMP ×é¼þÄ£ÐÍÖеÄһЩ±ä¶¯ºÍÒ»ÖÖÐ嵀 bean ÀàÐÍ£¬ËüÃǽ«ÔöÇ¿ÄúÔÚ¿ª·¢Ó¦ÓóÌÐòʱµÄÁé»îÐԺͿÉÒÆÖ²ÐÔ¡£ÇëÂÊÏÈÁ˽â´Ëй淶µÄ¹¦ÄÜ£¬±¾ÔÂÒÑ·¢²¼ÁËËüµÄ¹«¿ª²Ý°¸¡£
6 Ô 2 ºÅ·¢²¼µÄ Enterprise JavaBeans 2.0 ²»½öÊÇÒ»¸ö½×¶ÎÐÔ·¢Ðа棬¶øÇÒÊǸù淶µÄÒ»¸öа汾¡£Õû¸ö¹æ·¶ÓÐ 500 ¶àÒ³£¬±ÈÒÔÇ°µÄ EJB 1.1 ¹æ·¶³¤ÁË 200 Ò³ (66%)¡£¸Ã¹æ·¶ÖÐ×îÖØÒªµÄ±ä¶¯ÊǶÔÈÝÆ÷¹ÜÀíµÄ³Ö¾ÃÐÔ (CMP) Ëù×÷µÄ¸ü¸Ä£¬ÒÔ¼°ÒýÈëÁËÒ»ÖÖÈ«Ð嵀 bean ÀàÐÍ£¬¼´
MessageDrivenBean¡£
EJB 2.0 ÖеĴóÁ¿¸ü¸Ä¶¼¼¯ÖÐÔÚÒ»ÖÖРCMP ×é¼þÄ£Ð͵Ķ¨ÒåÖС£ËüÍêÈ«²»Í¬ÓÚ¾ÉµÄ CMP Ä£ÐÍ£¬ÒòΪËüÒýÈëÁËÒ»¸öȫеijÉÔ±£¬¼´³Ö¾ÃÐÔ¹ÜÀíÆ÷£¬²¢ÒýÈëÁËȫеķ½Ê½À´¶¨ÒåÈÝÆ÷¹ÜÀíµÄ×ֶΣ¬ÒÔ¼°¶¨ÒåÕâЩ×Ö¶ÎÓëÆäËü bean ºÍ´ÓÊô¶ÔÏóµÄ¹Øϵ¡£
MessageDrivenBean £¨ÏûÏ¢ bean£©µÄÒýÈëÒ²ÊǷdz£ÖØÒªµÄ¡£ÏûÏ¢ bean ÌåÏÖ³ö JMS £¨Java Message Service£©Óë EJB Ï༯³É£¬ÒÔ´´½¨³öÒ»ÖÖÈ«Ð嵀 bean ÀàÐÍ£¬ËüÉè¼ÆÓÃÀ´´¦ÀíÒì²½µÄ JMS ÏûÏ¢¡£ÕâÖÖÕñ·ÜÈËÐĵÄÐÂÐÍ bean Ϊ JMS ¿Í»§»úÌṩһÖÖ×é¼þÄ£ÐÍ£¬ÔÊÐí½«ËüÃDz¿Êðµ½ EJB
ÈÝÆ÷ϵͳµÄ·á¸»¶øÇ¿½¡µÄ»·¾³ÖÐÈ¥¡£
¶Ô¸Ã¹æ·¶»¹×÷ÁËÐí¶à½ÏСµÄÆäËü¸ü¸Ä¡£ÕâЩÆäËü¸ü¸ÄËäȻҲÖØÒª£¬µ«ËüÃÇÖ÷ÒªÊÇÉ漰ʹ¸Ã¹æ·¶¸üÑϸñ£¬ÒÔ±ãÏû³ý¶àÒåÐÔ£¬²¢Ê¹ÕâЩ×é¼þ¾ßÓиü¸ßµÄ¿ÉÒÆÖ²ÐÔ¡£±¾Îļ¯ÖÐÌÖÂÛ EJB 2.0 ÖÐÒýÈëµÄРCMP ºÍÏûÏ¢ bean ×é¼þÄ£ÐÍ¡£
ÎÒ½«Ìṩ¼¸¸ö¾ßÌåµÄÀý×Ó£¬ËùÒÔ¶ÁÕßÓ¦¸ÃºÜÈÝÒ׸úÉϲ¢Àí½âËü¡£µ«ÊÇ£¬EJB ³õѧÕß¿ÉÄÜ·¢ÏÖÕâ¸ö²ÄÁϱȽÏÀ§ÄÑ£¬ÒòΪËü¼Ù¶¨¶ÁÕßÒÑ¶Ô EJB ÓÐÁË»ù±¾µÄÁ˽⡣ÓÐ¹Ø EJB µÄÏêϸÐÅÏ¢£¬Çë²ÎÔIJο¼×ÊÁÏ¡£
ÈÝÆ÷¹ÜÀíµÄ³Ö¾ÃÐÔ
ÈÝÆ÷¹ÜÀíµÄ³Ö¾ÃÐÔÔÚ EJB 2.0 Öз¢ÉúÁ˸ù±¾±ä»¯¡£ÔÚ EJB 2.0 ÖУ¬³Ö¾ÃÐÔ¹ÜÀíÆ÷ÔÚÔËÐÐʱ×Ô¶¯´¦Àí CMP ʵÌå bean µÄ³Ö¾ÃÐÔ¡£³Ö¾ÃÐÔ¹ÜÀíÆ÷¸ºÔð¸ù¾ÝÒ»ÖÖ³ÆΪ³éÏó³Ö¾ÃÐÔ·½°¸µÄÐ嵀 bean ³Ö¾ÃÐÔ¹ÜÀíÆ÷ºÏÔ¼£¬½«ÊµÌå bean
Ó³Éäµ½Êý¾Ý¿â¡£´ËÍ⣬³Ö¾ÃÐÔ¹ÜÀíÆ÷»¹¸ºÔðʵÏÖºÍÖ´ÐжàÖÖ²éÕÒ·½·¨£¬ÕâЩ²éÕÒ·½·¨¾ù»ùÓÚÒ»ÖÖ³ÆΪ EJB QL µÄÐÂÐͲéѯÓïÑÔ¡£
×¢Òâµ½ÒÔÏÂÊÂʵÊǺÜÖØÒªµÄ£¬¼´·ûºÏ EJB 2.0 ¹æ·¶µÄ²úÆ·±ØÐëÄÜÖ§³Ö EJB 1.1 CMP Ä£ÐÍ£¬ÓÖÄÜÖ§³ÖÐ嵀 EJB 2.0 Ä£ÐÍ¡£ËäÈ»ÕâÁ½ÖÖÄ£ÐͲ¢²»¼æÈÝ£¬µ«ÊÇΪÁ˱£Ö¤Ïòºó¼æÈÝÐÔ£¬¾Í±ØÐëÄÜÖ§³Ö EJB 1.1 Ä£ÐÍ¡£
³éÏó³Ö¾ÃÐÔ·½°¸
ΪÁËÀí½â³éÏó³Ö¾ÃÐÔ·½°¸ÊÇÈçºÎ¹¤×÷µÄ£¬ÒÔ¼°ËüΪʲôÖØÒª£¬ÎÒ½«ÎªÄú¿ìËٵػعËÒ»ÏÂÔÚ EJB 1.1 ÖÐÊÇÈçºÎ´¦Àí CMP µÄ£¬ËæºóÔÙÌÖÂÛÔÚ EJB 2.0 ÖÐÈçºÎ¶¨ÒåËü¡£
EJB 1.1 ÖÐµÄ CMP Ä£ÐÍ
ÔÚ EJB 1.1 ÖУ¬bean ¿ª·¢ÈËÔ±¸ºÔ𽫠bean ÀàµÄ³Ö¾ÃÐÔ×Ö¶ÎÉùÃ÷Ϊ Java »ù±¾ÀàÐÍ»ò¿ÉÐòÁл¯ÀàÐÍ¡£ÏÂÁÐʾÀýÏÔʾÁËÒ»¸ö Employee ÆóÒµ¼¶ bean À࣬ËüÊÇ°´ EJB 1.1 ¶¨ÒåµÄ£¬´øÓм¸¸ö CMP ×ֶΣº
// Employee bean Àà
public class EmployeeBean implements
java.ejb.EntityBean {
// ʵÀý×Ö¶Î
EntityContext ejbContext;
// ÈÝÆ÷¹ÜÀíµÄ×Ö¶Î
public int identity;
public String firstName;
public String lastName;
public double salary;
public Address address;
public Integer ejbCreate(int id, String fname,
String lname){
identity = id;
firstName = fname;
lastName = lname;
return null;
}
...
}
// Address ´ÓÊôÀà
public class Address implements Serializable{
public String street;
public String city;
public String state;
public String zip;
}
µ±½«¹ØϵÊý¾Ý¿âÓÃÓÚ³Ö¾ÃÐÔʱ£¬»ù±¾×Ö¶ÎÈç identity¡¢firstName¡¢lastName ºÍ salary£¬ºÜÈÝÒ׳־û¯£¬ÒòΪËüÃǺܺõØÓ³ÉäΪ SQL ÀàÐÍ£¬Èç INTEGER¡¢CHAR ºÍ DOUBLE¡£
ÔÚ EJB 1.1 ÖУ¬CMP bean µÄ XML ²¿ÊðÃèÊö·ûÌṩ cmp-field ÔªËØ£¬ÓÃÒÔ±êʶ´Ë bean ÀàÖеij־ÃÐÔ×ֶΣ¨ÈÝÆ÷¹ÜÀíµÄ×ֶΣ©¡£ÈçÏÂËùʾ£¬cmp-field ÔªËØÓÃÀ´Çø·ÖдÈëÊý¾Ý¿âµÄ×ֶκͲ»Ð´ÈëÊý¾Ý¿âµÄ×ֶΡ£ÀýÈ磬ejbContext
×ֶξͲ»°üÀ¨ÔÚÈÝÆ÷¹ÜÀíµÄ×ֶεÄÁбíÖУ¬Òò´ËËü²»Êdz־ÃÐÔ×ֶΡ£
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>EmployeeEJB</ejb-name>
...
<persistence-type>Container</persistence-type>
...
<cmp-field><field-name>identity</field-name></cmp-field>
<cmp-field><field-name>firstName</field-name></cmp-field>
<cmp-field><field-name>lastName</field-name></cmp-field>
<cmp-field><field-name>salary</field-name></cmp-field>
<cmp-field><field-name>address</field-name></cmp-field>
...
ÈÝÆ÷ÌṩÕßÌṩһÖÖ¹¤¾ß£¬ÓÃÀ´½« bean µÄ³Ö¾ÃÐÔ×Ö¶ÎÓ³Éäµ½Êý¾Ý¿â±íÖеÄÁУ¬Í¨³£Ã¿¸ö bean ¶ÔÓ¦Ò»¸ö±í¡£µ«ÊÇ£¬¿ÉÐòÁл¯µÄÀàÐÍ£¬Èç Address£¬¾Í±È½ÏÄÑÓڳ־û¯¡£ÔÚ EJB 1.1 ÖУ¬Ã»Óбê×¼µÄ·½·¨½«¿ÉÐòÁл¯µÄ¶ÔÏóÓ³Éäµ½¹ØϵÊý¾Ý¿â¡£ËäÈ» Address ÀàÓÐÆä×ÔÉíµÄ×ֶμ¯£¬µ« XML
²¿ÊðÃèÊö·û²¢Ã»ÓÐÌṩһÖÖ»úÖÆ£¬À´½«ÕâЩ×Ö¶ÎÓ³Éäµ½Êý¾Ý¿â¡£ÔÚ´ó¶àÊýÇé¿öÏ£¬ÈËÃÇÆÚÍû½«¿ÉÐòÁл¯µÄ¶ÔÏó£¨Èç Address£©×÷Ϊ¶þ½øÖÆÀàÐÍ£¨ÓÐʱ³ÆΪ blob ÀàÐÍ£©³Ö¾Ã»¯µ½Ä³¸öÊý¾Ý¿â±íÖС£
ÓÉÓÚʵÌå bean µÄÊý¾Ý·½°¸Ö𽥸´ÔÓÆðÀ´£¬ËùÒÔÕâ¸öÎÊÌâÒ²±äµÃÑÏÖØÁË¡£ÀýÈ磬Employee bean ¿ÉÄÜÓжà¸öÀàËÆÓÚ Address µÄ×Ó¶ÔÏó£¬Èç Benefits ºÍ JobPosition¡£ÕâЩ×Ó¶ÔÏó³ÆΪ´ÓÊô¶ÔÏ󣬿ÉÒÔÐγɹØϵÊý¾Ý¿âÖп缸¸ö±íµÄ¸´ÔÓ¶ÔÏóͼ¡£ÁíÍ⣬EJB 1.1 ÖÐµÄ CMP
Ôںܴó³Ì¶ÈÉϲ»×ãÒԳ־û¯ÓëÆäËü bean µÄ¹Øϵ¡£ÔÚ EJB 1.1 ÖУ¬Èç¹ûij¸ö bean ×¼±¸Î¬³ÖÓëÁíÒ»¸ö bean µÄ¹Øϵ£¬ÔòÈÝÆ÷»á×Ô¶¯½«Ö÷¹Ø¼ü×Ö»ò¾ä±úÓÃ×÷Ò»¸öÁ´½Ó¡£ÓëijЩÆäËü bean µÄ¹ØϵÆäÐÔÖÊ¿ÉÄÜÊÇË«ÏòµÄ£¬»òÕßÒªÒÀÀµÓÚһЩ²»Ò×ÓÃÖ÷¹Ø¼ü×Ö»ò¾ä±úÀ´±íʾµÄ×ֶΣ¬ÎªÁ˱£³ÖÓëÕâÀà
bean µÄ¹Øϵ£¬ÉÏÃæµÄ°ì·¨Òѱ»Ö¤Ã÷ÊÇÒ»ÖÖԶδÍêÉƵĻúÖÆ¡£
EJB 2.0 µÄ CMP Ä£ÐÍ
ÔÚ EJB 2.0 ÖУ¬CMP ʵÌå bean ºÍ³Ö¾ÃÐÔ¹ÜÀíÆ÷Ö®¼äµÄкÏÔ¼£¬Ê¹ÄúÄܹ»ÔÚʵÌå bean Öж¨Òå¸ü¸´Ôӵġ¢¿ÉÒÆÖ²ÐÔ¸üÇ¿µÄ¹Øϵ£¬°üÀ¨ bean Óë bean Ö®¼ä¡¢bean Óë´ÓÊô¶ÔÏóÖ®¼ä¡¢ÉõÖÁ´ÓÊô¶ÔÏóÓë´ÓÊô¶ÔÏóÖ®¼äµÄ¹Øϵ¡£
³Ö¾ÃÐÔ¹ÜÀíÆ÷ÊÇмÓÈëµ½ Enterprise JavaBeans ²¿Êð¹ý³ÌÖеġ£ÈÝÆ÷³§ÉÌ£¬»òר³¤ÓÚÌض¨Êý¾Ý¿âµÄ³Ö¾ÃÐԵij§ÉÌ£¬½«ÄÜÌṩÕâÖÖ³Ö¾ÃÐÔ¹ÜÀíÆ÷¡£Æä˼·Êǽ«ÓÃÓÚ¹ÜÀí bean
¹ØϵµÄ»úÖÆ´ÓÈÝÆ÷ÖзÖÀë³öÀ´£¬ÈÝÆ÷Ö»¸ºÔð¹ÜÀí°²È«¡¢ÊÂÎñºÍ×ÊÔ´¡£ÕâÖÖÖ°ÔðÉϵķÖÀëʹ²»Í¬µÄ³Ö¾ÃÐÔ¹ÜÀíÆ÷Äܹ»Ó벻ͬµÄÈÝÆ÷Ò»Æð¹¤×÷¡£ËüҲʹʵÌå bean ÔÚ²»Í¬ EJB ³§ÉÌÖ®¼äÒÔ¼°ÔÚ¸÷ÖÖ³Ö¾ÃÐÔ¹ÜÀíÆ÷Ö®¼ä¾ßÓиüÇ¿µÄ¿ÉÒÆÖ²ÐÔ¡£
Èç¹ûÄúʹÓûòѧϰ¹ý Thought Inc. Éú²úµÄ£¬ÄÜ×Ô¶¯Îª EJB 1.1 ÈÝÆ÷Éú³É BMP£¨bean ¹ÜÀíµÄ³Ö¾ÃÐÔ£©bean µÄ²úÆ· CocoBase£¬ÔòÄú¶Ô³Ö¾ÃÐÔ¹ÜÀíÆ÷¹¤¾ßÈçºÎ¹¤×÷¾ÍÒѾ±È½ÏÊìϤÁË¡£CocoBase ¸ù¾Ý bean ²¿ÊðÕßÌṩµÄ£¬´Ó¶ÔÏóµ½¹ØϵµÄÓ³ÉäÐÅÏ¢£¬Îª BMP bean
Éú³ÉÈ«²¿Êý¾Ý¿â·ÃÎÊÂß¼¡£ÔÚ EJB 2.0 ÖУ¬³Ö¾ÃÐÔ¹ÜÀíÆ÷Äܹ»¸ù¾Ý²¿ÊðÃèÊö·û¡¢bean µÄ³éÏó³Ö¾ÃÐÔ·½°¸ºÍ²¿ÊðÕßÍê³ÉµÄ¹¤×÷ËùÌṩµÄÐÅÏ¢£¬Éú³É CMP ʵÌåµ½¹ØϵÊý¾Ý¿âµÄÓ³Éä¡£µ«ÊÇ£¬³Ö¾ÃÐÔ¹ÜÀíÆ÷²¢²»¾ÖÏÞÓÚ¹ØϵÊý¾Ý¿â¡£Ò²¿ÉÒÔΪ¶ÔÏóÊý¾Ý¿âÒÔ¼°ÒÅÁôµÄϵͳºÍ ERP ϵͳ£¨Èç
SAP£©¿ª·¢³Ö¾ÃÐÔ¹ÜÀíÆ÷¡£
ΪÁ˽«³Ö¾ÃÐÔ¹ÜÀíÆ÷´ÓÈÝÆ÷ÖзÖÀë³öÀ´£¬±ØÐ붨Òå bean Óë³Ö¾ÃÐÔ¹ÜÀíÆ÷Ö®¼äµÄºÏÔ¼¡£Õâ¸öºÏÔ¼ÔÚеijéÏó³Ö¾ÃÐÔ·½°¸ÖбíÏÖ³öÀ´¡£´Ë·½°¸ÊÇͨ¹ý²¿ÊðÃèÊö·ûÖÐÒ»×éÐ嵀 XML ÔªËØºÍ CMP ʵÌå bean ÖеÄÒ»×é´úÂëÏ°ÓﶨÒåµÄ¡£ÔÚ EJB 2.0 ÖУ¬CMP bean
Àà±»ÉùÃ÷Ϊ³éÏóÀ࣬ËüµÄ³Ö¾ÃÐÔ×ֶκ͹Øϵ×Ö¶ÎÊÇʹÓóéÏóµÄ¶Á·½·¨ºÍд·½·¨À´·ÃÎʵģ¬¶øÕâÁ½ÖÖ·½·¨µÄ·½·¨ÌØÕ÷ÔòÓ³ÉäΪ XML ²¿ÊðÃèÊö·ûÖеÄÌض¨ÔªËØ¡£
ÔÚ²¿Êð¸Ã bean ʱ£¬Äú½«Ê¹Óó־ÃÐÔ¹ÜÀíÆ÷¹¤¾ß£¬¸ù¾Ý XML ²¿ÊðÃèÊö·ûºÍ bean À࣬À´¾ßÌåʵÏִ˳éÏó bean À༰Æä´ÓÊô¶ÔÏóÀà¡£¾ßÌåʵÏÖ½«°üÀ¨Êý¾Ý·ÃÎÊ´úÂ룬´Ë´úÂ뽫ÔÚÔËÐÐʱ½« bean
µÄ״̬ʵ¼Ê¶Á³öºÍдµ½Êý¾Ý¿âÖС£ÔÚÔËÐÐʱ£¬ÈÝÆ÷ʹÓÃÓɳ־ÃÐÔ¹ÜÀíÆ÷¹¤¾ßÉú³ÉµÄ×ÓÀ࣬¶ø²»Ê¹Óà bean ÌṩÕ߶¨ÒåµÄ³éÏóÀà¡£
bean ÀàµÄ¼Ì³Ð²ã´Î½á¹¹
ΪÁËʹÌÖÂÛ¸ü³äʵ£¬ÕâÀïÌṩһ¸ö CMP ʵÌåµÄʾÀý£¬Ëü¸ü¾ßÌåµØ˵Ã÷Á˳éÏó³Ö¾ÃÐÔ·½°¸ÊÇÈçºÎ¹¤×÷µÄ¡£
EJB 2.0 ÖеÄÒ»¸öʾÀý CMP ʵÌå
ÔÚ EJB 2.0 ÖУ¬ÈÝÆ÷¹ÜÀíµÄʵÌå bean ±»¶¨ÒåΪ³éÏóµÄ£¬¶øÇÒËüµÄ³Ö¾ÃÐÔ×ֶβ¢²»ÔÚ bean ÀàÖÐÖ±½Ó¶¨Òå¡£×÷ΪÌæ´ú£¬¿ª·¢ÁËÒ»ÖÖ³éÏóµÄ³Ö¾ÃÐÔ·½°¸£¬´Ó¶øÔÊÐí bean ÌṩÕß¼ä½ÓµØÉùÃ÷³Ö¾ÃÐÔ×Ö¶ÎºÍ bean ¹Øϵ¡£ÏÂÃæÊÇ Employee bean
µÄÒ»¸öʾÀý£¬ËüʹÓÃÁËеijéÏó³Ö¾ÃÐÔ·½°¸¡£Çë×¢Ò⣬¸Ã bean ÀàÖÐδÉùÃ÷Èκγ־ÃÐÔ×ֶΡ£
public abstract EmployeeBean implements
javax.ejb.EntityBean {
. // ʵÀý×Ö¶Î
EntityContext ejbContext;
// ÈÝÆ÷¹ÜÀíµÄ³Ö¾ÃÐÔ×Ö¶Î
public abstract void setIdentity(int
identity);
public abstract int getIdentity();
public abstract void setFirstName(String
firstName);
public abstract String getFirstName();
public abstract void setLastName(String
lastName);
public abstract String getLastName();
// ÈÝÆ÷¹ÜÀíµÄ¹Øϵ×Ö¶Î
public abstract void
setContactInfo(ContactInfo info);
public abstract ContactInfo
getContactInfo();
...
}
ÔÚ´Ë bean µÄ XML ²¿ÊðÃèÊö·ûÖУ¬³éÏóµÄ³Ö¾ÃÐÔ·½°¸ÉùÃ÷ÈÝÆ÷¹ÜÀíµÄ¸÷¸ö×ֶκ͸÷ÖÖ¹Øϵ¡£
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>EmployeeEJB</ejb-name>
...
<persistence-type>Container</persistence-type>
...
<cmp-field><field-name>identity</field-name></cmp-field>
<cmp-field><field-name>firstName</field-name></cmp-field>
<cmp-field><field-name>lastName</field-name></cmp-field>
...
</entity>
</enterprise-beans>
<dependents>
<dependent>
<dependent-class>ContactInfo</dependent-class>
<dependent-name>ContactInfo</dependent-name>
<cmp-field>street</cmp-field>
<cmp-field>city</cmp-field>
<cmp-field>state</cmp-field>
<cmp-field>zip</cmp-field>
<cmp-field>homePhone</cmp-field>
<cmp-field>workPhone</cmp-field>
<cmp-field>email</cmp-field>
...
</dependent>
<relationships>
<ejb-relation>
<ejb-relation-name>Employee-ContactInfo</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>
employee-has-contactinfo
</ejb-relationship-role-name>
<multiplicity>one</multiplicity>
<role-source>
<ejb-name>EmployeeEJB</ejb-name>
</role-source>
<cmr-field>
<cmr-field-name>contactInfo</cmr-field-name>
<cmr-field-type>ContactInfo</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>
contactinfo_belongsto_employee
</ejb-relationship-role-name>
<multiplicity>one</multiplicity>
<role-source>
<dependent-name>ContactInfo<dependent-name>
</role-source>
</ejb-relationship-role>
</ejb-relation>
</relationships>
<ejb-jar>
ÓÃÀ´ÃèÊöÈÝÆ÷¹ÜÀíµÄ¹ØϵµÄ XML ÔªËØ¿ÉÄܱäµÃ·Ç³£¸´ÔÓ£¬ÒòΪËûÃDZØÐë´¦Àí¸÷ÖÖ¹ØϵµÄ¶ÔÓ¦ÐԺͷ½Ïò£¨µ¥ÏòµÄ»¹ÊÇË«ÏòµÄ£©¡£ÉÏÃæµÄ´úÂë¶Î˵Ã÷£¬ÎªÁËÃèÊö bean ÓëÆä´ÓÊô¶ÔÏóÀàÖ®¼äµÄ¼òµ¥¹Øϵ£¬ÄúÐèÒªÄÄЩԪËØ¡£ËäÈ»¼´Ê¹ÊǼòµ¥µÄ¹ØϵҲ»á±»×ª»»ÎªÈß³¤µÄ
XML£¬µ«ËùÓÐÕâЩԪËض¼ÊDZØÐèµÄ£¬ÒÔ±ã³Ö¾ÃÐÔ¹ÜÀíÆ÷Äܹ»½«¸´ÔӵĶÔÏóͼӳÉäµ½Êý¾Ý¿âÖС£
ËäÈ»ÓÃÓÚ¶¨Òå CMP bean µÄ³éÏó³Ö¾ÃÐÔ·½°¸µÄ XML ÔªËØÊÇ EJB 2.0 ÖÐµÄ CMP µÄÖ÷ÒªÎÊÌ⣬µ«ÎªÁ˼ò½àÆð¼û£¬±¾ÎIJ»ÔÙÌṩ XML ʾÀý¡£×÷ΪÌæ´ú£¬±¾ÎĽ«´¿´âÒÀ¿¿ bean ÀàÖбØÐëʹÓõijéÏóÏ°ÓÀ´ËµÃ÷ EJB 2.0 ÖÐµÄ CMP ±³ºóµÄ»ù±¾¸ÅÄî¡£ÕâЩ´úÂëÏ°ÓïÓë XML
²¿ÊðÃèÊö·ûÖеĹØϵԪËØÒ»ÆðʹÓ㬲¢ÓɺóÕ߶¨Ò壬ËùÒÔÄú²»ÄÜÖ»ÓÐÆäÒ»¶øûÓÐÁíÒ»¸ö£¬µ«ËüÃDZȸ÷½°¸µÄ XML ²¿·Ö½ÏÈÝÒ×Àí½â¡£
³ýÁË XML ÔªËØÖ®Í⣬³éÏóµÄ³Ö¾ÃÐÔ·½°¸»¹¶¨ÒåÁËÒ»×éÏ°ÓËüÃÇÔÚÉùÃ÷ bean À༰ÆäÏà¹ØµÄ¶ÔÏóʱ±ØÈ»»áÓõ½¡£ÓÃÀ´·ÃÎʺÍÐÞ¸Ä×ֶεķ½·¨ÊÇÑϸñ¶¨ÒåÁ˵ģ¬ÒªÇóÓà set<METHOD> ·½·¨Ð޸ij־ÃÐÔ×ֶΣ¬¶øÓà get<METHOD>
·½·¨·ÃÎÊËüÃÇ¡£ÕâЩ·½·¨µÄÃû³ÆºÍ·µ»ØÀàÐÍÓɲ¿ÊðÃèÊö·ûÖÐËüÃÇÏàÓ¦µÄ XML ¹ØϵԪËع涨¡£
ʵÌå bean ÀàºÍ´ÓÊôÀ඼×ñÑÏàͬµÄ³éÏó³Ö¾ÃÐÔ·½°¸¡£ÏÂÃæÊÇÈçºÎ½« ContactInfo ¶ÔÏó¶¨ÒåΪ´ÓÊô¶ÔÏóÀàµÄʾÀý¡£
public abstract class ContactInfo {
// ¼ÒÍ¥µØÖ·ÐÅÏ¢
public abstract void setStreet(String street);
public abstract String getStreet();
public abstract void setState(String state);
public abstract String getState();
public abstract void setZip(String zip);
public abstract String getZip();
public abstract void setHomePhone(String phone);
public abstract String getHomePhone();
// ¹¤×÷µØÖ·ÐÅÏ¢
public abstract void setWorkPhone(String phone);
public abstract String getWorkPhone();
public abstract void setEMail(String email);
public abstract String getEMail();
...
}
´ÓÊô¶ÔÏóËæʵÌå bean µÄ´æÔÚ¶ø´æÔÚ£¬ËæʵÌå bean µÄÖÐÖ¹¶øÖÐÖ¹£¬ÕâÊÇÀí½â´ÓÊô¶ÔÏóÓëʵÌå bean Ö®¼ä¹ØϵµÄ¹Ø¼ü¡£´ÓÊô¶ÔÏó°üº¬ÔÚÒ»¸ö¾ßÌåµÄʵÌåÖУ¬ËùÒÔɾ³ýÕâ¸öʵÌ彫µ¼Ö´ÓÊô¶ÔÏóÒ²±»É¾³ý¡£ÓùØϵÊý¾Ý¿âµÄÊõÓïÀ´Ëµ£¬ÓÐʱÕâ¾Í³ÆΪ¼¶ÁªÉ¾³ý¡£
´ÓÊô¶ÔÏó£¬Èç ContactInfo£¬ÓÃÔÚ¹Øϵ×Ö¶ÎÖС£ÓëʵÌå bean ÐγɹØϵµÄ´ÓÊô¶ÔÏó¼¼ÊõÉϳÆΪ´ÓÊô¶ÔÏóÀà¡£EJB ¿Í»§¶ËÓ¦ÓóÌÐòÓÀÔ¶²»ÄÜÖ±½Ó·ÃÎÊ´ÓÊô¶ÔÏóÀࣻÕâÖÖÀ಻ÄÜÓÃ×÷ bean µÄÔ¶³Ì»ò±¾µØ½Ó¿ÚÖеIJÎÊý»ò·µ»ØÖµ¡£´ÓÊô¶ÔÏóÀàÖ»¶Ô bean Àà²ÅÊǿɼûµÄ¡£
´ÓÊô¶ÔÏóÀ಻ÊʺÏ×÷ΪԶ³Ì²ÎÊýÀàÐÍ£¬ÒòΪËüÃÇÓë bean ÔÚÔËÐÐʱµÄ³Ö¾ÃÐÔÂß¼ÓÐÃÜÇеÄÁªÏµ¡£³Ö¾ÃÐÔ¹ÜÀíÆ÷À©Õ¹Á˳éÏóµÄ´ÓÊô¶ÔÏóÀ࣬ÒÔ±ãÄÜÌṩһÖÖʵÏÖ£¬¿ÉÓÃÓÚÔÚÔËÐÐʱ¹ÜÀí bean µÄ³Ö¾ÃÐÔ״̬¡£´ËÍ⣬³éÏóµÄ³Ö¾ÃÐÔ·½°¸»¹ÎªÊý¾Ý½¨Ä£ -- ¶ø²»ÊÇΪÄÇЩÓÉÆóÒµ¼¶ bean
±íʾµÄÒµÎñ¸ÅÄģ -- ËùÒÔ£¬×÷ΪһÖÖÉè¼Æ²ßÂÔ£¬½«³éÏóµÄ³Ö¾ÃÐÔ·½°¸¶Ô EJB ¿Í»§»úÒþ²ØÆðÀ´ÊÇÓÐÒâÒåµÄ¡£
ÀýÈ磬ContactInfo ¹Øϵ×Ö¶ÎÖгýÁË bean µÄ¿Í»§»úËùÐèµÄ¼òµ¥µØÖ·ÐÅÏ¢Ö®Í⣬»¹°üº¬Ðí¶àÆäËüÐÅÏ¢¡£ËäÈ»Äú¿ÉÒÔʹÓóéÏó³Ö¾ÃÐÔ·½°¸ÖеĴÓÊô¶ÔÏóÀà ContactInfo£¨Ëü¶Ô bean
µÄ¿Í»§»úÊÇÒþ²ØµÄ£©£¬µ«ÊÇ£¬ÄúµÃÓÃÆäËüµÄ¶ÔÏóÀ´°ÑÕâЩÊý¾Ýʵ¼Ê±í¶¸ø¿Í»§»ú¡£ÏÂÃæÊÇÒ»¸öʾÀý£¬ËµÃ÷ÁËÈçºÎ¶Ô EJB ¿Í»§»úÒþ²Ø ContactInfo ´ÓÊô¶ÔÏó¡£ÔÚ´ËÀýÖУ¬µØÖ·ÐÅÏ¢ÊÇͨ¹ýÔÚ EJB 1.1 µÄʾÀýÖпª·¢µÄ Address ¶ÔÏóÀ´±í¶µÄ¡£
// Employee bean µÄÔ¶³Ì½Ó¿Ú
public interface Employee extends javax.ejb.EJBObject {
public Address getHomeAddress();
public void setHomeAddress(Address address);
public int getIdentity() throws RemoteException;
public void setFirstName(String firstName) throws
RemoteException;
public String getFirstName()throws RemoteException;
public void setLastName(String lastName) throws
RemoteException;
public String getLastName() throws RemoteException;
}
// Employee bean µÄ bean Àà
public abstract EmployeeBean implements
javax.ejb.EntityBean {
...
public Address getHomeAddress(){
ContactInfo info = getContactInfo();
Address addr = new Address();
addr.street = info.getStreet();
addr.city = info.getCity();
addr.state = info.getState();
addr.zip = info.getZip();
return addr;
}
public void setHomeAddress(Address addr){
ContactInfo info = getContactInfo();
info.setStreet(addr.street);
info.getCity(addr.city);
info.getState(addr.state);
info.getZip(addr.zip);
}
....
// ÈÝÆ÷¹ÜÀíµÄ¹Øϵ×Ö¶Î
public abstract void setContactInfo(ContactInfo
info);
public abstract ContactInfo getContactInfo();
...
}
¾¡¹ÜÈÝÆ÷¹ÜÀíµÄ¹Øϵ×Ö¶ÎûÓбí¶¸ø¿Í»§»ú£¬µ«ÄúÈÔÈ»¿ÉÒÔ´ÓÔ¶³Ì½Ó¿ÚÖ±½ÓʹÓÃÈÝÆ÷¹ÜÀíµÄ³Ö¾ÃÐÔ×ֶΡ£Çë×¢Ò⣬ÓÃÀ´·ÃÎÊ firstName ºÍ lastName µÄÈÝÆ÷¹ÜÀíµÄ³Ö¾ÃÐÔ×Ö¶ÎÊÇÔÚÔ¶³Ì½Ó¿ÚÖÐʹÓõġ£
Ò»¸ö bean Óë¸÷ÖÖ´ÓÊô¶ÔÏóÀàÖ®¼ä¿ÉÄܾßÓжàÖÖ²»Í¬µÄ¹Øϵ£¬ËüÃÇÓÉÕâÖÖ¹ØϵµÄ¶ÔÓ¦ÐԺͷ½ÏòÀ´¶¨Òå¡£Bean Óë´ÓÊô¶ÔÏóÀàÖ®¼ä¿ÉÒÔÓÐÒ»¶Ô¶àºÍÒ»¶ÔÒ»µÄ¹Øϵ¡£ÀýÈ磬Employee bean ¿ÉÄܽöÓÐÒ»¸ö Benefit ´ÓÊô¶ÔÏóÀ࣬µ«¿ÉÄÜÓÐÐí¶à ContactInfo ´ÓÊô¶ÔÏóÀà¡£
public abstract EmployeeBean implements
javax.ejb.EntityBean {
...
public abstract void setContactInfos(Collection
addresses);
public abstract Collection getContactInfos():
public abstract void setBenefit(Benefit benefit);
public abstract Benefit getBenefit();
...
}
Óë´ÓÊô¶ÔÏóÀàµÄÒ»¶Ô¶à¹Øϵ¼È¿É±íʾΪ java.util.Collection ÀàÐÍ£¬Ò²¿É±íʾΪ ava.util.Set ÀàÐÍ£¨×¢£ºÔÚ±¾¹æ·¶µÄºóÐø°æ±¾ÖУ¬java.util.Map ºÍ java.util.List ±»ÊÓΪ¸½¼ÓµÄ·µ»ØÀàÐÍ£©£¬¶øÓë´ÓÊô¶ÔÏóµÄÒ»¶ÔÒ»¹ØϵÔòʹÓôÓÊô¶ÔÏóµÄÀàÐÍ¡£
ʵÌå bean Ò²¿ÉÒÔ¶¨ÒåÓëÆäËüʵÌå bean µÄ¹Øϵ¡£ÕâЩ¹Øϵ¿ÉÒÔÊÇÒ»¶ÔÒ»¡¢Ò»¶Ô¶à»ò¶à¶Ô¶à¡£ÀýÈ磬Employee bean ¿ÉÄÜÓÐÐí¶à×Ó¼¶ bean£¬¶øÖ»ÓÐÒ»¸öÅä¶ÔµÄ bean¡£ÏÂÃæµÄ´úÂë¶ÎʹÓóéÏó³Ö¾ÃÐÔ·½°¸µÄ·½·¨Ï°Ó˵Ã÷ÁËÈçºÎΪÕâЩ¹Øϵ½¨Ä£¡£¸ÃÓ¦ÓóÌÐòÖУ¬×Ó¼¶ bean ºÍÅä¶ÔµÄ bean
¶¼±íÏÖΪ Person bean¡£
public abstract EmployeeBean implements
javax.ejb.EntityBean {
...
public abstract void setSpouse(Person manager);
public abstract Person getSpouse();
public abstract void setChildren(Collection
family);
public abstract Collection getChildren();
...
}
ÓëÁíÒ»¸ö bean µÄÒ»¶Ô¶à¹Øϵ±íʾΪ java.util.Collection ÀàÐÍ»ò java.util.Set ÀàÐÍ£¬¶øÒ»¶ÔÒ»¹ØϵÔòʹÓøà bean µÄÔ¶³Ì½Ó¿ÚÀàÐÍ¡£
´ÓÊô¶ÔÏó±¾ÉíÓëͬһ¸ö bean ÖеÄÆäËü´ÓÊô¶ÔÏóÖ®¼ä¿ÉÒÔÓÐÒ»¶ÔÒ»¡¢Ò»¶Ô¶àºÍ¶à¶Ô¶àµÄ¹Øϵ¡£´ËÍ⣬´ÓÊô¶ÔÏóÓëÆäËüʵÌå bean£¨³ýÆ丸¼¶ bean Ö®Í⣩Ҳ¿ÉÒÔÓÐÒ»¶ÔÒ»¡¢Ò»¶Ô¶àµÄ¹Øϵ¡£ÏÂÃæµÄʾÀýÏÔʾ£¬Benefit ´ÓÊô¶ÔÏóÀàÓë Salary
´ÓÊô¶ÔÏó£¨Ò»ÖÖ±¨³ê¼ÆËã³ÌÐò£©Ö®¼äÔõÑù¾ßÓÐÒ»¶ÔÒ»µÄ¹Øϵ£¬¶øÓë Investment bean ÓÖÔõÑù¾ßÓÐÒ»¶Ô¶àµÄ¹Øϵ¡£
public abstract class Benefit {
public abstract void setSalary(Salary salary);
public abstract Salary getSalary();
public abstract void setInvestments(Collection
investments);
public abstract Collection getInvestments();
}
ÔÚ²¿Êðʱ£¬²¿ÊðÕß½«Ê¹Óó־ÃÐÔ¹ÜÀíÆ÷¹¤¾ßÀ´¾ßÌåʵÏÖÕâ¸ö bean À༰Æä´ÓÊôÀà¡£ÕâЩ¾ßÌåʵÏÖ½«ÔÚÔËÐÐʱ±£³Ö¸÷ÖÖ¹Øϵ£¬²¢Ê¹¸÷ bean ʵÀýµÄ״̬ÓëÊý¾Ý¿âͬ²½¡£ÈÝÆ÷½«ÔÚÔËÐÐʱ¹ÜÀí³Ö¾ÃÐÔʵÀý£¬´Ó¶øÌṩһÖÖÇ¿½¡µÄ»·¾³£¬ÆäÖоßÓÐ×Ô¶¯µÄ·ÃÎÊ¿ØÖƺÍÊÂÎñ¿ØÖÆ¡£
bean Ò²¿ÉÒÔ¶¨Òå´ÓÊô¶ÔÏóµÄÖµ£¬ÕâЩ¶ÔÏóÊÇ¿ÉÐòÁл¯µÄ¶ÔÏó£¬Èç EJB 1.1 ʾÀýÖÐµÄ Address ¶ÔÏó¡£ÕâЩֵͨ¹ýÐòÁл¯¶ø±äΪ³Ö¾ÃµÄ£¬ËüÃDz¢²»ÐγÉÓë bean µÄ¹Øϵ -- ËüÃÇÊÇÑϸñµÄÈÝÆ÷¹ÜÀíµÄ³Ö¾ÃÐÔ×ֶΡ£
ÈÝÆ÷Óë³Ö¾ÃÐÔ¹ÜÀíÆ÷Ö®¼äÒ²ÒѾ¶¨ÒåÁËÒ»¸öºÏÔ¼£¬Ê¹³Ö¾ÃÐÔ¹ÜÀíÆ÷¿ÉÒÔ»ñµÃÊÂÎñµÄ¾ä±ú£¬²¢·ÃÎÊÓɸÃÈÝÆ÷¹ÜÀíµÄÊý¾Ý¿âÁ¬½Ó³Ø¡£Õâ¸öºÏÔ¼ÉÔÏÓ¿íËÉ£¬½«À´»¹ÐèҪʹÆä¸üΪÑϸñ£¬µ«ËüÊÇÔÊÐí³Ö¾ÃÐÔ¹ÜÀíÆ÷¿ç EJB ÈÝÆ÷ÒÆÖ²µÄ»ù´¡¡£ÈÝÆ÷ºÍ³Ö¾ÃÐÔ¹ÜÀíÆ÷Ö®¼äºÏÔ¼µÄϸ½ÚÒѳ¬³öÁ˱¾Îĵķ¶Î§¡£
³ýÁËͨ¹ý³éÏó³Ö¾ÃÐÔ·½°¸¶¨Òå³Ö¾ÃÐÔÖ®Í⣬EJB 2.0 »¹ÌṩÁËÒ»ÖÖеIJéѯÓïÑÔ£¬ÓÃÀ´ËµÃ÷³Ö¾ÃÐÔ¹ÜÀíÆ÷Ó¦¸ÃÈçºÎʵÏÖ CMP Öеĸ÷ÖÖ²éÕÒ·½·¨¡£
EJB ²éѯÓïÑÔ
EJB ²éѯÓïÑÔ (EJB QL) ¹æ¶¨Á˳־ÃÐÔ¹ÜÀíÆ÷Ó¦¸ÃÈçºÎʵÏÖÔÚ±¾µØ½Ó¿ÚÖж¨ÒåµÄ¸÷ÖÖ²éÕÒ·½·¨¡£ EJB QL ÒÔ SQL-92 Ϊ»ù´¡£¬¿ÉÓɳ־ÃÐÔ¹ÜÀíÆ÷×Ô¶¯±àÒ룬ÕâʹµÃʵÌå bean ¾ßÓиü¸ßµÄ¿ÉÒÆÖ²ÐÔ£¬²¢ÇÒ¸üÈÝÒײ¿Êð¡£
EJB QL ºÍ²éÕÒ·½·¨
EJB QL Óï¾äÊÇÔÚʵÌå bean µÄ²¿ÊðÃèÊö·ûÖÐÉùÃ÷µÄ¡£Ê¹Óà EJB QL ·Ç³£¼òµ¥¡£×÷Ϊһ¸öÀý×Ó£¬Employee bean µÄ±¾µØ½Ó¿Ú¿ÉÒÔ°´ÒÔÏ·½Ê½ÉùÃ÷£º
public interface EmployeeHome extends javax.ejb.EJBHome
{
...
public Employee findByPrimaryKey(Integer id)
throws RemoteException, CreateException;
public Collection findByZipCode(String zipcode)
throws RemoteException, CreateException;
public Collection findByInvestment(String
investmentName)
throws RemoteException, CreateException;
}
¸ø¶¨ÁËÉÏÃæµÄ±¾µØ½Ó¿Ú¶¨ÒåÖ®ºó£¬Äú¾Í¿ÉÒÔʹÓà EJB QL À´Ö¸¶¨³Ö¾ÃÐÔ¹ÜÀíÆ÷Ó¦¸ÃÈçºÎÖ´ÐвéÕÒ·½·¨¡£Ã¿¸öʵÌå bean ¶¼±ØÐëÓÐÒ»¸ö findByPrimaryKey() ·½·¨¡£ÎªÖ´Ðи÷½·¨ËùÐèµÄ²éѯÊǺÜÃ÷Ï﵀ -- ʹÓÃÖ÷¹Ø¼ü×ֵģ¨Ò»¸ö»ò¼¸¸ö£©×Ö¶ÎÔÚÊý¾Ý¿âÖвéÕÒ bean£¬ÕâÑù¾Í²»ÐèÒªÈκΠEJB
QL Óï¾ä¡£
findByZipCode() ·½·¨ÓÃÀ´»ñµÃ¾ßÓÐij¸öÓÊÕþ±àÂëµÄËùÓÐ Employee bean¡£Õ⽫ʹÓò¿ÊðÃèÊö·ûÖеÄÏÂÁÐ EJB QL À´±í´ï¡£
FROM contactInfo WHERE contactInfo.zip = ?1
¸ÃÓï¾ä±¾ÖÊÉÏÊDZíʾ¡°Ñ¡ÔñÆäÓÊÕþ±àÂëµÈÓÚ zipcode ²ÎÊýµÄËùÓÐ Employee bean¡±¡£
ÔÚÓÃÓÚ²éÕÒ·½·¨µÄ EJB QL Óï¾äÖУ¬²»ÐèҪʹÓà SELECT ×Ó¾äÀ´±íÃ÷ҪѡÔñµÄÄÚÈÝ¡£ÕâÊÇÒòΪ£¬²éÕÒ·½·¨½«×ÜÊÇÑ¡ÔñÓëÆä×ÔÉíµÄ bean ÀàÐÍÏàͬµÄÔ¶³ÌÒýÓá£ÔÚÕâÖÖÇé¿öÏ£¬¾Í¿ÉÒÔÈÏΪѡÔñÓï¾ä½«·µ»ØÔ¶³Ì Employee bean µÄÈ«²¿ÒýÓá£
Èç¹û¸÷ÖÖ²éÕÒ·½·¨¶¼Ò»Æð²¿ÊðÔÚͬһ¸ö ejb-jar ÎļþÖУ¬²¢ÇÒÆä¼ä¾ßÓпɵ¼º½µÄʵ¼Ê¹Øϵ£¬ÄÇôÕâЩ²éÕÒ·½·¨¾ÍÉõÖÁ¿ÉÒÔ¿çÔ½µ½ÁíһЩ bean µÄ³éÏó³Ö¾ÃÐÔ·½°¸ÖÐÈ¥¡£ÀýÈ磬findByInvestment() ·½·¨½«ÒªÇó¸Ã²éÕÒ²éѯ´Ó Employee µ¼º½µ½Í¶×Ê bean
µÄ³éÏó³Ö¾ÃÐÔ·½°¸ÖÐÈ¥¡£ÉùÃ÷À´±í´ïÕâÖÖ²éÕÒ²Ù×÷µÄ EJB QL Óï¾äÈçÏÂËùʾ¡£
FROM element IN benefit.investments WHERE element.name
= ?1
ÒÔÉÏÓï¾äÊÇ˵£º¡°Ñ¡ÔñÈ«²¿ÕâÑùµÄ Employee bean£ºÆä»ñÀû´ÓÊô¶ÔÏóÖÁÉÙ°üº¬Ò»¸öͶ×Ê bean µÄÒýÓ㬲¢ÇÒÆäÃû³ÆµÈÓÚ findByInvestment() ·½·¨µÄ investmentName ²ÎÊý¡£¡±
EJB QL ºÍÑ¡Ôñ·½·¨
EJB QL Ò²ÓÃÓÚÒ»ÖÖ³ÆΪ ejbSelect ·½·¨µÄвéѯ·½·¨ÖУ¬¸Ã·½·¨ÀàËÆÓÚ²éÕÒ·½·¨£¬Ö»ÊÇËü½ö¹© bean ÀàʹÓ᣸÷½·¨²»ÔÚ±¾µØ½Ó¿ÚÖÐÉùÃ÷£¬ËùÒÔÒ²²»ÏÔ¶¸ø¿Í»§»ú¡£´ËÍ⣬ejbSelect ·½·¨¿É·µ»Ø·¶Î§¸ü´óµÄ¸÷ÖÖÖµ£¬¶ø²»½öÏÞÓÚ bean ±¾ÉíµÄÔ¶³Ì½Ó¿ÚÀàÐÍ¡£
´æÔÚÁ½ÖÖÑ¡Ôñ·½·¨£ºejbSelect<METHOD> ºÍ ejbSelect<METHOD>InEntity¡£ejbSelect<METHOD> ·½·¨ÊÇÈ«¾ÖÖ´Ðеģ¬ÕâÊÇÖ¸ÕâÖÖ·½·¨²¢·ÇרÓÃÓÚÖ´Ðи÷½·¨µÄ bean ʵÀý¡£ejbSelect<METHOD>InEntity ·½·¨ÔòרÓÃÓÚÖ´Ðи÷½·¨µÄʵÌåʵÀý¡£ÕâЩѡÔñ·½·¨ÔÚ bean
ÀàÖб»ÉùÃ÷Ϊ³éÏó·½·¨£¬²¢ÔÚÕâЩÀàµÄÒµÎñ·½·¨ÖÐʹÓá£ÏÂÃæÊÇ ejbSelect<METHOD> ·½·¨ºÍ ejbSelect<METHOD>InEntity ·½·¨µÄʾÀý£¬Í¬Ê±ËµÃ÷ÁË¿ÉÒÔÈçºÎÔÚÒµÎñ·½·¨ÖÐʹÓÃËüÃÇ¡£
public abstract class EmployeeBean implements
javax.ejb.EntityBean {
...
// ejbSelectInEntity
public abstract Collection
ejbSelectInvestmentsInEntity (String risk);
// ejbSelect
public abstract Collection
ejbSelectInvestments(String risk);
...
}
ÔÚÉÏÃæµÄÉùÃ÷ÖУ¬Á½ÖÖÑ¡Ôñ·½·¨ÔËÐÐÓÚ²»Í¬µÄ·¶Î§¡£ejbSelectInvestmentsInEntity() ½öÔÚµ±Ç°µÄ Employee bean ʵÀýÉÏÖ´ÐУ¬ËùÒÔËüÖ»·µ»Ø¹ÍÔ±µÄ·çÏÕͶ×Ê¡£
SELECT invest FROM invest IN benefit.investments WHERE
invest.type = ?1
ÁíÒ»·½Ã棬ejbSelect<METHOD> ·½·¨µÄ·¶Î§ÔòÊÇÈ«¾ÖÐԵģ¬ËùÒÔͬһ¸ö²éѯ½«·µ»ØÕû¸öÆóÒµÄÚËùÓйÍÔ±µÄÈ«²¿·çÏÕͶ×Ê¡£
ejbSelect<METHOD>InEntity Ñ¡Ôñ·½·¨¿ÉÒÔ·µ»Ø bean µÄÔ¶³ÌÀàÐÍ£¨ÈçÔÚÉÏÃæµÄ²éѯÖÐÄÇÑù£©¡¢´ÓÊô¶ÔÏó»òÈκÎÆäËü Java ÀàÐÍ¡£ÁíÒ»·½Ã棬ȫ¾ÖÑ¡Ôñ·½·¨Ôò²»ÄÜ·µ»Ø bean µÄ´ÓÊô¶ÔÏóÀàÐÍ¡£
Ñ¡Ôñ·½·¨µÄ EJB QL Óï¾äÒªÇóʹÓà SELECT ×Ӿ䣬ÒòΪËüÃÇÄܹ»·µ»Ø·¶Î§¸ü¹ãµÄ¸÷ÖÖÖµ¡£
Ð嵀 ejbHome ·½·¨
ÔÚ EJB 2.0 ÖУ¬ÊµÌå bean ¿ÉÒÔÉùÃ÷һЩ ejbHome ·½·¨£¬ÓÃÀ´Ö´ÐÐÓë EJB ×é¼þÏà¹ØµÄ²Ù×÷£¬µ«²¢²»×¨ÓÃÓÚij¸ö bean ʵÀý¡£ÔÚ bean ÀàÖж¨ÒåµÄ ejbHome ·½·¨ÔÚ±¾µØ½Ó¿ÚÖбØÐëÓÐÒ»¸öÓëÆäÏàÆ¥ÅäµÄ±¾µØ·½·¨¡£ÏÂÃæµÄ´úÂë˵Ã÷ÁËÒ»¸ö±¾µØ·½·¨£¬ËüÕýÊÇ×÷Ϊ Employee bean
µÄ±¾µØ½Ó¿Ú¶¨ÒåµÄ¡£applyCola() ·½·¨ÓÃÀ´¸ù¾Ý×î½ü COLA£¨Éú»î·ÑÓõ÷Õû£©µÄÔö³¤À´¸üÐÂËùÓйÍÔ±µÄнˮ¡£
public interface EmployeeHome extends javax.ejb.EJBHome
{
// ±¾µØ·½·¨
public void applyCola(double increate) throws
RemoteException;
...
}
applyCola() ·½·¨ÔÚ bean ÀàÖбØÐëÓÐÆ¥ÅäµÄ ejbHome ·½·¨£¬Ëü±»ÉùÃ÷Ϊ ejbHomeApplyCola()¡£ejbHomeApplyCola() ·½·¨²¢·ÇרÓÃÓÚÒ»¸ö bean ʵÀý£¬ËüµÄ·¶Î§ÊÇÈ«¾ÖµÄ£¬ËùÒÔËü½«¶ÔËùÓйÍÔ±µÄнˮʹÓÃͬһ¸ö COLA¡£
public abstract class EmployeeBean implements
javax.ejb.EntityBean {
...
// ejbHome ·½·¨
public void ejbHomeApplyCola (double increase ){
Collection col = ejbSelectAllEmployees ();
Iterator employees = col.iterator();
while(employees.next()){
Employee emp =
(Employee)employees.next();
double salary =
emp.getAnnualSalary();
salary = salary + (salary*increase);
emp.setAnnualSalary(salary);
}
}
}
bean µÄ¿ª·¢ÈËÔ±ÐèҪΪ BMP ºÍ CMP ʵÌå bean ¶¼ÊµÏÖ ejbHome ·½·¨¡£CMP ʵÏÖ¿ÉÄÜÔںܴó³Ì¶ÈÉÏÒªÒÀÀµÓÚÈ«¾ÖµÄÑ¡ÔñÓï¾ä£¨ÈçÉÏÃæËù˵Ã÷µÄÄÇÑù£©ºÍ finder ·½·¨£¬¶ø ejbHome µÄ BMP ʵÏÖÔò½«Ê¹ÓÃÖ±½ÓÊý¾Ý¿â·ÃÎÊºÍ bean µÄ finder ·½·¨£¬À´²éѯÊý¾ÝºÍ½øÐиü¸Ä¡£
MessageDrivenBean
ÔÚ EJB 2.0 ÖУ¬¶Ô¹æ·¶µÄÒ»¸ö»ù´¡ÐÔ¸ü¸ÄÊÇÌí¼ÓÁËÒ»ÖÖȫеÄÆóÒµ¼¶ bean ÀàÐÍ£¬¼´ MessageDrivenBean¡£MessageDrivenBean רÃÅÉè¼ÆÀ´´¦ÀíÈëÍøµÄ JMS ÏûÏ¢¡£¶ÔÓÚÐí¶à¿ª·¢ÈËÔ±À´Ëµ£¬JMS ÊÇÒ»ÖÖеķ¶Àý£¬ËùÒÔ±¾ÎĽ«»¨Ò»Ð©Ê±¼äÖð²½ËµÃ÷¶Ô JMS µÄÀí½â£¬ÒÔ¼°ËüÃÇÔÚ EJB 2.0
ÖеÄÓ÷¨¡£
ʲôÊÇ JMS£¿
JMS ÊÇÒ»ÖÖÓ볧ÉÌÎÞ¹ØµÄ API£¬ÓÃÀ´·ÃÎÊÏûÏ¢ÊÕ·¢ÏµÍ³¡£ËüÀàËÆÓÚ JDBC (Java Database Connectivity)£ºÕâÀJDBC ÊÇ¿ÉÒÔÓÃÀ´·ÃÎÊÐí¶à²»Í¬¹ØϵÊý¾Ý¿âµÄ API£¬¶ø JMS ÔòÌṩͬÑùÓ볧ÉÌÎ޹صķÃÎÊ·½·¨£¬ÒÔ·ÃÎÊÏûÏ¢ÊÕ·¢·þÎñ¡£Ðí¶à³§ÉÌÄ¿Ç°¶¼Ö§³Ö JMS£¬°üÀ¨ IBM µÄ
MQSeries¡¢BEA µÄ Weblogic JMS service ºÍ Progress µÄ SonicMQ£¬ÕâÖ»ÊǼ¸¸öÀý×Ó¡£
JMS ʹÄúÄܹ»Í¨¹ýÏûÏ¢ÊÕ·¢·þÎñ£¨ÓÐʱ³ÆΪÏûÏ¢Öнé³ÌÐò»ò·ÓÉÆ÷£©´ÓÒ»¸ö JMS ¿Í»§»úÏòÁíÒ»¸ö JML ¿Í»§»ú·¢ËÍÏûÏ¢¡£ÏûÏ¢ÊÇ JMS
ÖеÄÒ»ÖÖÀàÐͶÔÏó£¬ÓÉÁ½²¿·Ö×é³É£º±¨Í·ºÍÏûÏ¢Ö÷Ìå¡£±¨Í·ÓÉ·ÓÉÐÅÏ¢ÒÔ¼°ÓйظÃÏûÏ¢µÄÔªÊý¾Ý×é³É¡£ÏûÏ¢Ö÷ÌåÔòЯ´ø×ÅÓ¦ÓóÌÐòµÄÊý¾Ý»òÓÐЧ¸ºÔØ¡£¸ù¾ÝÓÐЧ¸ºÔصÄÀàÐÍÀ´»®·Ö£¬¿ÉÒÔ½«ÏûÏ¢·ÖΪ¼¸ÖÖÀàÐÍ£¬ËüÃÇ·Ö±ðЯ´ø£º¼òµ¥Îı¾ (TextMessage)¡¢¿ÉÐòÁл¯µÄ¶ÔÏó
(ObjectMessage)¡¢ÊôÐÔ¼¯ºÏ (MapMessage)¡¢×Ö½ÚÁ÷ (BytesMessage)¡¢ÔʼֵÁ÷ (StreamMessage)£¬»¹ÓÐÎÞÓÐЧ¸ºÔصÄÏûÏ¢ (Message)¡£
ÏûÏ¢ÊÕ·¢ÏµÍ³ÊÇÒì²½µÄ£¬Ò²¾ÍÊÇ˵£¬JMS ¿Í»§»ú¿ÉÒÔ·¢ËÍÏûÏ¢¶ø²»±ØµÈ´ý»ØÓ¦¡£±È½Ï¿ÉÖª£¬ÕâÍêÈ«²»Í¬ÓÚ»ùÓÚ RPC µÄ£¨»ùÓÚÔ¶³Ì¹ý³ÌµÄ£©ÏµÍ³£¬Èç EJB 1.1¡¢CORBA ºÍ Java RMI µÄÒýÓÃʵÏÖ¡£ÔÚ RPC
ÖУ¬¿Í»§»úµ÷Ó÷þÎñÆ÷ÉÏij¸ö·Ö²¼Ê½¶ÔÏóµÄÒ»¸ö·½·¨¡£ÔÚ·½·¨µ÷Ó÷µ»Ø֮ǰ£¬¸Ã¿Í»§»ú±»×èÈû£»¸Ã¿Í»§»úÔÚ¿ÉÒÔÖ´ÐÐÏÂÒ»ÌõÖ¸Áî֮ǰ£¬±ØÐëµÈ´ý·½·¨µ÷ÓýáÊø¡£ÔÚ JMS ÖУ¬¿Í»§»ú½«ÏûÏ¢·¢Ë͸øÒ»¸öÐéÄâͨµÀ£¨Ö÷Ìâ»ò¶ÓÁУ©£¬¶øÆäËü JMS ¿Í»§»úÔòÔ¤¶©»ò¼àÌýÕâ¸öÐéÄâͨµÀ¡£µ± JMS
¿Í»§»ú·¢ËÍÏûϢʱ£¬Ëü²¢²»µÈ´ý»ØÓ¦¡£ËüÖ´Ðз¢ËͲÙ×÷£¬È»ºó¼ÌÐøÖ´ÐÐÏÂÒ»ÌõÖ¸Áî¡£ÏûÏ¢¿ÉÄÜ×îÖÕת·¢µ½Ò»¸ö»òÐí¶à¸ö¿Í»§»ú£¬ÕâЩ¿Í»§»ú¶¼²»ÐèÒª×÷³ö»ØÓ¦¡£
EJB 2.0 ÖÐµÄ JMS
EJB 2.0 ÒÔÁ½ÖÖ·½Ê½Ö§³Ö JMS µÄ¼¯³É£º×÷ΪһÖÖ bean ¿ÉÓõÄ×ÊÔ´£¬ºÍ×÷Ϊһ¸ö MessageDrivenBean¡£µ±½« JMS ÓÃ×÷Ò»ÖÖ×ÊԴʱ£¬Ê¹Óà JMS API µÄ bean ¾ÍÊÇÏûÏ¢µÄ²úÉúÕß»ò·¢ËÍÕß¡£ÔÚÕâÖÖÇé¿öÏ£¬bean ½«ÏûÏ¢·¢Ë͸ø³ÆΪÖ÷Ìâ»ò¶ÓÁеÄÐéÄâͨµÀ¡£ÁíÒ»·½Ã棬MessageDrivenBean
ÔòÊÇÏûÏ¢µÄʹÓÃÕß»ò½ÓÊÕÕß¡£Ëü¼àÌýÌض¨µÄÐéÄâͨµÀ£¨Ö÷Ìâ»ò¶ÓÁУ©£¬²¢´¦Àí·¢Ë͸ø¸ÃͨµÀµÄÏûÏ¢¡£ÎªÁ˸üºÃµØÀí½âÏûÏ¢²úÉúÕߺÍÏûϢʹÓÃÕßµÄ×÷Óã¬Óà SessionBean bean À´·¢ËÍÒ»ÌõʹÓà JMS µÄÏûÏ¢£¬È»ºóʹÓÃÒ»¸öÐ嵀 MessageDrivenBean À´Ê¹ÓøÃͬһÌõÏûÏ¢¡£
×÷Ϊ EJB 2.0 ×ÊÔ´µÄ JMS
»á»° bean ºÍʵÌå bean ¶¼ÊÇ»ùÓÚ RPC µÄ×é¼þ£¬ÎªÁ˽«¸÷ÖÖÊÂÎñÐÔµÄ×é¼þ×°Åäµ½Ò»Æð£¬ÕâÊÇÒ»ÖÖ׿ԽµÄÌåϵ½á¹¹¡£µ«ÊÇ£¬ÔÚijЩÇé¿öÏ£¬RPC µÄͬ²½ÐÔÖÊ»á³ÉΪһÖÖÕÏ°£¬ÕâÕýÊÇ EJB 1.1 Öн«¶Ô JMS API µÄ·ÃÎÊ×÷ΪһÖÖ×ÊÔ´°üÀ¨ÔÚÄÚµÄÔÒò¡£ÀûÓà JNDI »·¾³ÃüÃûµÄÉÏÏÂÎÄ£¬bean
¿ÉÒÔ»ñµÃÒ»¸ö JMS ¹¤³§£¬²¢½«Ò»ÌõÒì²½ÏûÏ¢·¢Ë͸øÖ÷Ìâ»ò¶ÓÁУ¨Ò²´Ó JNDI »ñµÃ£©£¬¶ø²»±ØµÈ´ý»ØÓ¦¡£ÏÂÃæÊÇ ShoppingCart bean µÄÒ»¸öÀý×Ó£¬ËüʹÓà JMS ½« Order µÄÏêϸÐÅÏ¢·¢Ë͸øÏûÏ¢ÊÕ·¢Ö÷Ìâ¡£
public class ShoppingCartBean implements SessionBean {
// ¶©µ¥ÏêϸÐÅÏ¢ÊÇÒ»¸ö¿ÉÐòÁл¯µÄ¶ÔÏó£¬Ëü°üº¬È«²¿¶©µ¥ÐÅÏ¢¡£
public OrderDetail orderDetail;
public void processOrder(){
// ´¦Àí¶©µ¥µÄÂß¼´Ó´Ë´¦¿ªÊ¼
....
// ... ´¦Àí¶©µ¥ÒÔºó£¬ÏòÆäËüϵͳ·¢ËÍÓйش˶©µ¥µÄÒ»ÌõÏûÏ¢
InitialContext jndiEnc = new
InitialContext();
// ʹÓà JNDI ENC »ñÈ¡ JMS ¹¤³§ºÍÖ÷Ìâ±êʶ·û
TopicConnectionFactory factory =
jndiEnc.lookup("java:comp/env/jms/topicfactory");
Topic orderTopic =
jndiEnc.lookup("java:comp/env/jms/ordertopic");
// »ñµÃÒ»¸öÓÃÀ´·¢ËÍÏûÏ¢µÄ·¢²¼Õß
TopicConnection con =
factory.createTopicConnection();
TopicSession session =
con.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE );
TopicPublisher publisher =
session.createPublisher(orderTopic);
// ½«Ò»¸ö ObjectMessage ·¢Ë͸øÖ÷Ì⣨ÐéÄâͨµÀ£©
ObjectMessage message =
session.createObjectMessage();
message.setObject(orderDetail);
publisher.publish(message);
con.close();
}
...
}
ÔÚÕâÖÖÇé¿öÏ£¬JMS ÊÇÓÃÀ´Í¨ÖªÁíÍâµÄÓ¦ÓóÌÐò£¬¶©µ¥Òѱ»´¦Àí¡£ÕâЩÁíÍâµÄÓ¦ÓóÌÐò¶ÔÓÚ´¦Àí¶©µ¥À´Ëµ²¢²»ÖØÒª£¬µ«ËüÃÇ»áÒòΪµÃµ½Ò»¸ö¶©µ¥Òѱ»´¦ÀíµÄ֪ͨ¶øÊÜÒæ¡£ÕâÑùµÄÀý×Ó°üÀ¨×Ô¶¯µ÷Õû¿â´æµÄ¿â´æϵͳ£¬ºÍÄܽ«¿Í»§Ìí¼Ó½ø²úƷĿ¼ÓʼÄÃûµ¥ÖеÄÏúÊÛÓ¦ÓóÌÐò¡£
ʹÓà JMS ʹ bean Äܹ»·¢²¼£¨·¢ËÍ£©ÏûÏ¢¶ø²»»á·¢Éú×èÈû¡£bean
²¢²»ÖªµÀ˽«ÊÕµ½ÏûÏ¢£¬ÒòΪËüÊǽ«ÏûÏ¢·¢Ë͸øij¸öÖ÷Ì⣨ÐéÄâͨµÀ£©£¬¶ø²»ÊÇÖ±½Ó·¢Ë͸øÁíÒ»¸öÓ¦ÓóÌÐò¡£Ó¦ÓóÌÐò¿ÉÒÔÑ¡ÔñÔ¤¶©¸ÃÖ÷Ì⣬²¢½ÓÊÕÓйØж©µ¥µÄ֪ͨ¡£ÕâÑù¾ÍÓпÉÄܶ¯Ì¬µØÔÚÐéÄâͨµÀÖÐÌí¼Ó»òɾ³ýÓ¦ÓóÌÐò£¬´Ó¶ø²úÉúÁËÒ»ÖÖ¸ü¼ÓÁé»îµÄϵͳ¡£
Ô¤¶©Á˶©µ¥Ö÷ÌâµÄÓ¦ÓóÌÐò½«ÊÕµ½ÓйØж©µ¥µÄ֪ͨ£¬Ó¦ÓóÌÐò¿ÉÒÔʹÓÃËüÃÇÈÏΪºÏÊʵÄÈκη½Ê½À´´¦ÀíÕâ¸ö֪ͨ¡£Ô¤¶©Á˸÷ÖÖÖ÷ÌâµÄÓ¦ÓóÌÐò»òÕß´Ó¸÷¸ö¶ÓÁÐÖнÓÊÕÏûÏ¢µÄÓ¦ÓóÌÐò¿ÉÒÔÊÇ Java Ó¦ÓóÌÐò¡¢EAI ϵͳ£¨ÓÃÓÚ¼¯³ÉÒÅÁôϵͳºÍ ERP ϵͳ£©»òÕß MessageDrivenBean ×é¼þ£¬ÔÚ
JMS µÄÊõÓïÖУ¬ËüÃÇÈ«²¿±»ÈÏΪÊÇ JMS ¿Í»§»ú¡£
JMS ºÍ MessageDrivenBean
ËäÈ»´ó¶àÊý JMS ³§É̶¼ÌṩÏûÏ¢Öн鹤¾ß£¬À´½«ÏûÏ¢´Ó·¢ËÍÕß·Óɵ½½ÓÊÕÕߣ¬µ«¹¹½¨Ê¹Ó㨽ÓÊÕ£©ÏûÏ¢µÄ JMS ¿Í»§»úÈ´ÊÇÓ¦ÓóÌÐò¿ª·¢ÈËÔ±µÄÖ°Ôð¡£ÔÚÐí¶àÇé¿öÏ£¬½ÓÊÕÏûÏ¢µÄÓ¦ÓóÌÐò±ØÐëÇ¿½¡¡¢°²È«¡¢¿ìËÙ¶øÇÒ¿ÉÉìËõ£»ËüÐèÒªµÄ»ù´¡½á¹¹»ù±¾ÉÏÓë EJB Ó¦ÓóÌÐòÏàͬ¡£
ÓÉÓÚÈÏʶµ½ÕâÖÖÐèÒª£¬EJB 2.0 ÏÖÔÚ°üÀ¨ÁË MessageDrivenBean ÀàÐÍ£¬Ëü¿ÉÒÔʹÓà JMS ÏûÏ¢£¬²¢ÇÒÔÚͬһ¸öÇ¿½¡µÄ¡¢»ùÓÚ×é¼þµÄ»ù´¡½á¹¹Öд¦ÀíÕâЩÏûÏ¢£¬ÕâÑùµÄ»ù´¡½á¹¹¶ÔÓڻỰ bean ºÍʵÌå bean ¶¼·Ç³£ÓÐÓá£MessageDrivenBean ÀàÐÍ£¨ÏûÏ¢ bean£©ÊÇÒ»ÖÖÆóÒµ¼¶ bean
×é¼þ£¬ËüÉè¼ÆÀ´Ê¹ÓÃÒì²½µÄ JMS ÏûÏ¢¡£
³ýÁËÌṩÈÝÆ÷»ù´¡½á¹¹ÒÔÍ⣬EJB »¹¾ßÓÐÁíÒ»¸öÖØÒªµÄÓŵ㣺²¢·¢´¦Àí¡£ÔÚ EJB ÖУ¬Ò»¸öÒѲ¿ÊðµÄÏûÏ¢ bean ±íʾһ¸öµ¥Ò»µÄÏûϢʹÓÃÕߣ¬µ«Õâ¸ö bean ±¾ÉíÊÇÓÉÐí¶à bean ʵÀýÌṩ·þÎñµÄ¡£Ã¿¸ö bean ʵÀý¶¼¿ÉÒÔ·Ö±ðµØʹÓÃÏûÏ¢ bean ½ÓÊÕµ½µÄÏûÏ¢¡£ÕâÒâζ×Å£¬ÏûÏ¢ bean
²»±ØÏñ³£¹æ JMS ¿Í»§»úÄÇÑùÁ¬ÐøµØʹÓÃÏûÏ¢¡£ÏûÏ¢ bean ¿ÉÒÔ²¢·¢µØʹÓýÓÊÕµ½µÄ¶à¸öÏûÏ¢£¬ÕâÑù¾ÍÄÜ´ïµ½±È´«Í³ JMS Ó¦ÓóÌÐò¸ßµÃ¶àÍÌÍÂÁ¿ºÍºÃµÃ¶àµÄ¿ÉÉìËõÐÔ¡£
ΪÁË˵Ã÷ÏûÏ¢ bean µÄ×÷Ó㬾Ϳª·¢ÁË MarketingBean À࣬²¢½«Ëü´Ó¶©µ¥Ö÷ÌâÖв¿Êðµ½¹©Ê¹ÓõÄÏûÏ¢ÖÐÈ¥¡£MarketingBean ½«´ÓÏûÏ¢ÖÐÌáÈ¡ OrderDetail ¶ÔÏ󣬲¢Ê¹ÓÃËü½«¿Í»§Ìí¼Óµ½Êʵ±µÄĿ¼ÓʼÄÃûµ¥ÖС£ÕâÊÇÒ»ÖÖ×ÖµĴóÁ¿ÓʼÄϵͳ¡£
ÏÂÃæÊÇ MarketingBean ÀàµÄ¶¨Ò壬Õâ¸öÀàʹÓ÷¢²¼¸ø¶©µ¥Ö÷ÌâµÄÏûÏ¢¡£
public class MarketingBean implements
javax.ejb.MessageDrivenBean {
public void onMessage(Message message) {
ObjectMessage orderMessage =
(ObjectMessage)orderMessage:
OrderDetail orderDetail =
(OrderDetail)orderMessage.getObject();
Integer customerID =
orderDetail.getCustomerID();
InitialContext jndiEnc = new
InitialContext();
CatalogHome catalogHome =
(CatalogHome)jndiEnc.lookup("java:comp/env/ejb/catalog");
Iterator productIDs =
orderDetail.getProductsIDs();
while(productIDs.hasNext()){
Integer productID =
(Integer)productIDs.next();
Catalog cat =
CatalogHome.findByProductID(productID);
cat.addCustomerToMailingList(customerID);
}
}
}
ÕýÏñ»á»° bean ºÍʵÌå bean Ò»Ñù£¬MessageDrivenBean Ò²ÊÇÒ»ÖÖÍ걸µÄÆóÒµ¼¶ bean£¬µ«Æä¼äÈÔ´æÔÚһЩÖØÒªµÄÇø±ð¡£ÏûÏ¢ bean ûÓÐÔ¶³Ì½Ó¿Ú»ò±¾µØ½Ó¿Ú¡£ÕâÊÇÒòΪÏûÏ¢ bean ²»ÊÇ RPC ×é¼þ¡£ËüûÓй© EJB ¿Í»§»úµ÷ÓõÄÒµÎñ·½·¨¡£ÏûÏ¢ bean
¼àÌýÐéÄâÏûϢͨµÀ£¨Ö÷Ìâ»ò¶ÓÁУ©£¬²¢Ê¹ÓÃÆäËü JMS ¿Í»§»ú·¢Ë͸ø¸ÃͨµÀµÄÏûÏ¢¡£
¸÷¸öÏûÏ¢ bean ¹¹³ÉÒ»¸ö bean À࣬Õâ¸öÀàʵÏÖ MessageDrivenBean ½Ó¿ÚºÍÒ»¸ö XML ²¿ÊðÃèÊö·û¡£ÏÂÃæÊÇ MessageDrivenBean ½Ó¿ÚµÄ¶¨Ò壬ËùÓÐÏûÏ¢ bean ¶¼±ØÐëʵÏÖÕâ¸ö½Ó¿Ú¡£
package javax.ejb;
import javax.jms.Message;
import javax.jms.MessageListener;
public interface MessageDrivenBean extends
MessageListener{
public void onMessage(Message message);
public void ejbCreate();
public void ejbRemove();
public void
setMessageDrivenContext(MessageDrivenContext mdc);
}
µ±²¿ÊðÁËÒ»¸öÏûÏ¢Çý¶¯µÄ bean ÒÔºó£¬Ëü¾Í±»Ö¸ÅÉÀ´´¦ÀíÌض¨Ö÷Ìâ»ò¶ÓÁÐÖеÄÏûÏ¢¡£JMS ¿Í»§»ú£¨Java Ó¦ÓóÌÐò¡¢bean »ò±¾µØ¿Í»§»ú£©·¢Ë͵ÄÈκÎÏûÏ¢£¬½«ÓÉÏûϢ·ÓÉÆ÷ת·¢¸øÏûÏ¢ bean£¬¸ÃÏûÏ¢ bean ÕýÊDZ»Ö¸ÅÉÀ´´Ó¸ÃÐéÄâͨµÀÖнÓÊÕÏûÏ¢µÄ¡£µ±Ò»ÌõÏûÏ¢±»·¢Ë͸øÒ»¸öÏûÏ¢ bean
ʱ£¬EJB ÈÝÆ÷¾Í»á´Óij¸ö³ØÖÐÑ¡Ôñ¸Ã bean µÄÒ»¸öʵÀý£¬À´´¦ÀíÕâÌõÏûÏ¢¡£µ± bean ʵÀýµ÷ÓÃÆä onMessage() ·½·¨Ê±£¬Ëü¾Í»á½ÓÊÕµ½ÕâÌõÏûÏ¢£¬²¢Äܹ»ÒÔËüÈÏΪºÏÊʵÄÈκη½Ê½À´´¦ÀíÕâÌõÏûÏ¢¡£Ò»µ©ÕâÌõÏûÏ¢±»Ê¹Óã¬ÔòÖ»ÒªÊÂÎñûÓÐÒì³£ÖÐÖ¹£¬ÕâÌõÏûÏ¢¶¼²»»á±»´«Ë͸øÕâ¸öÏûÏ¢ bean
µÄÈκÎÆäËüʵÀý¡£
ÏûÏ¢ bean ÔÚijµãÉÏÀàËÆÓÚÎÞ״̬µÄ»á»° bean£¬¼´ÕâÁ½ÖÖ bean ÔÚÁ½´ÎÇëÇóÖ®¼ä¶¼²»±£³ÖÈκÎ״̬¡£Òò´Ë£¬ÏûÏ¢Çý¶¯µÄ bean ÊÇÎÞ״̬µÄ£¬µ«ÊÇ£¬¾ÍÏñÎÞ״̬µÄ»á»° bean Ò»Ñù£¬ËüÃÇÒ²¿ÉÒÔÓÐʵÀý±äÁ¿£¬ÕâЩ±äÁ¿ÔÚÕâ¸ö bean ʵÀýµÄÕû¸öÉú´æÆÚÄÚ¾ù±£³Ö¡£
¶ÔÏûÏ¢ bean µÄ×îºóÒ»µã˵Ã÷ÊÇ£¬Àí½âÕâÑùÒ»¸öÊÂʵÊǺÜÖØÒªµÄ£¬¼´ bean ʹÓõÄÏûÏ¢²»Ò»¶¨ÒªÊÇÓÉÆäËü bean Ëù²úÉúµÄ¡£ÏûÏ¢ bean ¿ÉÒÔʹÓÃÓÉ·ûºÏ JMS µÄ³§ÉÌÌṩµÄÈκÎÖ÷Ìâ»ò¶ÓÁÐÖеÄÏûÏ¢¡£ÏûÏ¢ bean ʹÓõÄÏûÏ¢¿ÉÒÔÀ´×ÔÆäËü bean£¨»á»° bean¡¢ÊµÌå bean »òÏûÏ¢ bean£©¡¢·Ç
EJB µÄ Java Ó¦ÓóÌÐò¡¢»òÕßÉõÖÁ·Ç Java µÄÓ¦ÓóÌÐò£¨Èç¹ûÆ乩ӦÉÌ·ûºÏ JMS£©¡£ÀýÈ磬ÒÅÁôÓ¦ÓóÌÐò¿ÉÄÜʹÓà IBM µÄ MQSeries Ïò¶ÓÁз¢ËÍÏûÏ¢£¬¶ø¸ÃÏûÏ¢¼È¿ÉÒÔÓÉÆäËüÒÅÁôÓ¦ÓóÌÐòʹÓã¬Í¬Ñù¿ÉÒÔÓÉÏûÏ¢ bean ʹÓá£
½áÂÛ
ÓëÒÔÇ°µÄ¹æ·¶Ïà±È£¬Enterprise JavaBeans 2.0 ÖÐ×÷ÁËһЩÏ൱´óµÄ¸ü¸Ä¡£Ð嵀 CMP
Ä£ÐͱÈÒÔÇ°µÄÄ£ÐÍÒªÁé»îµÃ¶à£¬ËüÔÊÐí¸÷ÖÖʵÌåΪ¸´ÔӵĶÔÏóͼ½¨Á¢Ä£ÐÍ£¬¶øͬÓÖÌṩ¿çÈÝÆ÷µÄ¸ü´óµÄ¿ÉÒÆÖ²ÐÔ¡£ÈËÃÇÆÈÇеØÆÚ´ý×ÅΪ²éÕÒºÍÑ¡Ôñ²Ù×÷¶¨ÒåÒ»ÖÖͨÓõIJéѯÓïÑÔ£¬¶øËüÒ²½«ÓÐÖúÓÚÌá¸ß¿ÉÒÆÖ²ÐÔ¡£
ÕâÖÖÐ嵀 MessageDrivenBean ÀàÐͽ«ÓÐÖúÓÚʹÕâÖÖÇ¿´óµÄÏûÏ¢ÊÕ·¢·¶Àý³ÉΪÖÚÈËÖõÄ¿µÄ½¹µã£¬¾ÍÏñ EJB ÄÇÑù¡£ÏûÏ¢ÊÕ·¢ÔÚ·Ö²¼Ê½µÄ»ìºÏ¼ÆËãÖÐÊÇÒ»¸ö¼«ÆäÖØÒªµÄ×é³É²¿·Ö£¬½«Ëü°üÀ¨ÔÚ EJB ÄÚ¾ÍÊÇÆäÖØÒªÐÔµÄÒ»¸öÖ¤Ã÷¡£
ÔÚдÕâƪÎÄÕÂʱ£¬EJB 2.0 ¸Õ¸Õ×÷Ϊ¹«¿ª²Ý°¸·¢²¼£¬ÕâÒâζ×ÅÔÚËü³ÉΪһ¸ö×îÖչ淶֮ǰÈÔÓпÉÄܸü¸Ä¡£Èç¹û¸ü¸Ä¶Ô´Ë´¦ÌṩµÄ²ÄÁÏÓÐÖØ´óÓ°Ï죬½ìʱÎÒ½«Éè·¨¶Ô±¾ÎÄ×÷һЩעÊÍ£¬µ«Õâ¸ö¹æ·¶ÕýÔÚÇ÷ÓÚÎȶ¨£¬ËùÒÔ²»Ì«¿ÉÄÜÓÐÕæÕýÖØ´óµÄ¸ü¸Ä¡£
²Î¿¼×ÊÁÏ
"A Beginner's Guide to Enterprise JavaBeans," Mark Johnson£¨JavaWorld£¬1998 Äê 10 Ô£©£º
Richard Monson-Haefel µÄ EJB ¿ª·¢ÕßÍøÕ¾£¬EJBNow.com
EJB 2.0£¬¹æ·¶
Thought µÄ CocoBase
IBM µÄ MQ Series
BEA µÄ WebLogic JMS Service
Progess Sonic MQ
Richard Monson-Haefel ËùдµÄÆäËüÎÄÕ£º
"Create forward-compatible beans in EJB, Part 1"£¨JavaWorld£¬1999 Äê 12 Ô£©
"Create forward-compatible beans in EJB, Part 2"£¨JavaWorld£¬2000 Äê 1 Ô£©
×÷Õß¼ò½é
Richard Monson-Haefel ÊÇ×î½ü·¢²¼µÄ Enterprise JavaBeans µÚ¶þ°æµÄ×÷Õß¡£ËûÊÇ OpenEJB µÄÊ×ϯÉè¼Æʦ£¨OpenEJB ÊÇÒ»ÖÖ¿ª·ÅÔ´´úÂëµÄ Enterprise JavaBeans 2.0 ÈÝÆ÷£©£¬ËûÔø¾ÒÔÉè¼ÆʦÉí·ÝΪ Enterprise JavaBeans¡¢CORBA¡¢Java RMI ÒÔ¼°ÆäËü Java
·½°¸Ìṩ×Éѯ¡£Monson-Haefel »¹Î¬»¤×ÅÒ»¸öÍøÕ¾£¬¹©ÈËÃÇÌÖÂÛ Enterprise JavaBeans ºÍÏà¹ØµÄ·Ö²¼Ê½¼ÆËã¼¼Êõ¡£¿ÉÒÔͨ¹ý richard.monson-haefel@javaworld.com Óë Richard Monson-Haefel ÁªÏµ¡£
-- ÔÚʱ¼äÃæÇ°£¬Ã»ÓÐÓÀºã ------Ò»¸öÈÈ°®×ÔÓɵÄÈË
¡ù À´Ô´:¡¤¹þ¹¤´ó×϶¡Ïã bbs.hit.edu.cn¡¤[FROM: 211.100.69.28]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
Ò³ÃæÖ´ÐÐʱ¼ä£º420.294ºÁÃë