Using messaging within flows

This topic describes how to write flows that send messages using the IBM® Reliable Transport Extension for WebSphere® sMash or flows that respond to a message arriving on a queue.

Sending a message from within a flow

The POSTMessage activity can be used within a flow to send a message to a queue. This activity uses the Connection API to send its input to a connection destination, using the messaging protocol.

The message sent by the POSTMessage activity includes a header, contentType, containing the content type the input to the activity. If none is supplied, the value text/plain; charset=UTF-8 is used.

The following configuration is required to use the POSTMessage activity:

  • Include both zero.assemble.flow and zero.messaging.connection in the application dependencies.
  • Configure the connection to the messaging broker. For more information, see Configuring connections to messaging brokers.
  • Optionally, configure a connection destination definition describing the target queue.

The following example flow source uses the POSTMessage activity to send a message:

<POSTMessage name="POSTMessage_1" destination="myQueue" broker="myBroker">
   <input value="${receiveMessage_0}" content-type="text/plain"/>
</POSTMessage>

The following parameters can be used with the POSTMessage activity:

destination
The message is posted to the connection destination named by this required parameter. If a matching connection destination is not configured or does not specify a queue name, then the destination name is also used as the queue name.
broker
This optional parameter can be used to specify the broker connection configuration. If this is not specified, a matching connection destination with a valid connectionConfig is required.

If the destination name matches a configured connection destination that specifies a connectionConfig value, then the value specified by the destination configuration overrides any value specified in the activity broker property.

The messaging protocol packages the input to POSTMessage as an instance of javax.jms.Message for delivery. For example, a String input is sent as a TextMessage. For more information, see Using messaging with the Connection API.

Starting a flow when a message arrives

The IBM Reliable Transport Extension messaging receiver includes two components that can be used to start a flow to process a message that has arrived on a queue:

  • The FlowMessageHandler is used to handle the message event, which is generated by the messaging receiver when a message arrives on a monitored queue, and start a flow.
  • The receiveMessage activity is used within a flow started by the FlowMessageHandler to obtain the contents of the received message.

The output of the receiveMessage activity is annotated with the content type specified by the message header, contentType. If no contentType header is included with the message, then the default value, text/plain; charset=UTF-8, is used.

The following configuration is required to use FlowMessageHandler and the receiveMessage activity:

  • Include both zero.assemble.flow and zero.messaging.receiver in the application dependencies.
  • Configure a messaging kicker to monitor the queue and inform the receiver application when a message arrives.
  • Configure the messaging receiver and its connection to the messaging broker.
  • Configure the FlowMessageHandler to handle the message event for messages arriving on the queue.
  • Provide a flow file that uses the receiveMessage activity to accept the contents of the message.

See Using the messaging kicker and receiver for more information about these concepts and their configuration.

The following configuration extract specifies that the FlowMessageHandler should handle a message event associated with the specified queue:

/config/handlers += [{
  "events" : "message",
  "handler" : "zero.messaging.receiver.flow.FlowMessageHandler.class",
  "conditions" : "/request/messaging/queue =~ myQueue",
  "instanceData" : { "view" : "myMessageFlow.flow" }
}]

This configuration specifies that the FlowMessageHandler is invoked when a message arrives on the queue, myQueue. The view property in the handler instance data identifies the flow to be invoked, myMessageFlow.flow.

Here is an example flow that can be used with the above handler configuration. The flow is saved as /app/view/myMessageFlow.flow.

<process name="myMessageFlow" expressionLanguage="Groovy">
  <receiveMessage name="receiveMessage_0"/>
  <POSTMessage name="POSTMessage_0" destination="targetQueue">
    <input value="${receiveMessage_0}"/>
  </POSTMessage>
</process>

In this flow, the receieveMessage activity is used to accept the message. The output of the receiveMessage activity is then used as the input to a POSTMessage activity which forwards the message to another destination.

The receiveMessage activity exploits the global context type handler registered by zero.connection.jms to retrieve the message contents from the javax.jms.Message object. For example if the message is a TextMessage then the output of receiveMessage is a String. For more information about working with javax.jms.Message objects in the global context and with the Connection API, see the Developer's Guide topics about the protocol extension for JMS.

Version 1.1.26688