Subscriber.
- By Gopalan Suresh Raj
- July 18, 2000
Enterprise Java
Guarantee Inter-Enterprise Message Delivery Using JMS
Gopalan Suresh Raj
Listing 7. Subscriber.
import javax.jms.*;
import fiorano.jms.rtl.FioranoInitialContext;
class Subscriber implements MessageListener {
Sender m_nasdaq;
String m_bourseName; // Name of the Bourse ("nasdaq" or "nyse")
String m_nasdaqSymbols[]= {"SUNW", "MSFT", "CPWR"};
double m_buyBelow = 10; // Buy if prices go below $10
double m_sellAbove = 90; // Sell if prices go above $90
int m_buyNumStocks = 1000; // No. of Stocks to buy
int m_sellNumStocks = 100; // No. of Stocks to sell
public Subscriber (String bourseName, String customerID) {
m_nasdaq = new Sender(customerID);
m_nasdaq.TalkToYourStockBroker ();
m_bourseName = bourseName;
}
public static void SubscribeToStockQuotes (
String customerID) {
try {
// 1. Create the InitialContext Object used for looking up
// JMS administered objects on the Fiorano/EMS
// located on the default host.
FioranoInitialContext initialCtx = null;
initialCtx = new FioranoInitialContext ();
initialCtx.bind ();
// 1.1 Lookup Connection Factory and Topic names
TopicConnectionFactory topicConnFactory =
(TopicConnectionFactory) initialCtx.lookup
("primaryTCF");
Topic nasdaqTopic = (Topic)initialCtx.lookup
("Nasdaq_Topic");
// 1.2 Dispose the InitialContext resources
initialCtx.dispose();
// 2. create and start a topic connection
System.out.println("Creating topic connection");
TopicConnection topicConnection =
topicConnFactory.createTopicConnection();
topicConnection.start ();
// 3. create topic session on the connection just created
System.out.println
("Creating topic session: not transacted, auto ack");
TopicSession topicSession =
topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
// 4. create subscriber
System.out.println("Creating topic, subscriber");
TopicSubscriber nasdaqTopicSubscriber =
topicSession.createSubscriber (nasdaqTopic);
// 5. install an asynchronous listener/callback on the subscriber
// object just created
System.out.println ("Ready to subscribe for messages :");
nasdaqTopicSubscriber.setMessageListener
(new Subscriber ("nasdaq", customerID));
}
catch (Exception e) {
e.printStackTrace ();
}
}
/**
* Message listener which receives messages asynchronously
* for the bound subscriber.
*/
public void onMessage (Message msg) {
String stockName;
double price = 0;
try {
// When a message is received, print it out to standard
// output & send a Message to Queue to Buy or Sell stock
MapMessage mapMsg = (MapMessage)msg;
if (m_bourseName.equals("nasdaq")) {
for (int i = 0; i < m_nasdaqSymbols.length; i++) {
price = mapMsg.getDouble(m_nasdaqSymbols[i]);
System.out.println ("Received : " + m_bourseName+":
Value of "+ m_nasdaqSymbols[i]+ " = $"+ price);
if(price < m_buyBelow) {
m_nasdaq.BuyStocks (m_nasdaqSymbols[i],
m_buyNumStocks, price);
}
if(price > m_sellAbove) {
m_nasdaq.SellStocks (m_nasdaqSymbols[i],
m_sellNumStocks, price);
}
}
}
System.out.println ("\n");
}
catch (JMSException e) {
e.printStackTrace ();
}
}
public static void main (String args[]) {
if (args.length <= 0) {
printUsageInfo ();
System.exit (1);
}
SubscribeToStockQuotes (args[0]);
}
private static void printUsageInfo () {
System.out.println ("Usage: Subscriber customerID");
System.out.println
("customerID: Name or Account Number of the customer");
}
}