Mule Flow: Reading messages from rabbitmq
In a project, Our Mule service was reading some messages from Rabbit MQ in the project. After that these messages was transformed from byte-array to object in json format. Step 2 was to convert these JSON object into the Java bean object. After converting it to Java object, passed these java object messages into our main java component for business logic.
To achieving this, I followed below steps:
- Reading some messages from Rabbit MQ
- Transformed from byte-array to object in json format
- Convert these JSON object into the Java bean object
- Sent this workload into the Java class.
For Step 1:
Created some global variables in the MULE configuration xml file.
<amqp:connector validateConnections="true" doc:name="AMQP Connector" name="AMQP_0_9_Connector" />
<amqp:endpoint exchangeName="main-exchange" queueName="<<Your Queue Name>>"
exchangeDurable="true" name="AMQP_0_9_queue1" responseTimeout="10000" doc:name="AMQP-0-9" routingKey="<<Your Routing Key>>" exchangeType="direct" />
Created flow component in the mule configuration xml file.
<flow name=”firstFlow”></flow>
Inside the flow, created amqp:inbound-endpoints.
<amqp:inbound-endpoint numberOfChannels="10" responseTimeout="10000" exchange-pattern="one-way" connector-ref="AMQP_0_9_Connector" ref="AMQP_0_9_queue1" doc:name="READING-MSG-FROM-QUEUE" ></amqp:inbound-endpoint>
For Step 2:
Transformed from byte-array to object in json format
<byte-array-to-object-transformer returnClass="java.lang.String" doc:name="Byte Array to Object" />
For Logging purpose:
<logger message="Message from Queue1: #[payload]" level="INFO"
doc:name="Logger: Outgoing to RabbitMQ" />
<set-payload name="msg" value="#[payload]" doc:name="set payload" mimeType="application/json"></set-payload>
Step 3:
Convert these JSON object into the Java bean object
<json:json-to-object-transformer returnClass="<<package_name>>.<<Class_name>>" mimeType="application/json" name="JSON_to_Object" doc:name="JSON to Object" />
Step 4:
Sent this workload into the Java class
<component class="<<package_name>>.<<class_Name>>" doc:name="Java">
<!-- This is for calling specific methoed inside the Java class-->
<method-entry-point-resolver>
<include-entry-point method="getRabbitMQMsg" />
</method-entry-point-resolver>
</component>
Complete flow of reading messages from rabbitmq and sent these messages in the Java classes :
<flow name="FlowOne" >
<amqp:inbound-endpoint numberOfChannels="10" responseTimeout="10000" exchange-pattern="one-way" connector-ref="AMQP_0_9_Connector" ref="AMQP_0_9_queue1" doc:name="READING-MSG-FROM-QUEUE" ></amqp:inbound-endpoint>
<byte-array-to-object-transformer returnClass="java.lang.String" doc:name="Byte Array to Object" />
<logger message="Message from Queue1: #[payload]" level="INFO" doc:name="Logger: Outgoing to RabbitMQ" />
<set-payload name="msg" value="#[payload]" doc:name="set payload" mimeType="application/json"></set-payload>
<json:json-to-object-transformer returnClass="<<Package_Name>>.<<Class_Name>>" mimeType="application/json" name="JSON_to_Object" doc:name="JSON to Object" />
<component class="<<Package_Name>>.<<Class_Name>>" doc:name="Java">
<method-entry-point-resolver>
<include-entry-point method="<<Method Name>>" />
</method-entry-point-resolver>
</component>
</flow>
Comments
Post a Comment