dynamically define cJMS destination queue

One Star

dynamically define cJMS destination queue

Hi
Is it possible to define the destination queue of the cJMS component dynamically?
I tried to define the destination by a context variable but if I change the value of the context variable within the route, the new value is not known in the cJMS component.
Any ideas?
thanks, laci

Moderator

Re: dynamically define cJMS destination queue

Hi,
You can use context value in cJMS destination queue.
I tried to define the destination by a context variable but if I change the value of the context variable within the route, the new value is not known in the cJMS component.

Please give us the screenshots of your current contexts setting so that we can address your issue.
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
One Star

Re: dynamically define cJMS destination queue

Hi Sabrina
Yes, I know that I can use a context Variable. What I tried is to set the value of a context variable in a cProcessor Component
context.put("destinationEvita", "new.queue");
the cProcessor component leads to the cJMS component which uses context.destinationEvita as Destination.
But when you look at the source code, you will see that the Destination which the cJMS component will use is set in a initialization phase when the route starts. So setting the value of that context variable at runtime has no effect.
Any other way to set the Destination field of the cJMS component dynamically?
Thanks,
Laci
Moderator

Re: dynamically define cJMS destination queue

Hi,
Why don't you set the context value in "Contexts" view? It seems what you put value to a global variable?
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
One Star

Re: dynamically define cJMS destination queue

Hi Sabrina
sorry, you don't seem to understand what I mean.
I want to set the queue dynamically in the cJMS component. That means, that I want to be able to change the queue during the process of my route.
For example: Say, I have a header variable QueueName="queue1"
then I want to send my message to queue1.
If the value of my header variable is QueueName="queue2"
then I want to send my message to queue2
That's why I tried to change the value of the context variable.

How can I upload a screenshot here? I can't find a button for that.
Best, Laci
Moderator

Re: dynamically define cJMS destination queue

Hi,
You should register and log in as a Community member first, then you'll get a Image upload box that allows to upload screen captures and images up to 200KB.(20 images per post, each image must be less then 1024x768 pixels and 200 KB)
For registered users, please clicking on "post reply" (not through the quick post").
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Employee

Re: dynamically define cJMS destination queue

If you are familiar with Camel, you can use Camel Properties.
http://camel.apache.org/properties.html
One Star

Re: dynamically define cJMS destination queue

Thanks camsellem, that helped a bit further.
In a cProcessor component I have the following code:
org.apache.camel.component.properties.PropertiesComponent pc = new org.apache.camel.component.properties.PropertiesComponent();
pc.setLocation("file:/home/talend/msp.properties");
org.apache.camel.CamelContext camelContext = getCamelContextMap().get("TestCntx-ctx");
camelContext.addComponent("properties", pc);
(TestCntx-ctx is the name of my route)
the cProcessor component leads to a cMessagingEndpoint with the URI:
"{{destination.queue}}"
which is defined in the file /home/talend/msp.properties as follows:
destination.queue=myQueue
When I run it in Talend Studio I get the error:
Failed to create route cJMS_1 at: >>> To <<< in route: Route] -> [pr... because of PropertiesComponent with name properties must be defined in CamelContext to support property placeholders.

I suspect that it doesn't work because the PropertiesComponent object is not registered correctly. But how do I do it?
Thanks for your help,
Laci
One Star

Re: dynamically define cJMS destination queue

ok, I found the solution.
I have to define the PropertiesComponent in a cConfig component like this:
org.apache.camel.component.properties.PropertiesComponent pc = new org.apache.camel.component.properties.PropertiesComponent();
pc.setLocation("file:/home/talend/msp.properties");
camelContext.addComponent("properties", pc);

And then I can use a cJMS component with destination:
"{{destination.queue}}"
where
destination.queue=queue.name
is defined in /home/talend/msp.properties
One Star

Re: dynamically define cJMS destination queue

... but how can I change the value of a Camel Property at runtime?
Employee

Re: dynamically define cJMS destination queue

Hi Laci,
You can manipulate what queue cJMS will use at runtime by changing a header on the Camel Exchange.
Please see:
http://camel.apache.org/jms.html
Heading: Reuse endpoint and send to different destinations computed at runtime
You can use the cSetHeader component (or your custom processor) to set the CamelJmsDestinationName header to the desired queue name.
Hope that helps!
-Ben
One Star

Re: dynamically define cJMS destination queue

Hi Ben
Thanks a lot. A simple and working way to do it!
Best,
Laci
One Star

Re: dynamically define cJMS destination queue

hi ,
i want to set Content-Type value dynamically from database in cHttp ?
Can anyone tell me how to do this?
Employee

Re: dynamically define cJMS destination queue

Hi Suresh,
Please post your question as a new thread.  When you do, try and be more descriptive about your use case.
Employee

Re: dynamically define cJMS destination queue

Hi Suresh,
This example uses XPath to pull a value from an XML message and use it as the queue name in the following cJMS component.  The appearance is a little different from 5.x because it's 6.0.
Hope this helps.
Six Stars

Re: dynamically define cJMS destination queue

Hi guys
I had similar question recently in a Message Dispatcher need.  
Here was my approach
I choosed a combination of 

cProcessor (or anything else) that sets a Header variable
"MYQUEUE"

(depending of my logic)
cRecipientList with a Simple expression 
"cMQConnectionFactory1:queue:"+"${in.header.MYQUEUE}"


The trick is the URI that uses the JMS Connection Factory
Vincent
Eight Stars

Re: dynamically define cJMS destination queue

Yes, just have to change the "CamelJmsDestinationName" header and that's all

 

One thing to understand with contexts is they behave completly differet in routes/camel than in jobs. They are read at route startup but if you change their values they never will be taken in account. So they are only constant values. Headers and properties are the main stuff for dynamic variables.

 

Loko

Highlighted
Six Stars

Re: dynamically define cJMS destination queue

Hey Loko,

 

that's not exactly true! Have a look here to check out how to change your contexts with the help of external config files - it's pretty useful!

Thanks for the tip about the JMS Header!

 

David.

Eight Stars

Re: dynamically define cJMS destination queue

Hi David.
I know that .cfg file option, but what I said stays 100% true ! Once the value has been initialized, in the route itself or overriden by the cfgFile, it will never change. You could manually change its value in a cProcessor for instance, but the change would be lost for the nexr steps of your route.
Six Stars

Re: dynamically define cJMS destination queue

@Loko,

 

maybe I'm not understanding you correctly...

 

Here's an example of what I mean:

1.pngSimple REST Service set-up

2.pngContents of cSetBody with context variable usage

3.pngContext variable initialisation

4.pngGET REST call with browser

5.pngGetting ID

6.pngUpdating config for the already deployed service

7.pngSecond GET REST call to service after update

Can you show me an example of what you mean? I'm still quite new to Routes in Talend ESB and still trying to get my head around them, so it's quite possible that I'm missing something. In any case, if you have anything to teach me about Routes, I'll gladly take the advice!

 

Thanks,

 

David.

Eight Stars

Re: dynamically define cJMS destination queue

Yes, if you refresh your route after the modification of your config file, the new value will be used.

 

I mean , the routes usually have many components. For the same message, (that is, not 2 different calls but the sam call , in your case) once the value has been initiated in a component it cannot be changed during the rest of the route process.

Calling Talend Open Studio Users

The first 100 community members completing the Open Studio survey win a $10 gift voucher.

Start the survey

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Talend API Designer – Technical Overview

Take a look at this technical overview video of Talend API Designer

Watch Now

Getting Started with APIs

Find out how to get started with APIs

Read