The simplest WebSphere MQ Everyplace queue manager is a queue manager that uses a registry based upon the internal default values. The queue manager could be created without any queues, but its functionality would be severely limited. The example we create contains four standard queues:
The simplest queue manager has no security and has a registry stored in the local file system. The steps to achieve are:
These actions are described for both the Java codebase and the C codebase, with example code for each. The example Java code is shipped as examples.config.CreateQueueManager. For C example code, refer to the HelloWorld compilation section and the transport-c file in the Broker example.
Registries are created in Java by using the class com.ibm.mqe.MQeQueueManagerConfigure. An instance of this class is created, and activated by passing it some initialization parameters. The parameters are supplied in the form of an MQeFields object. Within this MQeFields are contained two sub fields, one holding information about the registry, and one holding information about the queue manager being created. As we are creating a very simple queue manager, we only need to pass two parameters, the queue manager name, in the queue manager parameters, and the registry location, in the registry parameters. We can then use the MQeQueueManagerConfigure to create the standard queues.
First, create three fields objects, one for the queue manager parameters, one for the Registry parameters. The third field object, parameters, is used to contain both the queue manager and registry fields objects.
MQeFields parms = new MQeFields(); MQeFields queueManagerParameters = new MQeFields(); MQeFields registryParameters = new MQeFields();
The queue manager name needs to be set. Use the MQeQueueManager.Name as the Field Label constant.
queueManagerParameters.putAscii(MQeQueueManager.Name, queueManagerName);
The location of the persistent registry needs to be specified. Do this in the registry parameters field object. Use the MQeRegistry.DirName as the Field Label constant.
registryParameters.putAscii(MQeRegistry.DirName, registryLocation);
The queue manager and registry parameters can now be set embedded the main fields object.
parms.putFields(MQeQueueManager.QueueManager, queueManagerParameters); parms.putFields(MQeQueueManager.Registry, registryParameters);
An instance of MQeQueueManagerConfigure can be created now. This needs the parameters fields object, plus a String identifying the details of the queue store to use.
MQeQueueManagerConfigure qmConfig = new MQeQueueManagerConfigure(parms, queueStore);
The four common types of queues can now be created via four convenience methods as follows:
qmConfig.defineQueueManager(); qmConfig.defineDefaultSystemQueue(); qmConfig.defineDefaultDeadLetterQueue(); qmConfig.defineDefaultAdminReplyQueue(); qmConfig.defineDefaultAdminQueue();
Finally the MQeQueueManagerConfigure object can be closed.
qmConfig.close();
Starting the simplest queue manager is facile, as we only need to provide the queue manager name and registry location to the queue manager constructor. This starts and activates the queue manager, and when the constructor returns the queue manager is running.
Figure 13. Start queue manager Java example
MQeQueueManager qm = newMQeQueueManager(queueManagerName, registryName);
There are other ways to start a queue manager that allow us to pass more parameters, in order to take advantage of some advanced features, which are explained in later chapters.
There are 2 ways to close down a queue manager.
This closes Queue Manager, specifying a delay to allow existing internal processes to finish normally. Note that this delay is only implemented as a series of 100ms pause and retry cycles. Calling this method will prevent any new activity, such as transmitting a message, from being started, but will allow activities already in progress to complete. The delay is a suggestion only, and various JVM dependant thread scheduling factors could result in the delay being greater. If the activities currently in progress finish sooner, then the method will return before the expiry of the quiesce duration.
If at the expiry of this period the queue has not closed, it is forced to close.
This method closes down the queue manager. One of the close methods should be called by WebSphere MQ Everyplace applications when they have finished using the queue manager.
After this method has been called, no more event notifications will be dispatched to message listeners. It is conceivable that messages may complete their arrival after this method has been called (and before it finishes). Such messages will not be notified. Application programmers should be aware of this, and not assume that every message arrival will generate a message event.
MQeQueueManager qmgr = new MQeQueueManager(); MQeMsgObject msgObj = null; try { qmgr.putMessage(null, "MyQueue", msgObj, null, 0); } catch (MQeException e) {// Handle the exception here } qmgr.closeQuiesce(3000); // close QMgr
This closes Queue Manager immediately. One of the close methods should be called by WebSphere MQ Everyplace applications when they have finished using the queue manager.
After this method has been called, no more event notifications are dispatched to message listeners. Messages might complete their arrival after this method has been called, and before it finishes. Such messages are not notified. Application programmers should be aware of this, and not assume that every message arrival will generate a message event.
MQeQueueManager qmgr = new MQeQueueManager(); MQeMsgObject msgObj = null; try { qmgr.putMessage(null, "MyQueue", msgObj, null, 0); } catch (MQeException e) {// Handle the exception here } qmgr.closeImmediate(); // close QMgr
The call to the administrator creates the queue manager.
if ( MQERETURN_OK == rc ) { MQeQueueManagerParms qmParams = QMGR_INIT_VAL; MQeRegistryParms regParams = REGISTRY_INIT_VAL; qmParams.hQueueStore = hQueueStore; qmParams.opFlags = QMGR_Q_STORE_OP; regParams.hBaseLocationName = hRegistryDir; display("Creating the Queue Manager\n"); rc = mqeAdministrator_QueueManager_create(hAdministrator, &exceptBlk, &hQueueManager, hLocalQMName, &qmParams, ®Params); }
Figure 14. Create queue manager C example
This process involves two steps:
Creating the queue manager requires two sets of parameters, one set for the queue manager and one for the registry. Both sets of parameters are initialized. The queue store and the registry require directories.
if (MQERETURN_OK == rc) { MQeQueueManagerParms qmParams = QMGR_INIT_VAL; MQeRegistryParms regParams = REGISTRY_INIT_VAL; qmParams.hQueueStore = hQueueStore; qmParams.opFlags = QMGR_Q_STORE_OP; /* ... create the registry parameters - minimum that are required */ regParams.hBaseLocationName = hRegistryDir; display("Loading Queue Manager from registry \n"); rc = mqeQueueManager_new( &exceptBlock, &hQueueManager, hLocalQMName, &qmParams, ®Params); }
You can now start the queue manager and carry out messaging operations:
/* Start the queue manager */ if ( MQERETURN_OK == rc ) { display("Starting the Queue Manager\n"); rc = mqeQueueManager_start(hQueueManager, &exceptBlock); }
Following the removal of the message from the queue, you can stop and free the queue manager. You can also free the strings that were created. Finally, terminate the session:
(void)mqeQueueManager_stop(hQueueManager,&exceptBlock); (void)mqeQueueManager_free(hQueueManager,&exceptBlock); /* Lets do some clean up */ (void)mqeString_free(hFieldLabel,&exceptBlock); (void)mqeString_free(hLocalQMName,&exceptBlock); (void)mqeString_free(hLocalQueueName,&exceptBlock); (void)mqeString_free(hQueueStore,&exceptBlock); (void)mqeString_free(hRegistryDir,&exceptBlock); (void)mqeSession_terminate(&exceptBlock);