📄 traderbean.java
字号:
package com.genuitec.traderx.ejb;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* XDoclet-based stateless session bean. The class must be declared
* <code>public</code>, according to the EJB specification.<br>
*
* To generate code:
* <br>
* <ul>
* <li> Add Standard EJB module to XDoclet project properties
* <li> Customize XDoclet configuration
* <li> Run XDoclet
* </ul>
* <br>
* Please see the included XDoclet Overview
* and the XDoclet Reference in the help system for details
*
* @ejb.bean name = "Trader"
* type = "Stateless"
* display-name = "Trader"
* description = "Trader EJB"
* view-type = "remote"
* jndi-name = "ejb/com/genuitec/trader/ejb/TraderHome"
* @ejb.env-entry name = "tradeLimit" value = "500" type = "java.lang.Integer"
* @weblogic.enable-call-by-reference True
* @jonas.bean ejb-name = "Trader"
* jndi-name = "ejb/com/genuitec/trader/ejb/TraderHome"
* @oc4j.bean ejb-name = "Trader"
* jndi-name = "ejb/com/genuitec/trader/ejb/TraderHome"
*/
public class TraderBean implements SessionBean {
/** The SessionContext */
private SessionContext context;
private int tradeLimit;
private void log(String s) {
System.out.println(s);
}
/**
* An <code>ejbCreate</code> method as required by the EJB specification. <br>
*
* The container calls the instance抯 <code>ejbCreate</code> method whose
* signature matches the signature of the <code>create</code> method invoked
* by the client. The input parameters sent from the client are passed to
* the <code>ejbCreate</code> method. Each session bean class must have at
* least one <code>ejbCreate</code> method. The number and signatures
* of a session bean抯 <code>create</code> methods are specific to each
* session bean class.
*
* @throws CreateException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*
* @ejb.create-method
*/
public void ejbCreate() throws CreateException {
log("ejbCreate called");
try {
InitialContext ic = new InitialContext();
Integer tl = (Integer) ic.lookup("java:comp/env/tradeLimit");
tradeLimit = tl.intValue();
} catch (NamingException ne) {
throw new CreateException("Failed to find environment value "+ne);
}
}
/**
* The <code>ejbActivate()</code> method as required by the EJB specification.<br>
*
* The activate method is called when the instance is activated from its
* passive" state. The instance should acquire any resource that it has
* released earlier in the <code>ejbPassivate()</code> method. <br>
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*/
public void ejbActivate() throws EJBException {
}
/**
* The <code>ejbPassivate()</code> method as required by the EJB specification.<br>
*
* The activate method is called when the instance is activated from
* its "passive" state. The instance should acquire any resource that
* it has released earlier in the <code>ejbActivate()</code> method. <br>
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*/
public void ejbPassivate() throws EJBException {
}
/**
* The <code>ejbRemove()</code> method as required by the EJB specification.<br>
*
* A container invokes this method before it ends the life of the
* session object. This happens as a result of a client's invoking
* a remove operation, or when a container decides to terminate the
* session object after a timeout. <br>
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*/
public void ejbRemove() throws EJBException {
}
/**
* Set the associated session context. The container calls this method
* after the instance creation. <br>
*
* The enterprise bean instance should store the reference to the context
* object in an instance variable. <br>
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*/
public void setSessionContext(SessionContext newContext)
throws EJBException {
context = newContext;
}
/**
* Buys shares of a stock for a named customer.
*
* @param customerName String Customer name
* @param stockSymbol String Stock symbol
* @param shares int Number of shares to buy
* @return TradeResult Trade Result
* if there is an error while buying the shares
*
* @ejb.interface-method
*/
public TradeResult buy(String stockSymbol, int shares) {
if (shares > tradeLimit) {
log("Attempt to buy "+shares+" is greater than limit of "+tradeLimit + ".");
shares = tradeLimit;
}
log("Buying "+shares+" shares of "+stockSymbol + ". Great move.");
return new TradeResult(shares, stockSymbol);
}
/**
* Sells shares of a stock for a named customer.
*
* @param customerName String Customer name
* @param stockSymbol String Stock symbol
* @param shares int Number of shares to buy
* @return TradeResult Trade Result
* if there is an error while selling the shares
*
* @ejb.interface-method
*/
public TradeResult sell(String stockSymbol, int shares) {
if (shares > tradeLimit) {
log("Attempt to sell "+shares+" is greater than limit of "+tradeLimit);
shares = tradeLimit;
}
log("Selling "+shares+" shares of "+stockSymbol);
return new TradeResult(shares, stockSymbol);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -