The following code is in the examples.helloworld.Run class in its complete state. Solutions using WebSphere MQ Everyplace classes are often separated into several separate tasks:
Before reading the information in this chapter, you need to configure a queue manager. The examples.helloworld.Configure program demonstrates the configuration of the queue manager. The examples.helloworld.Unconfigure program demonstrates the removal of the queue manager. This section of the documentation describes how to use the queue manager.
The main method controls the flow of the hello world application. From this code, you can see that the queue manager is started, a message is put to a queue, a message is got from a queue, and the queue manager is stopped.
Trace information can be redirected to the standard output stream if the MQE_TRACE_ON symbolic constant has its' value changed to 'true'.
public static void main(String[] args) { try { Run me = new Run(); if (MQE_TRACE_ON) { me.traceOn(); } me.start(); me.put(); me.get(); me.stop(); if (MQE_TRACE_ON) { me.traceOff(); } } catch (Exception error) { System.err.println("Error: " + error.toString()); error.printStackTrace(); } }
The examples.helloworld.Configure program creates an image of the HelloWorldQM queue manager on disk.
Before a queue manager can be used, it must be instantiated in memory, and started. The start method in the example program does this.
public void start() throws Exception { System.out.println("Starting the queue manager."); String queueManagerName = "HelloWorldQM"; String baseDirectoryName = "./QueueManagers/" + queueManagerName; // Create all the configuration information needed to construct the // queue manager in memory. MQeFields config = new MQeFields(); // Construct the queue manager section parameters. MQeFields queueManagerSection = new MQeFields(); queueManagerSection.putAscii(MQeQueueManager.Name, queueManagerName); config.putFields(MQeQueueManager.QueueManager, queueManagerSection); // Construct the registry section parameters. // In this examples, we use a public registry. MQeFields registrySection = new MQeFields(); registrySection.putAscii(MQeRegistry.Adapter, "com.ibm.mqe.adapters.MQeDiskFieldsAdapter"); registrySection.putAscii(MQeRegistry.DirName, baseDirectoryName + "/Registry"); config.putFields("Registry", registrySection); System.out.println("Starting the queue manager"); myQueueManager = new MQeQueueManager(); myQueueManager.activate(config); System.out.println("Queue manager started."); }
To start the queue manager, at a minimum you must know its name, location, and the adapter which should be used to read the queue manager's configuration information from its registry.
Activating the queue manager causes the configuration data from the disk to be read using the disk fields adapter, and the queue manager is then started and running, available for use.
The following code constructs a message, adds a unicode field with a value of "Hello World!" and the message is then put to the SYSTEM.DEFAULT.LOCAL.QUEUE on the local HelloWorldQM queue manager.
public void put() throws Exception { System.out.println("Putting the test message"); MQeMsgObject msg = new MQeMsgObject(); // Add my hello world text to the message. msg.putUnicode("myFieldName" , "Hello World!"); myQueueManager.putMessage(queueManagerName, MQe.System_Default_Queue_Name, msg, null, 0L); System.out.println("Put the test message"); }
The following code gets the "top" message from the local queue, SYSTEM.DEFAULT.LOCAL.QUEUE, checks that a message with the field myFieldName was obtained, and displays the text held in the unicode field.
This section describes how to stop a queue manager and delete the definition of the queue manager.
You can stop the queue manager using a controlled shutdown.
public void stop() throws Exception { System.out.println("Stopping the queue manager."); myQueueManager.closeQuiesce(QUIESCE_TIME); myQueueManager = null; System.out.println("Queue manager stopped."); }
You can use the examples.helloworld.Unconfigure program to remove the queue manager from disk.