122 WCTME: Application Development and Case Study
private String targetQName = "SYSTEM.DEFAULT.LOCAL.QUEUE";
private String targetSFQ = "SFQ";
private boolean createdQM;
private boolean timeToQuit = false;
/**
* The public constructor
* It stores the bundle context,sets up an MQe client to send messages to
the server,
* and then sets up an MQe server to receive responses from the server
* @param context
*/
public ITSORentalsMQeImpl(BundleContext context) {
super();
this.context = context;
}
/**
* @see
com.itso.rentals.mqe.ITSORentalsMQeService#init(java.util.Properties)
*/
public boolean init(Properties p) {
//deal with properties
if (p != null) {
String standAloneString =
p.getProperty("itso.mqe.standalone", "true");
standAlone = standAloneString.equalsIgnoreCase("true");
targetQMIPAddress =
p.getProperty("itso.mqe.QMIPAddress", "127.0.0.1");
targetQMPort = p.getProperty("itso.mqe.QMPort", "8086");
targetQMName = p.getProperty("itso.mqe.QMName", "Transponder");
myQMName = p.getProperty("itso.mqe.DeviceQMName", "ITSOQueues");
myRegistry = myQMName;
}
if (!standAlone) {
mqeAdmin = new MQeAdmin(myQMName, myRegistry);
setupMQeClient();
setupMQeServer();
}
//Resolve Database service
ServiceReference dbRef =
context.getServiceReference(
ITSORentalsDatabaseService.class.getName());
if (dbRef != null) {
databaseService =
(ITSORentalsDatabaseService) context.getService(dbRef);
Chapter 5. Messaging 123
System.out.println("Found DataBase service!");
}
return true;
}
/**
* sends a message when a customer arrives
* @param String - contract number of the customer who has arrived
* @return boolean - always returns true
*/
public boolean customerArrived(String contractNumber) {
if (standAlone) {
databaseService.updateVechicleLocation(contractNumber, "A" +
count++);
} else {
sendMessage(contractNumber);
}
return true;
}
/**
* Checks to see if a QM is already running. If not it uses the MQeAdmin
* to define a new QM, start it then create a connection definition
* to the receivers QM. It then adds a remote queue definition to the
* receivers local queue.
*/
public void setupMQeClient() {
if (MQeQueueManager.getDefaultQueueManager() == null) {
mqeAdmin.defineQM();
mqeAdmin.startQM();
mqeAdmin.setRemoteQueueManagerName(targetQMName);
mqeAdmin.createConnection(targetQMIPAddress, targetQMPort);
mqeAdmin.addRemoteQueueDefinition(targetQMName, targetQName);
} else {
mqeAdmin.startQM();
}
}
/**
* Sets up the MQe Server. Calls MQeAdmin to create and start a
listener, then
124 WCTME: Application Development and Case Study
* adds a message itself as a message listener
*/
public void setupMQeServer() {
mqeAdmin.createHomeServerQueue(targetQMName, targetSFQ);
Thread kicker = new Thread(this);
kicker.start();
MQeQueueManager qm = MQeQueueManager.getDefaultQueueManager();
try {
qm.addMessageListener(this, MQe.System_Default_Queue_Name, null);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This method is called by the queue manager when a message arrives
* The method extracts the contract id and slot number from the message,
and updates
* the database with the information
*/
public void messageArrived(MQeMessageEvent arg0) throws Exception {
MQeQueueManager qm = MQeQueueManager.getDefaultQueueManager();
//until we run out of messages
for (;;) {
try {
MQeMsgObject msg = qm.getMessage(null,
MQe.System_Default_Queue_Name, null, null, 0);
String contractID = msg.getAscii("ContractID");
String slot = msg.getAscii("Slot");
System.out.println( "In messageArrived. Updating " + contractID +
" to slot " + slot);
databaseService.updateVechicleLocation(contractID, slot);
} catch (MQeMessageStoreException mqemse) {
System.out.println("Exception in messageArrived. [" +
mqemse.getMessage() + "]");
mqemse.printStackTrace();
} catch (Exception e) {
if (!e.getMessage().equalsIgnoreCase("Message not found")) {
System.out.println("Exception in messageArrived. [" +
e.getMessage() + "]");
e.printStackTrace();
}
break;
}
Chapter 5. Messaging 125
}
}
/**
* Creates a message to send to the server. It includes the
* "return address" information, including our queue manager
* and queue names, plus the text of the message
*
* @param text - the text to be sent in the message
* @return true if the message is sent without an exception
*/
public boolean sendMessage(String text) {
if (mqeAdmin != null) {
try {
String qmName =
MQeQueueManager.getDefaultQueueManager().getName();
String qName = MQe.System_Default_Queue_Name;
MQeQueueManager qm = null;
MQeMsgObject outMessage = new MQeMsgObject();
outMessage.putAscii("ContractID", text);
outMessage.putAscii(MQe.Msg_ReplyToQ, qName);
outMessage.putAscii(MQe.Msg_ReplyToQMgr, qmName);
qm = MQeQueueManager.getDefaultQueueManager();
qm.putMessage(
targetQMName,
MQe.System_Default_Queue_Name,
outMessage,
null,
0);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} else {
return false;
}
}
public void shutdown() {
if (!standAlone) {
timeToQuit = true;
mqeAdmin.stopQM();
}
126 WCTME: Application Development and Case Study
}
/**
* The run method sleeps for 15 seconds and then tells the queue
* manager to check for messages
*/
public void run() {
while (!timeToQuit) {
try {
MQeQueueManager.getDefaultQueueManager().triggerTransmission();
Thread.sleep(15000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
20.Organize Imports and Save the file.
21.Open the ExtensionServicesBundleActivator.java file (in the default package)
and add the following code to the class:
private ServiceRegistration svcdb;
private BundleContext context;
private ITSORentalsMQeImpl mqeSvc;
22.Add the following code in Example 5-2 at the end of the start() method:
Example 5-2 Appending the start() method
this.context = context;
mqeSvc = new ITSORentalsMQeImpl(context);
svcdb = context.registerService(
ITSORentalsMQeService.class.getName(),
mqeSvc,
null);
This code registers the messaging service, so that other bundles can resolve
it.
23.Add the following code at the end of the stop() method:
mqeSvc.shutdown();
svcdb.unregister();
Note: There will be some unresolved external references until we build the
MQAdmin class.

Get IBM Workplace Client Technology Micro Edition Version 5.7.1: Application Development and Case Study now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.