![]() |
Point-to-Point Messaging (Queueing) - The Sender |
![]() |
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-brokers 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
Point-to-Point Messaging (Queueing)
As mentioned earlier, in Point-to-Point messaging, even though there may be multiple Senders of messages, there is only a single Receiver for the messages. The central concept in this type of messaging is the Queue, which is used to represent the Destination. Queues are created by an administrator and registered with a JNDI context. Clients can send messages to a Queue and receive messages from a Queue. Queues are expected to have multiple Senders sending messages but there is only a single Receiver for these messages.
A client uses JNDI to locate a QueueConnectionFactory. The QueueConnectionFactory class implements the ConnectionFactory interface and is used to create QueueConnection objects. The QueueConnection class implements the Connection interface, and is used to create QueueSessions. The QueueConnection class represents an active connection to the Queue.
The Queue class implements the Destination interface. A Client uses JNDI to lookup and locate a Queue. These Queues are used by Senders to send messages, and by a Receiver to receive messages. A Queue can have multiple Senders but only one Receiver.
A TemporaryQueue is a Queue created within a QueueSession and exists for the lifetime of the QueueSession. QueueSessions implement the Session interface and are used to create a QueueSender, QueueReceiver, QueueBrowser, or TemporaryQueues.
The QueueSender class implements the MessageProducer interface and is used to send messages to a Queue. The QueueReceiver implements the MessageConsumer interface and is used to receive messages from the Queue. The QueueBrowser class implements the MessageConsumer interface. It is used to examine the current contents of the Queue without actually consuming any messages.
The Sender (Queue Sender)
The source code for our Queue Sender class (Sender.java) which sends buy or sell messages to the Buy_Queue or Sell_Queue is shown in Listing. Line 2 imports a non-standard Fiorano/EMS specific class (FioranoInitialContext). It is used to create the InitialContext object for our use. The rest of the code is pure JMS.
The steps that are required to send messages to a queue are as follows:
Listing : Sender.java
import javax.jms.*; import fiorano.jms.rtl.FioranoInitialContext; public class Sender { String m_customerID; // Account ID of this customer QueueSession m_queueSession; QueueSender m_buySender; QueueSender m_sellSender; public Sender (String customerID) { m_customerID = customerID; } public void finalize () { try { m_queueSession.close(); m_buySender.close(); m_sellSender.close(); } catch (Exception e) { e.printStackTrace(); } } public void TalkToYourStockBroker () { 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 Queue names // ============================================================== QueueConnectionFactory queueConnFactory = (QueueConnectionFactory) initialCtx.lookup ("primaryQCF"); Queue buyQueue = (Queue)initialCtx.lookup("Buy_Queue"); Queue sellQueue = (Queue)initialCtx.lookup("Sell_Queue"); // ============================================================== // 1.2 Dispose the InitialContext resources // ============================================================== initialCtx.dispose(); // ============================================================== // 2. create and start a queue connection // ============================================================== System.out.println("Creating Queue connections"); QueueConnection queueConnection = queueConnFactory.createQueueConnection(); queueConnection.start (); // ============================================================== // 3. create queue session on the connection just created // ============================================================== System.out.println ("Creating queue session: not transacted, auto ack"); m_queueSession = queueConnection.createQueueSession (false, Session.AUTO_ACKNOWLEDGE); // ============================================================== // 4. create senders for the Queue // ============================================================== System.out.println("Creating Queue, senders"); m_buySender = m_queueSession.createSender(buyQueue); m_sellSender = m_queueSession.createSender(sellQueue); } catch (Exception e) { e.printStackTrace (); } } void SellStocks (String symbol, int sellNumStocks, double price) { try { // ============================================================== // 5. Create a text message // ============================================================== TextMessage textmsg = m_queueSession.createTextMessage(); textmsg.setText ("Customer ID :"+ m_customerID + ". Sell "+ sellNumStocks + " stocks of " + symbol+ " at $"+ price); // ============================================================== // Set and Publish the message // ============================================================== m_sellSender.send(textmsg, DeliveryMode.PERSISTENT, 5, 0); } catch (Exception e) { e.printStackTrace(); } } public void BuyStocks(String symbol, int buyNumStocks, double price) { try { // ============================================================== // 5. Create a text message // ============================================================== TextMessage textmsg = m_queueSession.createTextMessage(); textmsg.setText ("Customer ID :"+ m_customerID + ". Buy "+ buyNumStocks + " stocks of " +symbol+ " at $"+ price); // ============================================================== // Set and Publish the message // ============================================================== m_buySender.send(textmsg, DeliveryMode.PERSISTENT, 5, 0); } catch (Exception e) { e.printStackTrace(); } } }
Running the Subscriber and Queue Sender
The partial screen dumps are shown below:
Compile Subscriber.java and Sender.java and run the Subscriber with the customerID as a parameter in the command line.
MS-DOS Command Prompt |
E:\com\gopalan\StockMarket\Subscriber> E:\com\gopalan\StockMarket\Subscriber>javac Subscriber.java E:\com\gopalan\StockMarket\Subscriber>javac Sender.java E:\com\gopalan\StockMarket\Subscriber>java Subscriber Athul Creating topic connection Creating topic session: not transacted, auto ack Creating topic, subscriber Ready to subscribe for messages : Creating Queue connections Creating queue session: not transacted, auto ack Creating Queue, senders Received : nasdaq: Value of SUNW = $91.0 Received : nasdaq: Value of MSFT = $90.0 Received : nasdaq: Value of CPWR = $89.0 Received : nasdaq: Value of SUNW = $88.0 Received : nasdaq: Value of MSFT = $87.0 Received : nasdaq: Value of CPWR = $86.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 Publish
Subscribe Messaging - The Subscriber Page...
click here to go to
The Point to
Point Messaging (Queueing) - The Receiver Page...
click here to go to
My Java Message Service (JMS) HomePage...
This site was developed and is maintained by Gopalan Suresh Raj This page has been visited |
Last Updated : Sept 24, '99 |
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. |