requestresearch.java

来自「100多M的J2EE培训内容」· Java 代码 · 共 209 行

JAVA
209
字号
package bible.ejb.session.example1;import java.rmi.*;import javax.ejb.*;import java.util.*;import javax.naming.*;/** * Class RequestResearch * * * @author * @version %I%, %G% */public class RequestResearch implements SessionBean {  // Implemented create method required by RequestResearchHome.  /**   * Method ejbCreate   *   *   */  public void ejbCreate() {}  // Implemented methods required by SessionBean.  private SessionContext sessionContext;  /**   * Method ejbRemove   *   *   * @throws RemoteException   *   */  public void ejbRemove() throws RemoteException {}  /**   * Method ejbActivate   *   *   * @throws RemoteException   *   */  public void ejbActivate() throws RemoteException {}  /**   * Method ejbPassivate   *   *   * @throws RemoteException   *   */  public void ejbPassivate() throws RemoteException {}  /**   * Method setSessionContext   *   *   * @param sessionContext   *   * @throws RemoteException   *   */  public void setSessionContext(SessionContext sessionContext)    throws RemoteException {    this.sessionContext = sessionContext;  }  // Methods for processing requests for research as required  // for the RequestResearchRemote interface.  /**   * Fetches and returns research for a security.   */  public String requestResearch(String symbol) throws RemoteException {    // CheckEnvironment flag here to see whether to route the call to the    // internal system.    String research = null;    if (this.useRequestDataManager()) {      System.out.println("Locally calling RequestDataManager for research.");      research = getResearchFromDataManager(symbol);    } else {      System.out.println("Randomly generating research for this symbol.");      research = createRandomResearch(symbol);    }    return research;  }  /**   * Randomly creates 'research' for this security.   */  private String createRandomResearch(String symbol) {    Random rand = new Random();    int    x    = rand.nextInt();    x = x % 3;    String research = null;    switch (x) {    case 0 :      research = "Zeetrade recommends you buy " + symbol;      break;    case 1 :      research = "Zeetrade recommends you hold " + symbol;      break;    default :      research = "Zeetrade recommends you sell " + symbol;      break;    }    return research;  }  /**   * Does an environment entry lookup to determine where or not to use   * the DataManager or randomly generate data here.  This field can be edited in   * the ejb-jar.xml before EJB deployment.   */  private boolean useRequestDataManager() {    System.out.println("Checking the env-entry for useRequestDataManager");    InitialContext ctx        = null;    boolean        useManager = false;    try {      // Because Request Research is a bean living in the EJB container,      // no need to specify server parameters to obtain the correct context.      ctx = new InitialContext();      Boolean useManagerBool =        (Boolean) ctx.lookup("java:comp/env/useRequestDataManager");      useManager = useManagerBool.booleanValue();    } catch (Exception e) {      e.printStackTrace();    } finally {      try {        ctx.close();      } catch (Exception e) {}    }    return useManager;  }  /**   * Calls local EJB RequestDataManager to get research.   */  private String getResearchFromDataManager(String symbol) {    InitialContext ctx = null;    StringBuffer   sb  = new StringBuffer();    try {      // Because Request Research is a bean living in the EJB container,      // no need to specify server parameters to obtain the correct context.      ctx = new InitialContext();      Object objref = ctx.lookup("RequestDataManager");      // No need to do narrow, simply cast as this is a local object to this JVM.      RequestDataManagerLocalHome home     =        (RequestDataManagerLocalHome) objref;      RequestDataManagerLocal     dataBean = home.create();      sb.append("**Data Retreived from the local datasource**");      // Call the dataBean, this is not pass by value as in RMI (Remote      // beans included) Instead the stringbuffer is passed by reference,      // and therefore when the local session EJB RequestDataManager      // appends to it, the appends are altering this buffer (sb).      dataBean.appendResearchData(sb, symbol);    } catch (Exception e) {      e.printStackTrace();    } finally {      try {        ctx.close();      } catch (Exception e) {}    }    return sb.toString();  }}/*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*//*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?