Mule jms Endpoint Example

0

In this article we will look into JMS transport and go thru some basic concepts. We will send messages to and from queues. Another model that JMS supports is topic. In the queuing model, a sender posts messages to a particular queue and one of the consumers get to receive the message from the queue.
In the below xml, we define a JMS inbound-endpoint element to which JMS messages are sent.

<jms:endpoint name="in" queue="in" exchange-pattern="request-response"/>

We can also produce an outbound event using JMS outbound-endpoint.

<jms:outbound-endpoint ref="in" exchange-pattern="request-response"/>

In queue model, there is a single consumer where a topic model follows publish-subscribe model, you have multiple subscribers registered to receive the messages.
Another important element is the connector element which configures a generic connector for sending and receiving messages over JMS queues.

<jms:activemq-connector name="jmsConnector" />

JMS Asynchronous Example

In the below flow, we have configured a JMS inbound endpoint to which we will send a message asynchronously, won’t wait for any response as defined by exchange-pattern set to one-way.

<flow name="jmsInFlow">
    <jms:inbound-endpoint queue="inQ" exchange-pattern="one-way" />
    <custom-processor class="com.javarticles.mule.CustomProcessor"/>
</flow>	

The message is received from the JMS queue and then sent to the next processor in the flow.

CustomProcessor:

package com.javarticles.mule;

import org.mule.api.MuleEvent;
import org.mule.api.MuleException;
import org.mule.api.processor.MessageProcessor;

public class CustomProcessor implements MessageProcessor {

    @Override
    public MuleEvent process(MuleEvent event) throws MuleException {
        System.out.println("Process " + event.getMessageAsString());
        return event;
    }

}

JMS Synchronous Example

In this example, we will see how to send a receive a response. If you note the exchange pattern on the JMS inbound endpoint is set to request-response indicating that a response is expected. The JMS inbound endpoint refers to endpoint reference “in” which has the exchange-pattern attribute set.

A message is sent to a vm endpoint vm://vminbound which in turn is passed on to the JMS outbound endpoint. An outbound event is created and sent to the JMS inbound endpoint which returns the response back to the first flow.

muleContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns:test="http://www.mulesoft.org/schema/mule/test"
	xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
	xsi:schemaLocation="
       http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
       http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/current/mule-test.xsd
       http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
       http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">

	<jms:activemq-connector name="jmsConnector" />
	<jms:endpoint name="in" queue="in" exchange-pattern="request-response"/>

	<flow name="jmsInFlow">
		<jms:inbound-endpoint queue="inQ" exchange-pattern="one-way" />
		<custom-processor class="com.javarticles.mule.CustomProcessor"/>
	</flow>
	
	<flow name="vm2JmsFlow">
		<vm:inbound-endpoint path="vminbound" exchange-pattern="request-response" />
		<jms:outbound-endpoint ref="in" exchange-pattern="request-response"/>
	</flow>

	<flow name="response-flow-case-1">
		<jms:inbound-endpoint ref="in" />
		<append-string-transformer message=" - response from jms:in" />
	</flow>
</mule>


Below example, we send message sent to the JMS inbound endpoint.

MuleClient muleClient = ctx.getClient();
MuleMessage response = muleClient.send("jms://inQ", "Message to Jms inQ", null);
System.out.println("Response: " + response.getPayloadAsString());

MuleJmsEndpointExample:

package com.javarticles.mule;

import org.mule.api.MuleContext;
import org.mule.api.MuleMessage;
import org.mule.api.client.MuleClient;
import org.mule.api.context.MuleContextBuilder;
import org.mule.api.context.MuleContextFactory;
import org.mule.config.DefaultMuleConfiguration;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextBuilder;
import org.mule.context.DefaultMuleContextFactory;

public class MuleJmsEndpointExample {
    public static void main(String[] args) throws Exception {
        DefaultMuleConfiguration dmc = new DefaultMuleConfiguration();
        dmc.setId("muleexample");
        dmc.setWorkingDirectory("/esb/mule");
        SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder(
                "muleContext.xml");
        MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
        contextBuilder.setMuleConfiguration(dmc);
        MuleContextFactory contextFactory = new DefaultMuleContextFactory();
        MuleContext ctx = contextFactory.createMuleContext(configBuilder,
                contextBuilder);
        ctx.start();
        try {
            MuleClient muleClient = ctx.getClient();
            muleClient.dispatch("jms://inQ", "Message to Jms inQ", null);
            
            MuleMessage response = muleClient.send("vm://vminbound", "Message to vminbound", null);
            System.out.println("Response: " + response.getPayloadAsString());
        } finally {
            ctx.dispose();
        }
    }
}

From the below output, you can see, in case of synchronous case, JMS creates a temporary queue for the response data and set the queue name as the Reply-To property of the responding JMS message.

Output:

Process Message to Jms inQ
[INFO ] 2018-05-18 23:21:34.477 [ActiveMQ Session Task-1] DefaultTransportServiceDescriptor - Loading default outbound transformer: org.mule.transport.jms.transformers.ObjectToJMSMessage
[INFO ] 2018-05-18 23:21:34.477 [ActiveMQ Session Task-1] JmsReplyToHandler - Reply Message sent to: temp-queue://ID:INMAA1-L0005-53497-1526665894092-3:1:1 with correlationID:1ac61880-5ac4-11e8-8837-a0afbd832eee
Response: Message to vminbound - response from jms:in

Download the source code

This was an example about JMS endpoint.

You can download the source code here: muleJmsEndpointExample.zip
Share.

Comments are closed.