Java codebase

This section describes how to develop and run a basic "HelloWorld" application in the Java codebase.

Developing the Java "HelloWorld" application

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.

Overview of the examples.helloworld.run program

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();
      	  }
    	}

Stage 1: Start the queue manager

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.

Stage 2: Create a message and put to a local queue

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");
    	}

Stage 3: Get message from a local queue

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.

Stage 4: Shutdown

This section describes how to stop a queue manager and delete the definition of the queue manager.

Stopping 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.");
    	}
Deleting the definition of the queue manager from the disk

You can use the examples.helloworld.Unconfigure program to remove the queue manager from disk.

Running the Java "HelloWorld" application

From a command prompt, set up your classpath to refer to the WebSphere MQ Everyplace class files. These are available in the Java directory, in which you installed the Websphere MQ Everyplace product.

Ensure that your shell has the ability to create and modify the ./QueueManagers directory on your system. If it does not have this ability, you should change the source of the examples.helloworld programs, such that they refer to an accessible directory, and re-compile the java code.

Invoke the Configure program to create the queue manager. The syntax will depend on the Java Virtual Machine (JVM) you use. The IBM JVM is invoked using the "java" command, for example java examples.helloworld.Configure. This creates the queue manager on disk.

Run the java examples.helloworld.Run hello world program. This puts a message to a local queue, gets the message back and displays part of it.

You can now destroy the queue manager on the disk using java examples.helloworld.Unconfigure.

Refer to Running Applications, to understand how to set up an environment, which runs WebSphere MQ Everyplace applications.