In order to create a listener is it necessary to use an administration message. The following is based upon the example example.config.ConfigListener, the administration message is instantiated as follows:
MQeCommunicationsListenerAdminMsg createMessage = new MQeCommunicationsListenerAdminMsg();
We now need to provide a name for the listener:
createMessage.setName("Listener1");
The name of the queue manager to which the administration message is intended is also required:
createMessage.setTargetQMgr(queueManagerName);
The next thing we need to do is set the action for the administration message as well as providing the information the listener requires in order to function.
createMessage.create(com.ibm.mqe.adapters.MQeTcpipHistoryAdapter, 8087, 36000000, 10);
The first parameter provides the name of the communications adapter we wish to use, in this instance we have stipulated the MQeTcpipHistoryAdapter, an alias may be used instead. The type of communications adapter being used by the listener needs to be made known to clients wishing to connect to the queue manager using the listener.
The second parameter defines the named location the listener uses, in this instance an IP port number of 8087, again the clients will need to be aware of this in order to contact this listener.
The third parameter specifies the channel timeout value. This value is used to determine when an incoming channel should be closed. MQe polls the channels, if a channel has been idle for longer than the timeout value it will be closed.
The last parameter determines the maximum number of channels the listener will have running at any one time. If a client tries to connect once this value has been reached the connection is refused.
Having set the correct action and provided the relevant information we can set the message type, in this instance we are using a request message style which indicates we would like a reply to indicate success or failure. However, it might make no difference if a description is altered successfully or not. In this case, use a message style of datagram which indicates no reply is required.
createMessage.putInt(MQe.Msg_Style, MQe.Msg_Style_Request);
When requesting a reply, provide the queue and owning queue manager name to which the reply must be sent. This example uses the default administration reply queue.
createMessage.putAscii(MQe.Msg_ReplyToQ, MQe.Admin_Reply_Queue_Name); createMessage.putAscii(MQe.Msg_ReplyToQMgr, queueManagerName);
To get the correct reply message that corresponds to our administration message, use a correlation ID. This is copied from the administration message into the reply so we can get the correct message. To generate an id that is relatively safe as being unique, use the system time:
String match = "Msg" + System.currentTimeMillis(); createMessage.putArrayOfByte(MQe.Msg_CorrelID, match.getBytes());
We are now in a position to put the administration message to the administration queue of the target queue manager. The last two parameters provide the ability to use an attribute and an id to allow the undo method to be called, neither of which we shall worry about at this juncture.
queueManager.putMessage(queueManagerName, MQe.Admin_Queue_Name, createMessage, null, 0);
Having put the message to the queue we shall now wait for a reply. As can be seen we use the correlation identifier we used to put the message in order to get the reply and there is a useful method that provides us with the reason code to indicate success or failure.
MQeFields filter = new MQeFields(); filter.putArrayOfByte(MQe.Msg_CorrelID, match.getBytes()); // now wait for a reply MQeAdminMsg response = (MQeAdminMsg) queueManager.waitForMessage(queueManagerName, MQe.Admin_Reply_Queue_Name, filter, null, 0, 3000); // the administration message has a method that // will get out the return code : switch (response.getRC()) { case MQeAdminMsg.RC_Success : break;
Having successfully created our listener we need to start it, the listener is only automatically started on the next restart of the queue manager. Again an administration message is required to start or stop a listener, we can use the approach taken above, using the following methods in the MQeCommunicationsListenerAdminMsg class. To start the listener:
MQeCommunicationsListenerAdminMsg startMessage = new MQeCommunicationsListenerAdminMsg(); . . . startMessage.start();
To stop the listener:
MQeCommunicationsListenerAdminMsg startMessage = new MQeCommunicationsListenerAdminMsg(); . . . startMessage.stop();
In order to delete a listener we need to set the action of the administration message to delete as follows:
deleteMessage.setAction(MQeAdminMsg.Action_Delete);
If you try to delete a listener that is running you will receive an exception, so make sure your listener has successfully stopped before trying to delete it.