StockServer.

Enterprise Java
Guarantee Inter-Enterprise Message Delivery Using JMS
Gopalan Suresh Raj
Listing 6. StockServer.


import javax.jms.*;
import java.io.*;

import fiorano.jms.rtl.FioranoInitialContext;

class StockServer implements Runnable {

  String m_bourseName;         // Name of the Bourse "NASDAQ"
  String m_companySymbols[];   // Ticker Symbols like "SUNW","MSFT"
  Topic  m_topic;              // The Topic of Subscription
  TopicConnectionFactory m_topicConnFactory;

  public StockServer        (String bourseName,
                            String[] companySymbols,
                            Topic topic,
                            TopicConnectionFactory topicConnFactory) {

    m_bourseName = bourseName;
    m_companySymbols = companySymbols;
    m_topic = topic;
    m_topicConnFactory = topicConnFactory;
  }


  public static void main (String args[]) {
    String nasdaqSymbols[]= {"SUNW", "MSFT", "CPWR"};
    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");
      Thread nasdaqServer =
                new Thread(new StockServer("NASDAQ",
                                            nasdaqSymbols,
                                            nasdaqTopic, ]
                                            topicConnFactory));
                                            nasdaqServer.start();


      // 1.2  Dispose the InitialContext resources

      initialCtx.dispose();
    }  catch (Exception exception) {
      exception.printStackTrace ();
    }
  }

  public void run() {

    try {

      // 2. Create and start a topic connection

      System.out.println("Creating topic connection");
      TopicConnection topicConn =
                     m_topicConnFactory.createTopicConnection();
      topicConn.start ();


      // 3. Create a topic session on this connection
      System.out.println
                     ("Creating topic session: not transacted, auto ack");
      TopicSession topicSession = topicConn.createTopicSession
                               (false, Session.AUTO_ACKNOWLEDGE);


      // 4. Create a publisher for this topic
      System.out.println("Creating a topic publisher");
      TopicPublisher topicPublisher =
                            topicSession.createPublisher (m_topic);


      // 5. Create a Map Message for use in the while loop

      MapMessage mapMsg = topicSession.createMapMessage();


      // 6. Publish the Stock Quotes it in a loop

      double stockPrice = 100.00;
      boolean loop = true;
      while (loop == true) {

        // Set and Publish the message
        mapMsg.setDouble(m_companySymbols[0], stockPrice-);
        mapMsg.setDouble(m_companySymbols[1], stockPrice-);
        mapMsg.setDouble(m_companySymbols[2], stockPrice-);
        topicPublisher.publish(mapMsg);

        System.out.println (m_bourseName+": Value of "+
                                      m_companySymbols[0]+
                                      "= $"+ (stockPrice+3));
        System.out.println (m_bourseName+": Value of "+
                                      m_companySymbols[1]+
                                      "= $"+ (stockPrice+2));
        System.out.println (m_bourseName+": Value of "+
                                      m_companySymbols[2]+
                                      "= $"+ (stockPrice+1) + "\n");

        Thread.currentThread().sleep (1000);
        // Reset Stock Price when it goes below $5.00 if (stockPrice < 5 )
           stockPrice = 100.00;
      }
      System.out.println ("Closing topic session and topic connection");
      topicSession.close();
      topicConn.close();
    } catch (Exception exception) {
      exception.printStackTrace ();
    }
  }
}