Chapter 12 IBM WebSphere MQ retrieves mail

Retrieve email

To retrieve messages, do the following:

  1. Follow the instructions in creating a Connection Object to create a connection object. In this case, create%Net.MQRecvThe instance.ConnectionObject has a message queue from which messages can be retrieved.
  2. Call the following methods as needed:
  • %Get()– Returns a string message as the first argument by reference.
  • %GetStream()– Given an initialized stream of file characters, this method retrieves the message from the queue and places it in the file associated with the stream. Note that the stream must be setFilenameProperty to initialize it. Binary streams are not supported.
  1. Check the value returned by the called method. See “Getting Error Code.” Remember, when the queue is empty, IBMWebSphere MQreturn2033.
  2. After retrieving the message, callConnectionThe object’s%Close()Method to release a handle to a dynamically linked library.

Example 1: ReceiveString()

The following class method retrieves the message from the MQtest queue.

///Method returns string or null or error message
ClassMethod ReceiveString(a) As %String
{
 Set recv=##class(%Net.MQRecv%).New(a)Set queue="mqtest"
 Set qm="QM_antigua"
 Set chan="S_antigua/TCP/antigua(1414)"
 Set logfile="c:\mq-recv-log.txt"
 
 Set check=recv.%Init(queue,qm,chan,logfile)
 If 'check Quit recv.%GetLastError() Set check=recv.%Get(.msg) If 'check {
     Set reasoncode=recv.%GetLastError()
     If reasoncode=2033 Quit ""
     Quit "ERROR: "_reasoncode
     }
 
 Quit msg
}
Copy the code

Example 2: ReceiveCharacterStream()

The following method retrieves longer messages because it uses %GetStream() :

/// Method returns reason code from IBM WebSphere MQ
ClassMethod ReceiveCharacterStream(a) As %Integer
{
 Set recv=##class(%Net.MQRecv%).New(a)Set queue="mqtest"
 Set qm="QM_antigua"
 Set chan="S_antigua/TCP/antigua(1414)"
 Set logfile="c:\mq-recv-log.txt"

 Set check=recv.%Init(queue,qm,chan,logfile)
 If 'check Quit recv.%GetLastError() //initialize the stream and tell it what file to use //make sure filename is unique we can tell what we received Set longmsg=##class(%FileCharacterStream).%New() Set longmsg.Filename="c:\mq-received"_$h_".txt" Set check=recv.%GetStream(longmsg) If 'check  Quit recv.%GetLastError()
 Quit check
}
Copy the code

Update message information

The % net.mqsend and % net.mqrecv classes also provide the following methods:

%CorId()

Update (by reference) the association ID of the last message read.

%ReplyQMgrName()

Update (by reference) the reply queue manager name for the last message read.

%ReplyQName()

Update (by reference) the reply queue name of the last message read.

Troubleshooting

If you encounter problems using the InterSystems IRIS interface for IBM WebSphere MQ, you should first determine that the client is installed correctly and can communicate with the server. To perform such tests, you can use the sample program provided with IBM WebSphere MQ. The executable file is located in the bin directory of the IBM WebSphere MQ client.

The following steps show you how to use these sample programs on Windows. On other operating systems, the details may vary; Refer to the IBM documentation and check the name of the file that exists in your client.

  1. Create a file namedMQSERVEREnvironment variables of. The format of its value should bechannel_name/Transport/server, includingchannel_nameIs the name of the channel to use,TransportIs a string indicating the transport to use, andserverIs the name of the server. Such as:S_Antigua/TCP/Antigua
  2. On the command line, type the following command:
amqsputc queue_name queue_manager_name
Copy the code

Where QUEUE_NAME is the name of the queue to be used and QUEUE_MANAGER_NAME is the name of the queue manager. Such as:

amqsputc mqtest QM_antigua
Copy the code

If the AMQSPUTC command is not recognized, ensure that the PATH environment variable is updated to include the bin directory of the IBM WebSphere MQ client.

  1. You should see a few lines of code like this:
Sample AMQSPUT0 start
target queue is mqtest
Copy the code
  1. You can now send messages. Just type each message and press Enter after each message. Such as:
sample message 1
sample message 2
Copy the code
  1. After sending the email, press Enter twice. You will then see the following line:
Sample AMQSPUT0 end
Copy the code
  1. To complete this test, we retrieve the messages sent to the queue. On the command line, type the following command:
amqsgetc queue_name queue_manager_name
Copy the code

Where QUEUE_NAME is the name of the queue to be used and QUEUE_MANAGER_NAME is the name of the queue manager. Such as:

  1. You should then see a starting line, followed by the previously sent message, as follows:
Sample AMQSGET0 start
message <sample message 1>
message <sample message 2>
Copy the code
  1. The sample program waits briefly to receive any additional messages and then displays the following:
no more messages
Sample AMQSGET0 end
Copy the code

If the test fails, refer to the IBM documentation. Possible causes include the following:

  • Security issues
  • The queue definition is incorrect
  • The queue manager is not started