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 will include a header,
contentType, containing the content type associated with the input.
If none is supplied, a default value, text/plain; charset=UTF-8,
will be used.
To use the POSTMessage activity, you will first need to:
- Include both
zero.assemble.flowandzero.messaging.connectionin your 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 may be used with the POSTMessage activity:
-
destination - The message will be 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, the
destinationname is 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
connectionConfigis required.
If the destination name matches a configured connection destination which specifies
a connectionConfig value, the value specified by the destination configuration will
override any value specified in the activity broker property.
The messaging protocol will package the input to POSTMessage as an
instance of javax.jms.Message for delivery. For example, a String
input will be 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 may be used to start a flow to process a message that has arrived on a queue:
- The
FlowMessageHandleris used to handle themessageevent, generated by the messaging receiver when a message arrives on a monitored queue, and start a flow. - The
receiveMessageactivity is used within a flow started by theFlowMessageHandlerto accept the message and passes the message contents as the activity output.
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, the
default value, text/plain; charset=UTF-8, is used.
To use the FlowMessageHandler and the receiveMessage activity, you will need to:
- Include both
zero.assemble.flowandzero.messaging.receiverin your 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
FlowMessageHandlerto handle themessageevent for messages arriving on the queue. - Provide a flow file that uses the
receiveMessageactivity to accept 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" }
}]
Under the above configuration, the FlowMessageHandler will be invoked when a
message arrives for the queue, myQueue. The view property in the
handler instance data identifies the flow that will 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 simple 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 exploits the global context type handler registered by
zero.connection.jms to retrieve the message contents from the javax.jms.Message.
For example if the message is a TextMessage then the output of
receiveMessage will be a String.
See the zero.connection.jms developer's guide for more information about working with javax.jms.Message objects
in the global context and with the Connection API.