⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 weblogic应用全实例
💻 JAVA
字号:
//定义本类在包examples.ejb.basic.statelessSession中
package examples.ejb.basic.statelessSession;
//本类用到的其他类
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;

/**
 * 这个类演示了如何调用一个会话,并进行如下操作:
 * 创建一个Trader远程对象
 * 使用Trader远程对象购买一定数量的股票
 * 出售一定数量的股票
 * 清除Trader远程对象
 */

public class Client {
//定义JNDI服务中EJB的名称
  private static final String JNDI_NAME = "statelessSession.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) throws Exception {

    log("\nBeginning statelessSession.Client...\n");
//声明应用服务器地址变量
    String url       = "t3://localhost:7001";
    
    // Parse the argument list 
     if (args.length != 1) {
     	//打印用法提示
      System.out.println("Usage: java examples.ejb.basic.statelessSession.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 statelessSession.Client...\n");
  }

  /**
   * 运行实例
   */
  public void example()
    throws CreateException, RemoteException, RemoveException
  {
    
    // 创建远程对象
    log("Creating a trader");
    Trader trader = (Trader) narrow(home.create(), Trader.class);

    String [] stocks = {"BEAS", "MSFT", "AMZN", "HWP" };

      // 执行一系列购买操作
    for (int i=0; i<stocks.length; i++) {
      int shares = (i+1) * 100;
      log("Buying "+shares+" shares of "+stocks[i]+".");
      trader.buy(stocks[i], shares);
    }

    // 执行一系列出售操作
    for (int i=0; i<stocks.length; i++) {
      int shares = (i+1) * 100;
      log("Selling "+shares+" shares of "+stocks[i]+".");
      trader.sell(stocks[i], shares);
    }

    // 清除远程对象
    log("Removing the trader");
    trader.remove();

  }

  /**
   * 使用RMI/IIOP的narrow方法
   */
  private Object narrow(Object ref, Class c) {
    return PortableRemoteObject.narrow(ref, c);
  }

  /**
   * 在JNDI树中寻找EJB主接口
   */
  private TraderHome lookupHome()
    throws NamingException
  {
    // 使用JNDI寻找EJB
    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服务上下文
   */
  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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -