Difference between #JMSListener and #Consume annotations? - apache-camel

I am trying to consume JMS messages sent via spring JmsTemplate using #Consume annotated bean. The consumer is not receiving messages when sent using JmsTemplate.
Whereas, when sent using ProducerTemplate of Camel the messages are received.
What is the difference between #org.springframework.jms.annotation.JmsListener and #org.apache.camel.Consume?
Producer Logic
jmsTemplate.convertAndSend("jms:mailbox", message);
Consumer Logic
#Consume(uri="jms:mailbox")
public void onRequest(String name) {
System.out.println("Received message > "+name);
}

Apache Camel #Consume annotation can consume from any endpoint, which supports consuming. This annotation takes uri as parameter. URI consists of scheme, path and optional params. In case of JMS component the scheme is jms, path is Destination (in your case mailbox) and params are additional options customizing behavior of Consumer.
Spring #JmsListener can consume from JMS and takes Destination as parameter.
Your code does not work because the Destination is mailbox, not jms:mailbox. Spring JmsTemplate does not know about jms scheme, it is Camel specific. So use jmsTemplate.convertAndSend("mailbox", message) on Spring side and #Consume(uri="jms:mailbox") on Camel side.

Related

camel - quartz2 endpoint - Multiple consumers for the same endpoint

I am using apache camel and wants to have multiple routes.The route is as below.
endpoint -> quartz2://tsTimer?cron=0%2F20+*+8-18+%3F+*+MON%2CTUE%2CWED%2CTHU%2CFRI+*&stateful=true&trigger.timeZone=Asia%2FSingapore
Call bean method to get data.
Send to MQ
In this case my route is going to be same as the polling interval is same.
The data from bean method will be different.
And the MQ queue will be same.
Failed to start route route2 because of Multiple consumers for the same endpoint is not allowed: quartz2://tsTimer?cron=0%2F20+*+8-18+%3F+*+MON%2CTUE%2CWED%2CTHU%2CFRI+*&stateful=true&trigger.timeZone=Asia%2FSingapore
How do i achieve this? How do i differentiate camel route in case when the endpoint is quartz2 timer?
I din't notice that in endpoint uri I was having tsTimer which will distinguishes other end-points.
Something like below
quartz2://tsTimer1
quartz2://tsTimer2
quartz2://tsTimer3

Camel ProducerTemplate versus Spring JMSTemplate

I am using camel to send message to an IBM MQQueue.
The MDB listening to this queue expects messages of the type com.ibm.jms.JMSMapMessage.
When I use camel producerTemplate, an exception is thrown.
I am doing this
producerTemplate.sendBody("wmq:queue",hashMap);
Exception data: java.lang.ClassCastException: com.ibm.jms.JMSMapMessage incompatible with javax.jms.ObjectMessage
So I tried Spring jmsTemplate, and it worked.
jmsTemplate.send(new MessageCreator() {
#Override
public Message createMessage(Session session)
throws JMSException {
return session.createObjectMessage((Serializable) sctHmap);
}
});
Question:
The jms component documentation says
It uses Spring's JMS support for declarative transactions, including
Spring's JmsTemplate for sending and a MessageListenerContainer for
consuming.
I tried with disabling camels auto conversion using mapJmsMessage=false.
I realised it would not help as it would have sent a hash map, I still got the same exception. Is there any way I can get the producerTemplate to work in the same way as JMSTemplate? ProducerTemplate seems to be more elegant, atleast in terms of my unit tests
It seems I misinterpreted the classcast exception message. Camel was correctly sending, com.ibm.jms.JMSMapMessage, the MDB at the consumer application was expecting javax.jms.ObjectMessage.
I resolved it by setting jmsMessageType=object in the endpoint URI. :)

Camel jms request/reply threading model

I am using camel jms in a synchronous way using request-reply pattern.The camel route exposes a cxf endpoint and it places the message in to the jms queue another component process it and sends the response in replyto queue.
A new thread is getting created while receiving the reply from replyto queue?Iam facing issues with using log4j MDC with in the camel route.The values store in MDC are no more available?
Is it the behaviour of camel jms request/reply pattern?
Below is the JMS endpoint:
<to id="QueueEndpoint" pattern="InOut" uri="hornetq:queue:{{esb.api.requestqueue}}?replyTo=queue:{{esb.api.responsequeue}}&useMessageIDAsCorrelationID=true&replyToType=Exclusive&requestTimeout={{esb.api.queue.requesttimeout}}"/>

Camel CXFRS response

camel-fuse 2.8
I have a camel jaxrs server which accepts requests then kicks-off 2 Camel routes.
The first route, consumes requests from cxfrs endpoint/bean and ships them to jms queue inbox.
The second route, consumes requests from jms queue inbox for business logic processing, then ships the results to jms queue outbox.
My question is related to http response and sending the results to jaxrs server consumer.
Is it possible to send an http response back to http client from first route with results from second route? (synchronously)
from("cxfrs:bean:personLookupEndpoint") <-- http client waits for response...
.setExchangePattern(ExchangePattern.InOut)
.process(new RequestProcessor())
.to(inbox);
from(inbox)
.unmarshal(jaxb)
.process(new QueryServiceProcessor())
.to("bean:lookupService?method=processQuery(${body})")
.convertBodyTo(String.class)
.to(outbox); <-- need to send results to font-end consumer synchronously ...
Do you really need to do it using queues? I think that it would be better to use direct: routes instead.
There is a possibility to use the InOut exchange pattern for a JMS endpoint, but it has some limitations: http://fusesource.com/docs/router/2.2/transactions/JMS-Synchronous.html

How to connect an Apache Camel synchronous request/reply endpoint with asynchronous sub-routes

I'd like to route a webservice request to an InOnly endpoint of a jms queue. Then route a response jms message received from a separate InOnly endpoint back to the webservice client as the response. The webservice request/response is a synchronous InOut pattern, and the sub-routes are asynchronous. What options do I have to achieve this with Camel?
The Camel route here is used to explain my question:
String uri={webserice uri}
from(uri)
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
ServcieRequest req =
exchange.getIn().getBody(ServcieRequest.class);
// One option to me is to spawn another route here to route to jms queue...
ProducerTemplate template = exchange.getContext().createProducerTemplate();
template.sendBodyAndHeaders("jms:queue:INQueue", req.getPayload(), headers);
// then need to wait ...until received jms response from the route below
}});
from("jms:queue:OUTQueue")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
// received jms response message
// need to update the exchange data in the above route based on jms message
// so the final response to the webservice cilent can receive the data ...
}});
I think you should rely on the request reply mechanism in Camel for this task.
Camel Doc, Exclusive Fixed Reply Queue
So I guess the following DSL route does pretty much what you want (if there is no other reason why you should use a InOnly pattern for the JMS part?). Make sure you tune the requestTimeout if you need (as it defaults to 20Sec timeout).
from(webserviceURI)
.inOut().to("jms:queue:INQueue?replyTo=OUTQueue?replyToType=Exclusive");
Yes, one other thing. If you run this on several nodes, then you need one exclusive queue per node.

Resources