client.java

来自「weblogic应用全实例」· Java 代码 · 共 196 行

JAVA
196
字号
//定义本接口在包examples.ejb20.basic.statefulSession中
package examples.ejb20.basic.statefulSession;
//本类用到的其他类
import java.rmi.RemoteException;
import java.util.Properties;

import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

/**
 * This class illustrates calling a stateful SessionBean and performing
 * the following exercises:
 * <ul>
 * <li> Create a Trader
 * <li> Buy some shares using the Trader
 * <li> Sell some shares using the Trader
 * <li> Remove the Trader
 * </ul>
 *
 * @author Copyright (c) 1998 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1998-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class Client {
//定义JNDI服务中EJB的名称
  private static final String JNDI_NAME = "ejb20-statefulSession-TraderHome";

  //声明应用服务器的URL变量
  private String url;
  //声明主接口变量
  private TraderHome home;


  public Client(String url)
    throws NamingException
  {

    this.url       = url;
     //寻找主接口
    home = lookupHome();
  }

  /**
   * 在命令行运行这个实例:
   * java examples.ejb.basic.statefulSession.Client "t3://localhost:7001"
   * 参数是可选的
   */
  public static void main(String[] args) {
    log("\nBeginning statefulSession.Client...\n");
//声明应用服务器地址变量
    String url       = "t3://localhost:7001";
         
    // Parse the argument list 
     if (args.length != 1) {
     	//打印用法提示
      System.out.println("Usage: java examples.ejb20.basic.statefulSession.Client t3://hostname:port");   
      return;
    } else {
      url = args[0];
    }
    
    
    Client client = null;

    try {
    	//创建客户端实例
      client = new Client(url);
    } catch (NamingException ne) {
      System.exit(1);
    }

    try {
    	//调用测试方法
      client.example();
    } catch (Exception e) {
    	//异常处理
      log("There was an exception while creating and using the Trader.");
      log("This indicates that there was a problem communicating with the server: "+e);
    } 

    log("\nEnd statefulSession.Client...\n");
  }

  /**
   * 运行实例
   */
  public void example()
    throws CreateException, ProcessingErrorException, RemoteException,
           RemoveException
  {
  //声明顾客名字变量
    String customerName = "Matt"; 

    // 创建远程对象
    log("创建 trader\n");
    Trader trader = (Trader) narrow(home.create(), Trader.class);

    // 出售一些股票
    //股票名
    String stockName   = "MSFT";
    //股票数量
    int numberOfShares = 200;
    log("Selling " + numberOfShares + " of " + stockName);
    //调用远程方法sell
    TradeResult tr = trader.sell(customerName, stockName, numberOfShares);
    log(tr.toString());

    // 买进一些股票
    //股票名
    stockName      = "BEAS";
    //股票数量
    numberOfShares = 250;
    log("Buying " + numberOfShares + " of " + stockName);
    //调用远程方法buy
    tr = trader.buy(customerName, stockName, numberOfShares);
    log(tr.toString());

    // Get change in Cash Account from EJBean
    log("Change in Cash Account: $" + trader.getBalance()
      + "\n");
    log("Removing trader\n");
    //清除远程对象
    trader.remove();

  }

  /**
   * 验证远程对象的类型
   */
  private Object narrow(Object ref, Class c) {
    return PortableRemoteObject.narrow(ref, c);
  }

  /**
   * 从JNDI树中寻找EJB主接口
   */
  private TraderHome lookupHome()
    throws NamingException
  {
    // 获取JNDI服务上下文
    Context ctx = getInitialContext();
   
    try {
    //获取名称对象	
      Object home = ctx.lookup(JNDI_NAME);
      //获取主接口实例 
      return (TraderHome) narrow(home, TraderHome.class);
    } catch (NamingException ne) {
    	//异常处理
      log("The client was unable to lookup the EJBHome.  Please make sure ");
      log("that you have deployed the ejb with the JNDI name "+JNDI_NAME+" on the WebLogic server at "+url);
      throw ne;
    }
  }

  /**
   * 使用属性对象获取JNDI服务上下文
   * clients
   */
  private Context getInitialContext() throws NamingException {
    
    try {
      // 声明属性对象
      Properties h = new Properties();
      h.put(Context.INITIAL_CONTEXT_FACTORY,
        "weblogic.jndi.WLInitialContextFactory");
        //返回的特定属性的上下文对象
      h.put(Context.PROVIDER_URL, url);
      return new InitialContext(h);
    } catch (NamingException ne) {
    	//异常处理
      log("We were unable to get a connection to the WebLogic server at "+url);
      log("Please make sure that the server is running.");
      throw ne;
    }
  }

   /**
   * 这是获取上下文的第二个版本,依赖一个jndi.properties文件
   *
   */
//    private static Context getInitialContext()
//      throws NamingException
//    {
//      return new InitialContext();
//    }
//打印控制台输出
  private static void log(String s) {
    System.out.println(s);
  }

}

⌨️ 快捷键说明

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