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

📄 client.java

📁 学习J2EE的好事例
💻 JAVA
字号:
//定义本类在包examples.ejb.basic.beanManaged 中
package examples.ejb.basic.beanManaged;
//本类用到的其他类
import java.rmi.RemoteException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
//本类用到的EJB类
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.RemoveException;
//本类用到的名称服务类
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

/**
 * 这个类演示了如何调用一个实体EJB,并进行如下操作:
 */

public class Client {
//声明变量
  private String url;
  
  private AccountHome home;
//构造方法
  public Client(String url)
    throws NamingException
  {

    this.url       = url;
  //查找主接口,lookupHome是本例自定义方法。   
    home = lookupHome();
  }

 /**
   * 在命令行运行这个实例:
   * java examples.ejb.basic.beanManaged.Client "t3://localhost:7001"
   * 参数是可选的
   * @参数 url               URL such as "t3://localhost:7001" of Server
   */
  public static void main(String[] args) throws NamingException {
    System.out.println("\nBeginning beanManaged.Client...\n");

    String url       = "t3://localhost:7001";
         
    // 解析命令行参数
     if (args.length != 1) {
      System.out.println("Usage: java examples.ejb.basic.beanManaged.Client t3://hostname:port");   
      return;
    } else {
      url = args[0];
    }

    
    Client client = null;

    try {
    	//实例化本类
      client = new Client(url);
    } catch (NamingException ne) {
    	//异常处理
      log("Unable to look up the beans home: " + ne.getMessage());
      System.exit(1);
    }

    try {
    	//运行实例
      client.example();

	} catch (Exception e) {
	//异常处理
      log("There was an exception while creating and using the Accounts.");
      log("This indicates that there was a problem communicating with the server: "+e);
    } 

    System.out.println("\nEnd beanManaged.Client...\n");
  }
     /**
    * 执行实例
    *
    */ 
  public void example() 
    throws CreateException, RemoteException, FinderException,
           RemoveException
  {

    int numBeans = 20;
	//声明并创建账号数组
    Account [] accounts = new Account [numBeans];

    // 创建20个账号
    for (int i=0; i<numBeans; i++) {
      accounts [i] = findOrCreateAccount("ID: "+i, i * 1000);
    }

    // 打印账号结算
    for (int i=0; i<numBeans; i++) {
      log("Account: :"+accounts[i].getPrimaryKey()+
        " has a balance of "+accounts[i].balance());
    }

    // 查找所有结算大于5000的账号
    findBigAccounts(5000.0);

    // 清除所有账号

    log("Removing beans...");
    for (int i=0; i<numBeans; i++) {
      accounts[i].remove();
    }
  }

  
   /**
    * 列出所有结算大于给定值的账号
    * 这个finder方法演示返回枚举账号
    */
  private void findBigAccounts(double balanceGreaterThan)
    throws RemoteException, FinderException
  {
    log("\nQuerying for accounts with a balance greater than " +
      balanceGreaterThan + "...");
//调用主接口创建账号方法findBigAccounts,返回账号枚举
    Enumeration enum = home.findBigAccounts(balanceGreaterThan);

    if(! enum.hasMoreElements()) {
      log("No accounts were found!");
    }

    while (enum.hasMoreElements()) {
    	 //创建远程对象
      Account bigAccount= (Account) PortableRemoteObject.narrow(enum.nextElement(), 
                                                                Account.class);

      log("Account " + bigAccount.getPrimaryKey() + 
        "; balance is $" + bigAccount.balance());
    }
  }

  /**
   * 如果对应id的账号以存在,则返回这个id,否则创建它
   */
  private Account findOrCreateAccount(String id, double balance)
    throws CreateException, RemoteException, FinderException
  {
    try {
      log("Trying to find account with id: "+id);
      return (Account) PortableRemoteObject.narrow(home.findByPrimaryKey(id),
                                                         Account.class);
    } catch (ObjectNotFoundException onfe) {
     // 账号不存在,创建它
      return (Account) PortableRemoteObject.narrow(home.create(id, balance),
                                                         Account.class);
    } 
  }


  /**
   * 给定id和结算创建一个新的账号
   */
  private Account createAccount(String id, double balance)
    throws CreateException, RemoteException
  {
    log("Creating account " + id + " with a balance of " +
      balance + "...");
       //创建远程账号对象
    Account ac = (Account) PortableRemoteObject.narrow(home.create(id, balance),
                                                       Account.class);
    log("Account " + id + " successfully created");

    return ac;
  }

 /**
   * 使用JNDI查找bean的主接口
   */
  private AccountHome lookupHome()
    throws NamingException
  {
    Context ctx = getInitialContext();
   
    try {
//查找主接口
      Object home = ctx.lookup("beanManaged.AccountHome");
      return (AccountHome) PortableRemoteObject.narrow(home, AccountHome.class);

	} catch (NamingException ne) {
	//异常处理
      log("The client was unable to lookup the EJBHome.  Please make sure " +
      "that you have deployed the ejb with the JNDI name " + 
      "beanManaged.AccountHome on the WebLogic server at "+url);

      throw ne;
    }
  }

   /**
   * 获取初始化上下文
   */
  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;
    }
  }
//控制台输出
  private static void log(String s) {
    System.out.println(s);
  }
}

⌨️ 快捷键说明

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