Question & Answer
Question
IIB looping the MQGet node
Answer
On Demand Consulting
Author: Phil Bareham
Introduction
Quite often we see requirements for a message flow to repeatedly execute the MQGet node - in effect - looping around the MQGet node.
This article describes how to implement this in a message flow.
Note: this article uses the MQGet node as the example we see most frequently for a repeated execution of the same node. The solution proposed here could equally apply to many other IIB nodes - for example - a compute node or an MQOutput node.

Where the 'output' terminal from the MQGet node 'Get from Queue' is connected to the compute node 'Do some processing' whose output terminal is connected back to the input terminal of 'Get From Queue'.
THIS IS A REALLY BAD IDEA! It is likely to result in an IIB error with a stack overflow this is caused by the way IIB stacks each invoked node.
--Propagate to MQGet node to get all messages on named queue
--Uses shared variable to maintain loop
SET KeepLooping = TRUE;
WHILE KeepLooping DO
--set LocalEnv NQget properties to name of Subscriber Queue from incoming message
SET OutputLocalEnvironment.MQ.GET.QueueName
= InputRoot.XMLNSC.subMessage.subQueue;
-- Set local env destination for all messages
SET OutputLocalEnvironment.Destination.MQ.DestinationData[1].queueName
= InputRoot.XMLNSC.subMessage.destQueue;
PROPAGATE TO TERMINAL 'out';
END WHILE;
RETURN FALSE;
END;
The MQGet node 'Get message from queue' has its 'No message' terminal connected to the compute node 'EndLoop' which sets the KeepLooping shared variable to false to terminate the loop.
This approach has a limitation in that the message flow would have to run single threaded to avoid the shared variable being updated by another instance of the same message flow.
This article describes how to implement this in a message flow.
Note: this article uses the MQGet node as the example we see most frequently for a repeated execution of the same node. The solution proposed here could equally apply to many other IIB nodes - for example - a compute node or an MQOutput node.
Hard wiring the loop using terminal connections
Your first though maybe to hard wire the loop in the message flow like this:
Where the 'output' terminal from the MQGet node 'Get from Queue' is connected to the compute node 'Do some processing' whose output terminal is connected back to the input terminal of 'Get From Queue'.
THIS IS A REALLY BAD IDEA! It is likely to result in an IIB error with a stack overflow this is caused by the way IIB stacks each invoked node.
Using ESQL PROPAGATE to invoke the MQGet node multiple times
A solution to this is the use the ESQL PROPAGATE statement to cause the MQGET node to be executed multiples times something like this:

Where the PropagateUntilEmptyQueue ESQL uses a shared variable to control the Propagate Loop:
DECLARE KeepLooping SHARED BOOLEAN;
CREATE COMPUTE MODULE ConsumerFlow_PropageteUntilEmptyQueue
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
CALL CopyMessageHeaders();
-- CALL CopyEntireMessage();

Where the PropagateUntilEmptyQueue ESQL uses a shared variable to control the Propagate Loop:
DECLARE KeepLooping SHARED BOOLEAN;
CREATE COMPUTE MODULE ConsumerFlow_PropageteUntilEmptyQueue
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
CALL CopyMessageHeaders();
-- CALL CopyEntireMessage();
--Propagate to MQGet node to get all messages on named queue
--Uses shared variable to maintain loop
SET KeepLooping = TRUE;
WHILE KeepLooping DO
--set LocalEnv NQget properties to name of Subscriber Queue from incoming message
SET OutputLocalEnvironment.MQ.GET.QueueName
= InputRoot.XMLNSC.subMessage.subQueue;
-- Set local env destination for all messages
SET OutputLocalEnvironment.Destination.MQ.DestinationData[1].queueName
= InputRoot.XMLNSC.subMessage.destQueue;
PROPAGATE TO TERMINAL 'out';
END WHILE;
RETURN FALSE;
END;
The MQGet node 'Get message from queue' has its 'No message' terminal connected to the compute node 'EndLoop' which sets the KeepLooping shared variable to false to terminate the loop.
This approach has a limitation in that the message flow would have to run single threaded to avoid the shared variable being updated by another instance of the same message flow.
[{"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Product":{"code":"SSQTW3","label":"IBM On Demand Consulting for Hybrid Cloud"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"","label":""}}]
Was this topic helpful?
Document Information
Modified date:
16 March 2019
UID
ibm10776265