accountbean.java

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

JAVA
223
字号
//定义本类在包examples.cluster.ejb.account 中
package examples.cluster.ejb.account;
//本接口用到的其他类
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.*;

import weblogic.common.*;
import weblogic.management.Admin;
/**
 * AccountBean是实体EJB,它演示了:
 * 	容器管理的JDBC持续性管理和事务管理;
 *      在这个文件中的代码并没有直接访问数据库;
 * 	用户定义的异常;
 */
 //这个类是实体Bean,必须实现接口 EntityBean
public class AccountBean implements EntityBean {
	//设置是否打印控制台
  final static boolean VERBOSE = true;
  final static int     SLEEP    = 3000; 
  // -----------------------------------------------------------------
  //声明实体上下文变量
  private EntityContext ctx;
  //服务器名
  private transient String        serverName;

  // -----------------------------------------------------------------
  // 容器管理的属性
  public            String        accountId; 
  public            double        balance;
     
  // -----------------------------------------------------------------
  // 实现EntityBean
  
  /**
   * 返回主键,标识这个EJB
   * 
   * @返回                  String 标识
   */
  private String id() {
    return "" + System.identityHashCode(this) + ", PK = " + 
      (String) ((ctx == null) ? "nullctx" 
                 : ((ctx.getPrimaryKey() == null ?
                   "null" : ctx.getPrimaryKey().toString())));
  }

  /**
   * 这是本类必须实现的方法,在本例中没有用到
   *
   */
  public void ejbActivate() {
    if (VERBOSE)
      System.out.println("AccountBean.ejbActivate (" + id() + ")");
  }

  /**
   * 这是本类必须实现的方法,在本例中没有用到
   *
   */
  public void ejbPassivate() {
    if (VERBOSE)
      System.out.println("AccountBean.ejbPassivate (" + id() + ")");
  }

  /**
   * 为EJBean设置实体EJB上下文
   * 
   * @参数 ctx               EntityContext 
   */
  public void setEntityContext(EntityContext ctx) {
    if (VERBOSE)
      System.out.println("AccountBean.setEntityContext (" + id() + ")");
    this.ctx = ctx;
    this.serverName = getServerName();
  }

  /**
   * 取消实体上下文设置
   * 
   */
  public void unsetEntityContext() {
    if (VERBOSE)
      System.out.println("AccountBean.unsetEntityContext (" + id() + ")");
    this.ctx = null;
  }

  /**
   * 这是本类必须实现的方法,在本例中没有用到
   *
   */
  public void ejbLoad() {
    if (VERBOSE)
      System.out.println("AccountBean.ejbLoad (" + id() +  ")");
  }

  /**
   * 这是本类必须实现的方法,在本例中没有用到
   *
   */
  public void ejbStore() {
    if (VERBOSE) {
      System.out.println("AccountBean.ejbStore (" + id() + "): failover test point");
      try {
        Thread.sleep(SLEEP);
      } catch (InterruptedException ie) {
      // do nothing
      }
    }
  }

  /**
   * 这是本类必须实现的方法,在本例中没有用到
   *
   */
  public void ejbRemove() throws RemoveException {
    if (VERBOSE)
      System.out.println("AccountBean.ejbRemove (" + id() + ")");
  }
/**
   * 这个方法和"AccountBean.java"中定义的的Bean的"ejbCreate"方法相对应
   * 这两个方法的参数应该相同。当客户端调用"TraderHome.create()"方法时,EJB容器
   * 会找到EJBean的实例,并调用它的"ejbCreate()"方法。
   * 对容器管理的ejb,ejbCreate方法返回为null,而bean管理的ejb,返回的是主键类。
   * @参数 accountID         String 账号ID
   * @参数 initialBalance    double 初始化结算值
   * @异常               javax.ejb.CreateException
   *                          创建bean错误时抛出的异常

   */
  public AccountPK ejbCreate(String accountId, double initialBalance)
    throws CreateException
  {
    if (VERBOSE) 
      System.out.println("AccountBean.ejbCreate( id = " + System.identityHashCode(this) + ", PK = " +
                         accountId + ", " + "initial balance = $ '" + initialBalance + ")");

    this.accountId  = accountId;
    this.balance    = initialBalance;
    return null;
  }

  /**
   * 这是本类必须实现的方法,在本例中没有用到   
   */
  public void ejbPostCreate(String accountId, double initialBalance) {
    if (VERBOSE)
      System.out.println("AccountBean.ejbPostCreate (" + id() + ")");
  }

  // 应用程序定义的方法

  /**
   * 返回当前的结算
   *
   * @返回                  double 结算
   */
  public AccountResult balance() {
    if (VERBOSE) 
      System.out.println("AccountBean.balance (" + id() + ")");
    return new AccountResult(serverName, balance);
  }

  /**
   * 存入一定金额
   *
   * @参数 amount            double 数量
   * @返回                  double 结算
   */
  public AccountResult deposit(double amount) {
    if (VERBOSE) 
      System.out.println("AccountBean.deposit ($" + amount + 
                         " into '" + accountId + "', " + id() + ")");
    balance += amount;
    return new AccountResult(serverName, balance);
  }

  /**
   * 提取一定数量的金额
   *
   * @参数 amount            double 数量
   * @返回                  double 结算
   * @exception               ProcessingErrorException
   *                          如果提取的数量大于结算值
   */
  public AccountResult withdraw(double amount)
       throws AccountException
  {
    if (VERBOSE) 
      System.out.println("AccountBean.withdraw ($" + amount + " from '" + accountId + 
                         "', " + id() + ")");
    if (amount > balance) {
      throw new AccountException("Request to withdraw $" + amount +
                                 " more than balance " + balance +
                                 " in account " + accountId);
    }
    balance -= amount;
    return new AccountResult(serverName, balance);
  }


  /**
   * 获取服务器名weblogic.system.name
   *
   * @返回                  String 服务器名
   */
  private String getServerName() {
    String toReturn = null;
    try {
      toReturn = weblogic.management.Admin.getServerName();
      if (toReturn == null) {
        return "";
      } else {
        return toReturn;
      }
    }
    catch (Exception e) {
      return "";
    }
  }

}

⌨️ 快捷键说明

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