Õ¾ÄÚËÑË÷: ÇëÊäÈëËÑË÷¹Ø¼ü´Ê

µ±Ç°Ò³Ãæ: ¿ª·¢×ÊÁÏÊ×Ò³ ¡ú Java רÌâ ¡ú EJBµÄÈëÃŽ̲Ä

EJBµÄÈëÃŽ̲Ä

ÕªÒª: µÚÒ»²½£¬Ð´Ô¶³Ì½Ó¿Ú£¨remote interface£©,¼Ì³ÐEJBObject½Ó¿Ú£¬°ÑÐèÒªµ÷ÓõÄpublic·½·¨Ð´ÔÚÀïÃæ......
<iframe align=right marginWidth=0 marginHeight=0 src="http://www.chinabyte.com/tag/cont_flash_software.html" frameBorder=0 width=360 scrolling=no height=300></iframe>¡¡¡¡1¡¢EJBµÄ¿ª·¢

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

¡¡¡¡1.1 SessionBeanµÄ¿ª·¢

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

¡¡¡¡ÀýÈ磺

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0> <tr> <td>package
jsper.ejb;

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

public
interface MyEJB extends EJBObject
{

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

}</td></tr></table>

¡¡¡¡µÚ¶þ²½£¬

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

¡¡¡¡±ÈÈ磺

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0> <tr> <td>package
jsper.ejb;

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


public interface MyEJBHome extends
EJBHome
{



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

}</td></tr></table>

¡¡¡¡µÚÈý²½£¬

¡¡¡¡Ð´ÕæÕýµÄSession BeanµÄʵÏÖ(ʵÏÖ¶¨ÒåÔÚÔ¶³Ì½Ó¿ÚÖеķ½·¨)£¬ ÐèҪʵÏÖjavax.ejb.SessionBean½Ó¿Ú

¡¡¡¡×¢Ò⣺²»ÄÜÓÃimplents

¡¡¡¡MyEJBµÄ·½Ê½Ö±½ÓʵÏÖÔ¶³Ì½Ó¿Ú,´Ë´¦²»ÓÃÅ׳öRemoteException package jsper.ejb;

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0> <tr> <td>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");
¡¡}
} </td></tr></table>

¡¡¡¡µÚËIJ½£¬Ð´Ò»¸ö·¢²¼ÓõÄÅäÖÃÎļþejb-jar.xml ÐèÒªÌṩµÄÐÅÏ¢£º

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0> <tr> <td>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.</td></tr></table>

¡¡¡¡×îºóÄ㻹¿ÉÒÔÌṩÊôÓÚ×Ô¼ºµÄÅäÖÃÐÅÏ¢¹©×Ô¼º¿ØÖÆEJBµÄ¹¤×÷·½Ê½¡£

¡¡¡¡Àý×Ó£º ¡¡¡¡¡¡

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0> <tr> <td>helloEjb
com.jsper.ejb.MyEJBHome
com.jsper.ejb.MyEJB
com.jsper.ejb.MyEJBClass
Stateless
Container</td></tr></table>

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

¡¡¡¡ejb-jar.xmlÐëÔÚ¶¥¼¶µÄMETA-INF×ÓĿ¼

¡¡¡¡Õâ¾ä»°±È½ÏÒ§×죬 ¾Ù¸öÀý×Ó

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0> <tr> <td>mylib----META-INF--*.XML
|
|com--coucouniu--ejb---EJBClass
¡¡¡¡¡¡|-EJBHome
¡¡¡¡¡¡|-EJB
¡¡¡¡¡¡
ÔÚÉú³É.jarÎļþʱ

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

¡¡¡¡Çë×¢Ò⣺

¡¡¡¡µ½ÕâÒ»²½ÎÒÃÇ×ö³öµÄ¶«Î÷¶¼ÊǺͺÍÌض¨µÄ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½Ó¿ÚµÄʵÏÖ

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0> <tr> <td>¡¡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</td></tr></table>

¡¡¡¡ÕâÑù¾Í¿É´ÓĿ¼·þÎñÖеõ½Home½Ó¿ÚµÄʵÏÖ£¬ Ò²ÊÇÎÒÃÇ×î³£Óõķ½Ê½£¬ ¿ÉÒÆÖ²ÐԺܺÃ

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

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0> <tr> <td>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();
}
ÓÉÓں;ßÌåµÄĿ¼·þÎñ¡¢Ð­ÒéÏà¹Ø£¬ÎªÁË´ ïµ½¿ÉÒÆÖ²µÄÄ¿µÄ£¬
Ö»ºÃ¶à×öһЩ¹¤×÷£¬ÐÒºÃÒ»°ã²»ÐèÒª×öÕâЩ¹¤×÷¡£

/*</td></tr></table>




¡ü·µ»ØĿ¼
ǰһƪ: JBossÖÐEJBµÄ¿ª·¢
ºóһƪ: Struts Óû§Ö¸ÄÏ