PriceServerImpl.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:4k
源码类别:
Java编程
开发平台:
Java
- package bible.rmi.example4;
- import java.util.*;
- import javax.naming.*;
- import weblogic.rmi.*;
- // Implement the PriceServer remote interface.
- /**
- * Class PriceServerImpl
- *
- *
- * @author
- * @version %I%, %G%
- */
- public class PriceServerImpl implements PriceServer {
- // Stock price database, implemented as a Hashtable.
- /** stockTable */
- private Hashtable stockTable = null;
- // Default constructor - create a stock price database.
- /**
- * Constructor PriceServerImpl
- *
- *
- */
- public PriceServerImpl() {
- stockTable = new Hashtable();
- Float f = new Float(0);
- stockTable.put("SUNW", f.valueOf("20"));
- stockTable.put("INTC", f.valueOf("30"));
- stockTable.put("BEAS", f.valueOf("40"));
- }
- // Return the price of the given security.
- /**
- * Method getPrice
- *
- *
- * @param symbol
- *
- * @return
- *
- */
- public float getPrice(String symbol) throws java.rmi.RemoteException {
- Float f = (Float) stockTable.get(symbol);
- System.out.println("PriceServer: Call to getPrice for symbol : "
- + symbol);
- return f.floatValue();
- }
- // Set the price of the given security.
- /**
- * Method setPrice
- *
- *
- * @param symbol
- * @param price
- *
- */
- public void setPrice(String symbol, float price) throws java.rmi.RemoteException{
- Float f = new Float(price);
- stockTable.put(symbol, f);
- System.out.println("PriceServer: Call to setPrice for " + symbol
- + " to " + price);
- }
- // Return all ticker symbols in this server's database.
- /**
- * Method getSecurities
- *
- *
- * @return
- *
- */
- public String[] getSecurities() throws java.rmi.RemoteException {
- if (stockTable.size() > 0) {
- String[] securities = new String [stockTable.size()];
- Enumeration enum = stockTable.keys();
- int i = 0;
- while (enum.hasMoreElements()) {
- securities [i] = (String) enum.nextElement();
- i++;
- }
- return securities;
- }
- return null;
- }
- // Submit an order on a client's behalf, then notify the submitting
- // client when the order is executed.
- /**
- * Method enterMarketOrder
- *
- *
- * @param order
- * @param client
- *
- */
- public void enterMarketOrder(Order order, ExecutionAlert client) throws java.rmi.RemoteException{
- // The ExecuteTrade bean will return immediately and call back
- // the client when the trade has been executed.
- try {
- // Create a context with which to lookup the bean
- Hashtable ht = new Hashtable();
- ht.put(Context.INITIAL_CONTEXT_FACTORY, Environment.JNDI_FACTORY);
- ht.put(Context.PROVIDER_URL, Environment.WEBLOGIC_URL);
- Context ctx2 = new InitialContext(ht);
- // Get a reference to the bean object
- Object objref = ctx2.lookup("ExecuteTrade");
- // Get a reference to the bean's home interface
- ExecuteTradeHome home =
- (ExecuteTradeHome) javax.rmi.PortableRemoteObject.narrow(objref,
- ExecuteTradeHome.class);
- // Get a reference to the bean's remote interface
- ExecuteTradeRemote executionBean = home.create();
- // Invoke the bean's remote method to execute the trade
- executionBean.executeTrade(order, client);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Method main
- *
- *
- * @param args
- *
- */
- public static void main(String[] args) {
- try {
- PriceServerImpl server = new PriceServerImpl();
- Context ctx = new InitialContext();
- ctx.bind(PRICESERVERNAME, server);
- System.out.println("PriceServer was started and bound in the registry "
- + "to the name " + PRICESERVERNAME);
- } catch (Exception e) {
- e.printStackTrace();
- System.exit(1);
- }
- }
- }