中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 综合文章
weblogic——远程/近程调用EJB的方法总结
作者:未知 时间:2005-07-27 22:40 出处:CSDN 责编:chinaitpower
              摘要:weblogic——远程/近程调用EJB的方法总结

1、      客户端程序中调用EJB
前提:EJB要实现了REMOTE接口
客户端调用的代码可以用EJB Test Client工具生成。自己写就是这个样子:


      String url="t3://localhost:7001";
     Properties prop=new Properties();
     prop.put(Context.PROVIDER_URL,url);
            prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
            prop.put(Context.SECURITY_PRINCIPAL, "
name");
         prop.put(Context.SECURITY_CREDENTIALS,"
code");
     Context
context =new InitialContext(prop);
  
//通过ejbJNDI name查找到EJBHome对象
  Object ref = context.lookup("
ejb/com/J2EE/first/ejb/HelloHome ");   
   //
得到EJBHome
 
HelloHome trH=(HelloHome) PortableRemoteObject.narrow(obj,HelloHome.class);
   //
得到EJBObject
    DigestSession digestSession = digestSessionHome.create();

     Hello tr=trH.create();
     System.out.println(tr.hello());

    byte[] ret = digestSession.md5(temp.getBytes());//ejb
方法调用

  注意:Context.SECURITY_PRINCIPALContext.SECURITY_CREDENTIALS是可选的,涉及到对ejb的操作的权限。

2、SERVLET中调用EJB
前提:被调用的EJB实现了REMOTE接口
Servlet中,调用的代码应该是这个样子:


    try {
      Context context = new InitialContext();
      Object ref = context.lookup("UserFacade");
      //look up jndi name and cast to Home interface
      UserFacadeHome userFacadeHome = (UserFacadeHome) PortableRemoteObject.
          narrow(ref, UserFacadeHome.class);
      UserFacade userFacade = userFacadeHome.create();
      userFacade.updateUser("002","
老二");    }
    catch (Exception ex) {
      ex.printStackTrace();
    }


跟客户端程序中调用EJB的差别是在Context的生成上,servlet中直接用
Context context = new InitialContext();
而客户端程序中是用
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    properties.put(Context.PROVIDER_URL, "t3://localhost:7001");

    Context context= new InitialContext(properties);

3、      EJB中调用其他的EJB(同一EJB模块)
前提:
(1)
被调用者实现了LOCAL接口,调用者则实现了REMOTE接口
(2)
调用者和被调用者应该在同一EJB模块打包文件(jar)
(3)
调用者的部署描述(ejb-jar.xml)中有关于Local ref的描述,如下所示:


    <session>
      <display-name>UserFacade</display-name>
      <ejb-name>UserFacade</ejb-name>
      <home>ejbtest.test.UserFacadeHome</home>
      <remote>ejbtest.test.UserFacade</remote>
      <ejb-class>ejbtest.test.UserFacadeBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
      <ejb-local-ref>
        <ejb-ref-name>ejb/user</ejb-ref-name>
        <ejb-ref-type>Entity</ejb-ref-type>
        <local-home>ejbtest.test.UserHome</local-home>
        <local>ejbtest.test.User</local>
        <ejb-link>User</ejb-link>
      </ejb-local-ref>
    </session>

在调用者中,调用的程序代码应该是下面的样子:

package ejbtest.test;import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;

import javax.ejb.*;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.rmi.RemoteException;

public class UserFacadeBean
    implements SessionBean {
  SessionContext sessionContext;
  private UserHome userHome;
  private static Context context;

  public void ejbCreate() throws CreateException {
  }

  public void ejbRemove() {
  }

  public void ejbActivate() {
  }

  public void ejbPassivate() {
  }

  public void setSessionContext(SessionContext sessionContext) {
    System.out.println("@@@@@@@@@@@@@@@@ UserFacadeBean.setSessionContext()");
    this.sessionContext = sessionContext;
    try {
      findUserHome();
    }
    catch (Exception e) {
      throw new EJBException(e.getMessage());
    }
  }

  private void findUserHome() throws Exception {
    final String ENTITY_NAME = "java:comp/env/ejb/user";

    context = new InitialContext();

    if (userHome == null) {
      try {
        Object object = context.lookup(ENTITY_NAME);
        userHome = (UserHome) object;
      }
      catch (Exception e) {
        throw new EJBException(e.getMessage());
      }
    }
  }

  public void addUser(String id, String name) throws RemoteException {
    try {
      User user = userHome.create(id);
      user.setName(name);
    }
    catch (Exception ex) {
      throw new RemoteException(ex.getMessage());
    }
  }
}

4EJB中调用其他的EJB(不同的EJB模块)
前提:被调用者实现了REMOTE接口
最简单的方法是按客户端程序(或者SERVLET)中调用EJB的方法。

 


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有