Sender.
- By Gopalan Suresh Raj
- July 18, 2000
Enterprise Java
Guarantee Inter-Enterprise Message Delivery Using JMS
Gopalan Suresh Raj
Listing 8. Sender.
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();
}
}
}