Chapter 5. Messaging 127
This code unregisters the service when the bundle stops.
24.Organize Imports and save the file.
25.From the Package Explorer tab of the SMF Perspective, select the
ITSORentalsDatabaseServiceBundle Project you created and expand it
down to the file META-INF/MANIFEST.MF. Double-click the file name
MANIFEST.MF.
26.This opens the MANIFEST.MF editor. Scroll down to Export Packages.
27.Expand the section Export Packages and select Add...
28.Select the package com.itso.rentals.mqe and click OK.
29.Scroll down to Export Services and select Add...
30.Select the service com.itso.rentals.mqe.ITSORentalsMQeService and
click OK.
31.Scroll down to Import Services.
32.Expand the section Import Services and select Add...
33.Select the service
com.itso.rentals.database.ITSORentalsDatabaseService and click OK.
34.Save the file.
5.3.3 Creating the MQeAdmin Class
The MQeAdmin class is required for both the client side and the MQe server side
implementation. The MQeAdmin class contains a number of utility methods to
access and manipulate queue managers and queues.
1. Create the MQeAdmin class in the same package as above. Insert the
following code in Example 5-3 into the class file:
Example 5-3 MQeAdmin class code
/**
* MQeAdmin class provides helper methods to create and configure
* an MQeQueueManager.
*
* The QM name and Reg location to be used are passed in via
* the constructor.
*
* This class configures QueueManagers to use the following adapters:
* MQeTcpipHttpAdapter is used for comms.
* MQeDiskFieldsAdapter is used for registry storage.
*
128 WCTME: Application Development and Case Study
*/
public class MQeAdmin {
private String adapterClass = "com.ibm.mqe.adapters.MQeTcpipHttpAdapter";
private long channelTimeout = 3600000;
private int maxNumberChannels = 10;
private MQeFields parms;
private String myQmName;
private String myReg;
private MQeQueueManager qm = null;
private MQeQueueManagerConfigure qmConfig = null;
private String queueStore;
private String DISK_ADAPTER = "com.ibm.mqe.adapters.MQeDiskFieldsAdapter";
private String remoteQueueManagerName;
private String queueName = "SYSTEM.DEFAULT.LOCAL.QUEUE";
private String receiverQMIPAddress;
/**
* Private reference to a disk adapter, to make sure
* that configuration information set up in one method is not
* garbage collected before it is used in other methods.
*/
private MQeDiskFieldsAdapter myMemory = null;
/**
* Constructor to create a new MQeAdmin.
* This sets the QueueManager name and Registry location
* to be used by the MQeQueuemanager.
*/
public MQeAdmin(String qmName, String regLocation) {
myQmName = qmName;
myReg = regLocation;
}
/**
* setRemoteQueueManagerName sets the queue manager name
*
* @param remoteQueueManagerName
*/
public void setRemoteQueueManagerName(String remoteQueueManagerName) {
this.remoteQueueManagerName = remoteQueueManagerName;
}
/**
Chapter 5. Messaging 129
* Defines the Queue Manager
*/
public void defineQM() {
try {
myMemory = new com.ibm.mqe.adapters.MQeDiskFieldsAdapter();
queueStore =
DISK_ADAPTER
+ ":"
+ myReg
+ File.separator
+ "queues"
+ File.separator;
parms = new MQeFields();
MQeFields queueManagerParameters = new MQeFields();
MQeFields registryParameters = new MQeFields();
// add qm name here
queueManagerParameters.putAscii(MQeQueueManager.Name, myQmName);
// add reg location here
registryParameters.putAscii(MQeRegistry.DirName, myReg);
registryParameters.putAscii(MQeRegistry.Adapter, DISK_ADAPTER);
parms.putFields(
MQeQueueManager.QueueManager,
queueManagerParameters);
parms.putFields(MQeQueueManager.Registry, registryParameters);
// use MQeConfig
qmConfig = new MQeQueueManagerConfigure(parms, queueStore);
// define default queues
qmConfig.defineQueueManager();
qmConfig.defineDefaultSystemQueue();
qmConfig.defineDefaultDeadLetterQueue();
qmConfig.defineDefaultAdminReplyQueue();
qmConfig.defineDefaultAdminQueue();
} catch (MQeException e) {
if (e.code() == MQeExceptionCodes.Except_QMgr_AlreadyExists) {
System.out.println("MQeAdmin: QMgr already exists");
} else {
System.out.println("Some MQeException in defining QM");
e.printStackTrace();
}
} catch (Exception e) {
System.out.println("MQeAdmin: Error defining QM " + e);
130 WCTME: Application Development and Case Study
e.printStackTrace();
} finally {
try {
qmConfig.close();
} catch (Exception e) {
System.out.println(
"MQeAdmin: Error closing QueueManager Configuration");
e.printStackTrace();
}
}
}
/**
* Stops the Queue Manager
*/
public void stopQM() {
try {
// System.out.println("MQeAdmin: Stopping QM");
if (qm.isQueueManagerActive()) {
qm.closeQuiesce(3000);
}
} catch (Exception e) {
System.out.println("MQeAdmin: Error closing QM " + e);
e.printStackTrace();
}
}
/**
* Starts the Queue Manager
*/
public void startQM() {
try {
qm = MQeQueueManager.getDefaultQueueManager();
if (qm == null) {
qm = new MQeQueueManager();
}
if (parms != null) {
qm.activate(parms);
}
} catch (Exception e) {
System.out.println("MQeAdmin: Error starting QM " + e);
e.printStackTrace();
}
}
Chapter 5. Messaging 131
/**
* Creates a listener on the Queue Manager
*/
public void createListener(int portNumber) {
try {
MQeCommunicationsListenerAdminMsg createMessage =
new MQeCommunicationsListenerAdminMsg();
// set the name of the resource we wish to create
createMessage.setName("Listener 1");
// provide the parameters to the method that will set the action of
the
// administration message as well as the correct parameter names
using our
// input
createMessage.create(
adapterClass,
portNumber,
channelTimeout,
maxNumberChannels);
// set the target queue manager
createMessage.setTargetQMgr(myQmName);
MQeAdminMsg response = sendMsgAndWait(createMessage);
if (response.getRC() != MQeAdminMsg.RC_Success) {
throw new MQeException(response.getReason());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Starts the Listener that should have already been created on the QM.
*/
public void startListener() {
try {
MQeCommunicationsListenerAdminMsg startMessage =
new MQeCommunicationsListenerAdminMsg();
// set the name of the resource we wish to create
startMessage.setName("Listener 1");
// provide the parameters to the method that will set the action of
the

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.