Weblgoic調用EJB的幾種方法

來源:互聯網
上載者:User

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的方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.