简体   繁体   中英

When source and replyTo is the same, stop Camel from consuming after route stopped

Our system has configured to consume and send reply to the same queue, ie, JMSDestination and JMSReplyTo are the same. I cannot change that right now.

In my integration test, if I set replyToSameDestinationAllowed=true , Camel continues to consume the reply I sent to the queue, ie, it "captures" the source and never stop and enters a loop.

But, if I don't set it, Camel refuses to send the reply to the queue, saying this:

JMSDestination and JMSReplyTo is the same, will skip sending a reply message to itself

That causes problem for my integration test. I want to consume the message in a separate method and assert against it.

How can I stop Camel from capturing this queue, ie, consuming only once and ignore the rest?

At the end of my route I call stop() to send reply automatically.

When receiving the second message(the reply), I see this line:

2023-01-10 14:37:22,186 DEBUG [org.apa.cam.com.jms.EndpointMessageListener]-{Camel (camel-1) thread #19 - JmsConsumer[my.queue]}-Received Message has JMSCorrelationID [ID:hostname-1673354133272-4:1:1:10:1]

Can I use this to ignore the reply? Should I stop the route? Rollback? Or what should I do?

At last I filtered out messages based on the presence of JMSCorrelationID header.

from("activemq:xxx")
    .filter(simple("${header.JMSCorrelationID} == null")) // ignore reply
    .to("direct:main");

Even that I don't set it in my client side code, seems that Camel will use message id to set JMSCorrelationID when sending reply if the incoming message hasn't it. If incoming message already has JMSCorrelationID, Camel will not change it, and will copy that value to the reply.(I guess that if you manually set JMSCorrelationID in client side, Camel will stop setting it for you).

So basically, message without JMSCorrelationID means it's new message which hasn't passed through my client application. I think only client side should set it, especially in my case where original message and replies are put into the same queue, where client needs a mean to filter out replies.

Also, I find that receiving can specify a message collector stating the field you want to filter. For example:

    QueueReceiver receiver = jmsSession.createReceiver(myQueue, "JMSCorrelationID='" + correlationId + "'");

This is useful when you know the correlationId. But in my case ( @QuarkusIntegrationTest which is a black box test), this cannot be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM