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

📄 calculatorbean.java

📁 精通Jboss——Ejb和Web Services开发精解的随书源代码
💻 JAVA
字号:
/**
 * 
 * CalculatorBean.java
 * 
 * Created on 2003-8-15 12:23:16
 * 
 */
package com.liuyang.jboss.sessionbean.calculator.ejb;

//import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
//import javax.ejb.SessionSynchronization;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

/**
 * @author liuyang
 * @ejb.bean	description="CalculatorBean"
 *	          	display-name="CalculatorBean"
 *  	        jndi-name="CalculatorHomeRemote"
 *      	    name="Calculator"
 *          	type="Stateful"
 *	    	    view-type="remote"
 *  			transaction-type = "Bean"
 *			
 * 
 * 
 */
public class CalculatorBean implements SessionBean 
	//, SessionSynchronization 
	{
	private SessionContext ctx;
	private int value = 0;
	private long startTime;
	private long endTime;
	private long useedTime;


	/**
	 * @ejb.create-method 
	 */
	public void ejbCreate () {
	}
	/**
	 * 
	 * @ejb.interface-method view-type = "remote"
	 */
	public void reset() {
	  this.value = 0;
	}
	/**
	 * 
	 * @ejb.interface-method view-type = "remote"
	 */
	public int getValue() {
	  return value;
	}
	/**
	 * @param v
	 * @ejb.interface-method view-type = "remote"
	 */
	public void setValue(int v) {
	  this.value = v;
	}

	/**
	 * @param value
	 * @ejb.interface-method view-type = "remote"
	 */
	public void add(int value) {
	  this.value += value;
	}
	/**
	 * @param v
	 * @ejb.interface-method view-type = "remote"
	 */
	public void subtract(int v) {
	  this.value -= v;
	}
	/**
	 * @param v
	 * @ejb.interface-method view-type = "remote"
	 */
	public void multiplyBy(int v) {
	  this.value *= v;
	}
	/**
	 * @param v
	 * @ejb.interface-method view-type = "remote"
	 * 
	 */
	public void divideBy(int v) {
		//this.value /= v;
		UserTransaction ut = ctx.getUserTransaction();
		try{
			ut.begin();
			this.value /= v;
			ut.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				ut.rollback();
			} catch (IllegalStateException e1) {
				e1.printStackTrace();
			} catch (SecurityException e1) {
				e1.printStackTrace();
			} catch (SystemException e1) {
				e1.printStackTrace();
			}
		}
		/*try{		
			this.value /= v;
		}catch(Exception e){
			e.printStackTrace();
			this.ctx.setRollbackOnly();
		}*/
	}

	public void afterBegin() {
	  startTime = System.currentTimeMillis();
	}

	public void beforeCompletion() {
	  endTime = System.currentTimeMillis();
	  useedTime = endTime - startTime;
	}

	public void afterCompletion(boolean b) {
	  String s = (b) ? "Success" : "Error";
	  System.out.print("Calculator: " + s + "(elapse=" + useedTime + "ms)");
	}


	public void setSessionContext(SessionContext sc) {
	  this.ctx = sc;
	}

	public void ejbActivate() {
	}
	public void ejbPassivate() {
	}
	public void ejbRemove() {
	}

}

⌨️ 快捷键说明

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