Weblogic中EJB调用方法总结

类别:Java 点击:0 评论:0 推荐:

1、客户端程序中调用EJB
前提:EJB要实现了REMOTE接口
客户端调用的代码可以用EJB Test Client工具生成。自己写就是这个样子:
    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);
    Object ref = context.lookup("DigestSessionBean"); //通过ejb的JNDI name查找到EJBHome对象

    DigestSessionHome digestSessionHome = (DigestSessionHome) PortableRemoteObject.narrow(ref,
        DigestSessionHome.class);//得到EJBHome
    DigestSession digestSession = digestSessionHome.create();//得到EJBObject

    byte[] ret = digestSession.md5(temp.getBytes());//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());
    }
  }
}

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

 

本文地址:http://com.8s8s.com/it/it13817.htm