Stock[] stocks = new Stock[] {
new Stock("GOOG", 435.43),
new Stock("YHOO", 27.88),
new Stock("ASF", 1015.55), };
for (Stock s : stocks) {
Channel ch = b.getChannel("/stock/"+s.getSymbol(), true);
ch.subscribe(c);
}
Random r = new Random(System.currentTimeMillis());
while (run) {
for (int j = 0; j < 1; j++) {
int i = r.nextInt() % 3;
if (i < 0)
i = i * (-1);
Stock stock = stocks[i];
double change = r.nextDouble();
boolean plus = r.nextBoolean();
if (plus) {
stock.setValue(stock.getValue() + change);
} else {
stock.setValue(stock.getValue() - change);
}
Channel ch = b.getChannel("/stock/"+stock.getSymbol(), true);
Message m = b.newMessage(c);
m.put("stock", stock.toString());
m.put("symbol", stock.getSymbol());
m.put("price", stock.getValueAsString());
m.put("change", stock.getLastChangeAsString());
ch.publish(m);
System.out.println("Bayeux Stock: "+stock.getSymbol()+" Price: "+stock.getValueAsString()+" Change: "+stock.getLastChangeAsString());
}
Thread.sleep(850);
}
} catch (InterruptedException ix) {