ExecuteTrade.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:3k
源码类别:
Java编程
开发平台:
Java
- 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();
- }
- }
- }
- }