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

  • ESB
16 REPLIES
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
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
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
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.
Five 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
Five 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