executetrade.java
来自「100多M的J2EE培训内容」· Java 代码 · 共 125 行
JAVA
125 行
package bible.rmi.example4;import java.rmi.*;import javax.ejb.*;import javax.naming.*;import java.util.*;/** * Implementation of the ExecuteTrade Session EJB. * Executes a trade and notifies via client callback of execution. */public class ExecuteTrade implements SessionBean { /** sessionContext */ private SessionContext sessionContext; /** * Method ejbCreate * * */ public void ejbCreate() {} /** * Method ejbRemove * * */ public void ejbRemove() {} /** * Method ejbActivate * * */ public void ejbActivate() {} /** * Method ejbPassivate * * */ public void ejbPassivate() {} /** * Method setSessionContext * * * @param context * */ public void setSessionContext(SessionContext context) { sessionContext = context; } /** * Executes an order and alerts the client of the execution. * @param order Order to be executed. * @param alert Client to be alerted upon execution. * @throws RemoteException */ public void executeTrade(Order order, ExecutionAlert alert) throws RemoteException { // Multi-threaded this so it returns to the client right away and // does its execution/callback work later. DoExecution exec = new DoExecution(order, alert); new Thread(exec).start(); } /** * DoExecution */ private class DoExecution implements Runnable { /** order */ private Order order; /** client */ private ExecutionAlert client; /** * Constructor DoExecution * * * @param ord * @param alert * */ public DoExecution(Order ord, ExecutionAlert alert) { order = ord; client = alert; } /** * Method run * * */ public void run() { try { Thread.currentThread().sleep(15000); // sleep 15 seconds. InitialContext ctx = Environment.getInitialContext(); PriceServer server = (PriceServer) ctx.lookup(PriceServer.PRICESERVERNAME); Execution execution = new Execution(); execution.setNumber(order.getNumber()); execution.setPrice(server.getPrice(order.getSymbol())); // execution.setPrice(order.getPrice()); execution.setShares(order.getShares()); execution.setSymbol(order.getSymbol()); System.out.println("An order has been executed!"); System.out.println("Time: " + new Date()); System.out.println(execution + "\n"); client.notifyOfExecution(execution); } catch (Exception e) { e.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?