Make your own free website on Tripod.com

 Professional JMS Programming Read about my latest book on The Java Message Service  ...

 Professional JMS Programming ...  Professional JMS ...

Point-to-Point Messaging (Queueing) 

- The Receiver

Gopalan Suresh Raj

 

Note
To work with any of these samples, you will need the following:
.........................................JDK 1.1.6 or higher (I use JDK 1.1.7B)
.........................................
Fiorano/EMS Lite - a free JMS Server Provider from Fiorano Software (http://www.fiorano.com/)

The Stock Trader Application

To illustrate both Publish-Subscribe and Point-to-Point messaging, let us build a Stock Trader Application. We have a server (represented by the StockServer class) which just keeps sending Stock Quotes to a topic which we call the NASDAQ_Topic. A Subscriber (represented by the Subscriber class) subscribes to this Topic and receives notification of the change in prices of the company stock prices. We also have the Subscriber connected to his stock-broker’s Buy and Sell Queues which are named Buy_Queue and Sell_Queue through a Sender class. The Sender class sends Buy and Sell messages to the appropriate queues as requested by the Subscriber class. The Stock Agents who are responsible for Buying Stocks (represented by the BuyAgent class) and Selling Stocks (represented by the SellAgent class) are subscribed to the Buy_Queue and the Sell_Queue respectively. The whole scenario is depicted in Figure. We use Publish-Subscribe messaging between the StockServer and the Subscriber. We use Point-to-Point or Queue based messaging between the Sender and the BuyAgent and SellAgent classes.

To implement the following example, I used a beta version of Fiorano Software, Inc.’s Fiorano/EMS Lite for Windows NT, JMS implementation. You can download this implementation from Fiorano Software, Inc. at http://www.fiorano.com/ and set it up according to instructions.

Figure : Our Stock Trader Implementation

 

The BuyAgent (Queue Receiver)

The Buy Agent (represented by the BuyAgent class) is used to receive buy messages from the Buy_Queue for buying stock. The source code for our buyer class (BuyAgent.java) which receives stock buy messages from the Buy_Queue shown in Listing.

The steps that are required to receive messages from a queue are as follows:

  1. Create the InitialContext Object used for looking up JMS administered objects on the Fiorano/EMS located on the default host. Create the FioranoInitialContext and bind to it. (lines 17-18)
    1. Lookup Connection Factory and Queue names. Lookup "primaryQCF" by name and obtain a reference to the QueueConnectionFactory. Lookup "Buy_Queue" by name and obtain a reference to the Queue. (lines 23-25)
    2. Dispose the InitialContext resources. (line 30)
  2. Create and start a queue connection (lines 36-38)
  3. Create a queue session on this connection (lines 44-45)
  4. Create a receiver for this queue (line 51)
  5. When a message is received, print it out to standard output (line 58-59)

Listing :BuyAgent.java

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
}

Running the Queue Receiver

The partial screen dumps are shown below:

1.Compile and run the BuyAgent

MS-DOS Command Prompt

E:\com\gopalan\StockMarket\StockAgent>javac BuyAgent.java

E:\com\gopalan\StockMarket\StockAgent>java BuyAgent
Creating Queue connection
Creating Queue session: not trans, auto ack
Creating Queue Receiver
Execute Job : Customer ID :Athul. Buy 1000 stocks of MSFT at $9.0
Execute Job : Customer ID :Athul. Buy 1000 stocks of CPWR at $8.0
Execute Job : Customer ID :Athul. Buy 1000 stocks of SUNW at $7.0
Execute Job : Customer ID :Athul. Buy 1000 stocks of MSFT at $6.0
Execute Job : Customer ID :Athul. Buy 1000 stocks of CPWR at $5.0

 

2. Compile and run the SellAgent

MS-DOS Command Prompt

E:\com\gopalan\StockMarket\StockAgent>javac SellAgent.java

E:\com\gopalan\StockMarket\StockAgent>java SellAgent
Creating Queue connection
Creating Queue session: not trans, auto ack
Creating Queue Receiver
Execute Job : Customer ID :Athul. Sell 100 stocks of SUNW at $100.0
Execute Job : Customer ID :Athul. Sell 100 stocks of MSFT at $99.0
Execute Job : Customer ID :Athul. Sell 100 stocks of CPWR at $98.0


Author Bibliography
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He is contributing author to a couple of books "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia site (http://www.execpc.com/~gopalan) or mail him at gopalan@execpc.com.

click here to go to
The Point to Point Messaging (Queueing) - The Sender Page...

click here to go to
My Java Message Service (JMS) HomePage...


Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since September 24,1999.

Last Updated : Sept 24, '99

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-99, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.