当前位置:天才代写 > tutorial > JAVA 教程 > EJB的入门课本

EJB的入门课本

2017-11-11 08:00 星期六 所属: JAVA 教程 浏览:477

副标题#e#

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;
}


#p#副标题#e#

第三步,

写真正的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");
  }
}

第四步,写一个宣布用的设置文件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

#p#副标题#e#

第五步,将你的所有文件用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接口的实现

#p#分页标题#e#

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();
}
由于和详细的目次处事、协议相关,为了到达可移植的目标,
只许多几何做一些事情,幸好一般不需要做这些事情。
/*

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式