Db2 MQ functions and Db2 MQ XML stored procedures

You can use the Db2 MQ functions and stored procedures to send messages to a message queue or to receive messages from the message queue.

The Db2 MQ functions support the following types of operations:

  • Send and forget, where no reply is needed.
  • Read or receive, where one or all messages are either read without removing them from the queue, or received and removed from the queue.
  • Request and response, where a sending application needs a response to a request.
  • Publish and subscribe, where messages are assigned to specific publisher services and are sent to queues. Applications that subscribe to the corresponding subscriber service can monitor specific messages.

You can use the Db2 MQ functions and stored procedures to send messages to a message queue or to receive messages from the message queue. You can send a request to a message queue and receive a response, and you can also publish messages to the IBM® MQ publisher and subscribe to messages that have been published with specific topics. The Db2 MQ XML functions and stored procedures enable you to query XML documents and then publish the results to a message queue.

The Db2 MQ functions include scalar functions, table functions, and XML-specific functions. For each of these functions, you can call a version that uses the MQI. The function signatures are the same. However, the qualifying schema names are different. To call an MQI-based function, specify the schema name DB2MQ.

Requirement: Before you can call the version of these functions that uses MQI , you need to populate the Db2 MQ tables.

The following table describes the Db2 MQ scalar functions.

Table 1. Db2 MQ scalar functions
Scalar function Description
MQREAD (receive-service, service-policy) MQREAD returns a message in a VARCHAR variable from the MQ location specified by receive-service, using the policy defined in service-policy. This operation does not remove the message from the head of the queue but instead returns it. If no messages are available to be returned, a null value is returned.
MQREADCLOB (receive-service, service-policy) MQREADCLOB returns a message in a CLOB variable from the MQ location specified by receive-service, using the policy defined in service-policy. This operation does not remove the message from the head of the queue but instead returns it. If no messages are available to be returned, a null value is returned.
MQRECEIVE (receive-service, service-policy, correlation-id) MQRECEIVE returns a message in a VARCHAR variable from the MQ location specified by receive-service, using the policy defined in service-policy. This operation removes the message from the queue. If correlation-id is specified, the first message with a matching correlation identifier is returned; if correlation-id is not specified, the message at the beginning of queue is returned. If no messages are available to be returned, a null value is returned.
MQRECEIVECLOB (receive-service, service-policy, correlation-id) MQRECEIVECLOB returns a message in a CLOB variable from the MQ location specified by receive-service, using the policy defined in service-policy. This operation removes the message from the queue. If correlation-id is specified, the first message with a matching correlation identifier is returned; if correlation-id is not specified, the message at the head of queue is returned. If no messages are available to be returned, a null value is returned.
MQSEND (send-service, service-policy, msg-data, correlation-id) MQSEND sends the data in a VARCHAR or CLOB variable msg-data to the MQ location specified by send-service, using the policy defined in service-policy. An optional user-defined message correlation identifier can be specified by correlation-id. The return value is 1 if successful or 0 if not successful.
Notes:
  1. You can send or receive messages in VARCHAR variables or CLOB variables. The maximum length for a message in a VARCHAR variable is 32 KB. The maximum length for a message in a CLOB variable is 2 MB.

The following table describes the MQ table functions that Db2 can use.

Table 2. Db2 MQ table functions
Table function Description
MQREADALL (receive-service, service-policy, num-rows) MQREADALL returns a table that contains the messages and message metadata in VARCHAR variables from the MQ location specified by receive-service, using the policy defined in service-policy. This operation does not remove the messages from the queue. If num-rows is specified, a maximum of num-rows messages is returned; if num-rows is not specified, all available messages are returned.
MQREADALLCLOB (receive-service, service-policy, num-rows) MQREADALLCLOB returns a table that contains the messages and message metadata in CLOB variables from the MQ location specified by receive-service, using the policy defined in service-policy. This operation does not remove the messages from the queue. If num-rows is specified, a maximum of num-rows messages is returned; if num-rows is not specified, all available messages are returned.
MQRECEIVEALL (receive-service, service-policy, correlation-id, num-rows) MQRECEIVEALL returns a table that contains the messages and message metadata in VARCHAR variables from the MQ location specified by receive-service, using the policy defined in service-policy. This operation removes the messages from the queue. If correlation-id is specified, only those messages with a matching correlation identifier are returned; if correlation-id is not specified, all available messages are returned. If num-rows is specified, a maximum of num-rows messages is returned; if num-rows is not specified, all available messages are returned.
MQRECEIVEALLCLOB (receive-service, service-policy, correlation-id, num-rows) MQRECEIVEALLCLOB returns a table that contains the messages and message metadata in CLOB variables from the MQ location specified by receive-service, using the policy defined in service-policy. This operation removes the messages from the queue. If correlation-id is specified, only those messages with a matching correlation identifier are returned; if correlation-id is not specified, all available messages are returned. If num-rows is specified, a maximum of num-rows messages is returned; if num-rows is not specified, all available messages are returned.
Notes:
  1. You can send or receive messages in VARCHAR variables or CLOB variables. The maximum length for a message in a VARCHAR variable is 32 KB. The maximum length for a message in a CLOB variable is 2 MB.
  2. The first column of the result table of a Db2 MQ table function contains the message.