One of the very handful features in WSO2 Enterprise Integrator for RabbitMQ, is sequence template. That functionality is not so popular as proxy services, but can save a lot of time. I often have had to do integrations with RabbitMQ, so i wanted to simplify my integrations. For this I created that useful template sequence for send messages to different exchanges with various routing key in RabbitMQ.
I assume, that you have configured WSO2 Enterprise Integrator product, for sending messages to RabbitMQ. If not, you should configure accordingly to this documentation. It is good practice, to have configured like it has been described. This can be also used in newer products of WSO2 like Micro Integrator (MI). And for this product the configuration of RabbitMQ you can find in this documentation.
The main problem was that, I was struggling with very nasty looking address for RabbitMQ sender, like this:
It’s very hard to read what is going on. What are the sender parameters like
routing key which are important in sending in RabbitMQ. Where the message is sending. Second thing, is that, you always need use
FORCE_SC_ACCEPTED property for sending. Without that, you will recived warnings in your mediation. I will show below the very basic construction and usage of this template, but you can adapt to your own needs.
So I use template, named
send.rabbitmq, like below:
<template name="send.rabbitmq" xmlns="http://ws.apache.org/ns/synapse"> <parameter name="exchangeName"/> <parameter name="routingKey"/> <sequence> <property name="REST_URL_POSTFIX" action="remove" scope="axis2"/> <property name="OUT_ONLY" scope="default" type="STRING" value="true"/> <property name="FORCE_SC_ACCEPTED" scope="axis2" type="STRING" value="true"/> <header name="To" scope="default" expression="concat('rabbitmq:/?rabbitmq.connection.factory=CachedRabbitMQConnectionFactory &rabbitmq.exchange.name=',$func:exchangeName, '&rabbitmq.queue.routing.key=',$func:routingKey)" xmlns:ns="http://org.apache.synapse/xsd"/> <log/> <send/> </sequence> </template>
In first look, the simplify is not so obvious. We use XPATH
concat function, to create
To: header, which is used in
<send/> mediator. In template I also use removing url postfix, for removing my REST API information. And also use logging to get some extra information. The important part is usage of
<parameter name="exchangeName"/> and
<parameter name="routingKey"/> which corresponds to the
$func:routingKey in concatenation. In the same way, you can add some other parameters.The benefits are in usage, which is simple as you see below.
<call-template target="send.rabbitmq"> <with-param name="exchangeName" value="testExch"/> <with-param name="routingKey" value="testRoutingKey"/> </call-template>
You need only call this mediator with two parameters:
routingKey. It’s very handful, and also look nice in graphical.
As you see, there is also option to use expressions for that parameters. For example from context message:
$ctx:sampleProperty or other XPATH expression. This template can be used in proxyServices, APIs, and sequences mediations.
To sum up, the solution can be expanded, with many more parameters. I used only that parameters because I needed only them. You can add in concatenation other RabbitMQ sender parameters. Even you can set other hostname address, to send to other RabbitMQ instances. You can used it also in WSO2 API Manager as RabbitMQ message producer. I hope that would help You!