How to use a mediation route to read messages from ActiveMQ

In this “how to” you are going to build a Talend mediation route to consume a message from your ActiveMQ. A mediation route can be used for realtime application integration – providing web services, transforming data, with transactional capability and solid error handling.

Before you start working on this “how to” you need to have your activeMQ environment up and running and at least one available message queue. Therefore you need to follow this “how to” first if you haven’t already done it: How to install apache activemq and use it from Talend.

1. Build your mediation route

In this chapter you are going to add a simple mediation route. Therefore you need to start your Talend Open Studio for ESB.

1.1 Create route

  • Right click “Routes” and select “Create route”
  • Name your route “how_to_get_message_from_ActiveMQ”

Talend create route

1.2 Set up connection ActiveMQ

  • Drag and drop the cMQConnectionFactory component to your canvas

Talend cMQConnectionFactory

  • Open the basic settings
  • Select “ActiveMQ” as MQ Server because that’s the middle-ware you are using
  • Add the broker uri “tcp://localhost:61616”
  • Check the authentication box
  • Add username “admin” and password “admin”

Talend cMQConnection basic settings

1.3 Consume messages

Because you are going to read messages from a JMS Queue (ActiveMQ), you will have to use the cJMS component

Talend cJMS

  • Add the cJMS component to your canvas
  • Select “queue” as type
  • Select “TalendHowTo” as destination (read source because you are using it as an input in this case)
  • Finally add the cMQConnectionFactory component of the previous step as a ConnectionFactory

Talend cJMS basic settings

1.4 Add cMulticast

The cMulticast component is used to route one or more message to a number of endpoint at one go and process them in different ways. In this example we are going to send the message to a cDirect compoent to process the message in a subjob and we are going to send the message to a second cDirect component to put it back at the queue (TalendHowTo.archive).

  • Drag a cMulticast component to the canvas

Talend cMulticast

  • Connect the cJMS component to your cMulticast component (Row – Route)
  • Add two URIS to the basic settings of your cMulticast component because your are going to proces the message in two different ways.
    • direct:tojob
    • direct:toarc

alend cMulticast basic settings

1.5 Consume message from cMulticast

This will let you add two cDirect components to your canvas. The cDirect allows you to produce and consume messages synchronously in different threads with a single CamelContext. cDirect provides direct, synchronous invocation of any consumers when a producer sends a message exchange.

  • Add two cDirect components. You don’t have to connect them to the cMulticast component because the cDirect components will catch your message by defining the basic settings.

Talend cDirect

  • Configure the basic settings of your cDirect components
    • cDirect1: Name = “tojob”
    • cDirect2: Name = “toarc”

This is how your job should look like by now:

Talend route how_to_get_message_from_ActiveMQ job

1.6 Add message to archive queue

There are different ways to store your processed messages, but in this example we are going to add the message to an archive queue (TalendHowTo.archive).

  • Add an cJMS component next to your second cDirect component
  • Connect your cJMS component to your cJMS component (Row – Route)
  • Configure your basic settings

Talend cJMS basic settings

Your job should look like this:

Talend route how_to_get_message_from_ActiveMQ job

2. Build your subjob

This second chapter shows you how to add a subjob to your route because you are going to save your XML message from the “TalendHowTo” queue as a CSV file to your local disc. The easiest way to do this is by adding the cTalendJob component to the route that you have build in the previous chapter.

2.1 Add the cTalendJob component

  • Drag and drop the cTalendJob next to your first cDirect component

Talend cTalendJob

  • Connect the cDirect component to your cTalendJob component (Row – Route)
  • Go to the basic settings tab and press the dots as shown below.

Talend cTalendJob settings

  • You will see the pop-up screen as shown below.

Talend route assign job

  • Check the “Create a new Job and Assign it to this cTalendJob component” option and Talend will create the job for you.

This how the job looks like:

Talend cTalendJob create

This is how the route looks like:

Talend route

2.2 Build your subjob

Talend adds two components to your subjob by default when you are using the “Create a new Job and Assign it to this cTalendJob component” option of your cTalendJob component. In this case we only need the tRouteInput component.

  • Remove the tRouteOutput component because we are going to use the tFileOutputDelimited component to save the data as a csv file.
  • Add the tExtractXMLField component

Talend tExtractXMLField

  • Connect the tRouteInput component to your tExtractXMLField component (Row – Main)
  • Edit the schema

Talend tExtractXMLField schema

  • Configure the basic settings of your tExtractXMLField component as shown below:

Talend tExtractXMLField

  • Add the tFileOutputDelimited component

Talend tFileOutputDelimited

  • Connect the tExtractXMLField component to your tFileOutputDelimited component (Row – Main)
  • Configure the basic settings of your tFileOutputDelimited component (don’t forget to sync the columns)

Talend tFileOutputDelimited

  • Save your job and close it

3. Run your route

Your route and subjob are ready and you have already build a job to add a message to you ActiveMQ queue, so it’s time for testing!

  • Open your route and run it
  • Open your job to add a message to the “TalendHowTo” queue
    • Run your job

Or you can run it step by step!

3.1 Results step by step

  • Open your “how_to_post_message_ActiveMQ ” job and run it

Notice that the message have been queued

Talend ActiveMQ

  • Start your route “how_to_get_message_from_ActiveMQ”

Notice that:

  • your message is enqueued
  • there are no pending messages left
  • the message has been added to the “TalendHowTo_Archive” queue
  • your job has saved your message as a csv file to the location you defined

Talend ActiveMQ route


In this example you have used an integration job to add a message to the queue, but i’m going to add a post with an example of a REST API soon!

Don’t miss it and subscribe to!

One Comment

Leave a Reply