priceserverimpl.java
来自「100多M的J2EE培训内容」· Java 代码 · 共 174 行
JAVA
174 行
package bible.rmi.example3;
import java.util.*;
import javax.naming.*;
import weblogic.rmi.*;
/**
* PriceServerImpl implements the PriceServer remote interface, providing
* prices on securities for remote clients.
*/
public class PriceServerImpl implements PriceServer {
// Stock price database, implemented as a Hashtable.
/** stockTable */
private Hashtable stockTable = null;
// Hostname of the box where the server is running.
/** hostname */
private String hostname = null;
/**
* Creates the stock price 'database'.
*/
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"));
}
/**
* Returns the price of the given security.
* @param symbol
*/
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();
}
/**
* Sets the price for the given security.
* @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);
}
/**
* Returns all ticker symbols in this server's database.
*/
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;
}
/**
* Submits an order for the client, then asynchronously
* notifies that client when the order gets executed.
* @param order Order to be submitted by the client.
* @param clientRef Remote reference for the client, used for asynchronous execution notification.
*/
public void enterMarketOrder(Order order, ExecutionAlert clientRef) throws java.rmi.RemoteException{
// Multi-threaded this so it returns to the client right away and
// does its execution/callback work later.
System.out.println("A client has placed an order!");
System.out.println("Time: " + new Date());
System.out.println(order + "\n");
DoExecution exec = new DoExecution(order, clientRef);
new Thread(exec).start();
}
/**
* Inner-class providing asynchronous client callbacks of order activity.
*/
private class DoExecution implements Runnable {
/** order */
private Order order;
/** client */
private ExecutionAlert client;
/**
* @param ord
* @param clientRef
*/
public DoExecution(Order ord, ExecutionAlert clientRef) {
order = ord;
client = clientRef;
}
/**
* run
*/
public void run() {
try {
// Wait 10 seconds to simulate submission of the order
// to a stock exchange.
Thread.currentThread().sleep(10000);
// Create an Execution instance and execute the order.
Execution execution = new Execution();
// Copy order data into the execution.
execution.setNumber(order.getNumber());
execution.setSymbol(order.getSymbol());
execution.setShares(order.getShares());
// Execute at the stock's current price and write a status
// message to standard output.
execution.setPrice(getPrice(order.getSymbol()));
System.out.println("An order has been executed!");
System.out.println("Time: " + new Date());
System.out.println(execution + "\n");
// Make a remote call to the client to notify them
// of the execution.
client.notifyOfExecution(execution);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Instanciates and binds a PriceServer.
* @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);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?