BuyAgent.

Enterprise Java
Guarantee Inter-Enterprise Message Delivery Using JMS
Gopalan Suresh Raj
Listing 9. BuyAgent.


import javax.jms.*;
import java.io.*;
import java.net.*;
import fiorano.jms.rtl.*;

public class BuyAgent {
  public static void main (String args[]) {

  // 1. Create the initial context string to lookup the Queue
  //    connection factory.

  FioranoInitialContext initialCtx = null;
  try {
      initialCtx = new FioranoInitialContext ();
      initialCtx.bind ();


      // 1.1  Lookup Connection Factory and Queue names

      QueueConnectionFactory queueConnFactory =
       (QueueConnectionFactory) initialCtx.lookup ("primaryQCF");
      Queue queue = (Queue)initialCtx.lookup("Buy_Queue");


      // 1.2  Dispose the InitialContext resources

      initialCtx.dispose();


      // 2. Create and start a Queue connection

      System.out.println("Creating Queue connection");
      QueueConnection queueConnection =
                     queueConnFactory.createQueueConnection();
      queueConnection.start ();


      // 3. Create a Queue session on this connection

      System.out.println("Creating Queue session: not trans,
                                                  auto ack");
      QueueSession queueSession =
                                 queueConnection.createQueueSession
                                 (false, Session.AUTO_ACKNOWLEDGE);


      // 4. Create a Queue and a Sender for it

      System.out.println("Creating Queue Receiver");
      QueueReceiver receiver =
                               queueSession.createReceiver (queue);
      while (true) {


        // 5. Wait for a request

        TextMessage request = (TextMessage) receiver.receive ();
        System.out.println ("Execute Job : " + request.getText ());
      }
    }
    catch (JMSException e) {
      e.printStackTrace ();
    }
  } // main
}