How to consume from AMQP queue in browsing mode with Apache Camel? - apache-camel

Is there something like "browse" option (see section 2.4.3.3. browse for details) for amqp endpoint?

There is a "browse" component. to("browse:dummy"), but that will only make it possible to browse through messages that has passed this route. Handy in some cases, but not really as the JMS browse option.
What you can do is to use a BrowsableEndpoint to do a JMS browse (should work with AMQP as well, as it's based on a JMS client, haven't tried though).
You can't really receive a message and not delete it though, so you would need something else to trigger your browsing. Such as a timer or a trigger queue.
from("amqp:queue:trigger")
.process(new Processor(){
#Override
public void process(Exchange arg0) throws Exception {
BrowsableEndpoint browse = arg0.getContext().getEndpoint("amqp:queue:archive", BrowsableEndpoint.class);
List<Exchange> exchanges = browse.getExchanges();
System.out.println("Browsing queue: "+ browse.getEndpointUri() + " size: " + exchanges.size());
for (Exchange exchange : exchanges) {
String payload = exchange.getIn().getBody(String.class);
String msgId = exchange.getIn().getHeader("JMSMessageID", String.class);
System.out.println(msgId + "=" +payload);
}
}
});
You could do a pre-route to achive this trigger.
from("amqp:queue:processQueue")
.to("amqp:queue:archive")
.transform().constant("trigger msg")
.to("amqp:queue:trigger");

I figured out it eventually, I just thought that the option should be there on camel component options level, i.e. after connection string in form of e.g. "?mode=browse", but the option is actually to be set on amqp connection level - "my-queue; {mode: browse}". You can set camel component options adding "?option=value".
P.S. Setting the option on amqp connection level works for even-driven (default camel) routing, it does not work however for polling-driven one, see https://issues.apache.org/jira/browse/CAMEL-6784 for details.

Yes. In AMQP 0-10 there is an acquire mode option on message.subscribe which if set tonot-acquired results in a browsing subscription.In AMQP 1.0 the same is achieved by specifying a distribution mode of 'copy' for the source when establishing a subscriber link.
However, assuming you are accessing AMQP via a JMS client then as Petter says above, you should be able to use the browsing facility as defined by JMS I would imagine (I'm not familiar enough with camel to know how that is done).

Related

JMSReplyTo - How to create a generic camel route for VM/Artemis/IBM MQ

I have the following service.
Spring boot 2.5.13
Camel 3.18.0
JMS
I want to use an embedded ActiveMQ Artemis, standalone ActiveMQ Artemis, and IBM MQ.
I've managed to get all 3 running and connecting, but one thing I cant figure out is the JMSReplyTo option.
Running locally with embedded broker:
This runs fine. I can write a message to the queue and a response is send to the JMSReplyTo:
public void sendRequest(){
ActiveMQQueue activeMQQueue = new ActiveMQQueue("RESPONSE_QUEUE");
jmsTemplate.convertAndSend("REQUEST_QUEUE", "Hello", pp -> {
pp.setJMSReplyTo(activeMQQueue);
return pp;
});
}
Via ActiveMQ Artemis console:
This is where the inconstancy comes as the Object received is an ActiveMQDestination which makes setting the CamelJmsDestination much more involved.
Am I wasting my time here? Should I just grab the queue name and construct the uri manually? Or I am missing some logic as to how this works? Or maybe I'm not using the Artemis console in the correct way?
.setExchangePattern(ExchangePattern.InOut)
.setHeader("CamelJmsDestination", header("JMSReplyTo"))
When using javax.jms.Message#setJMSReplyTo(Destination) you have to pass a javax.jms.Destination which must implement one of the following:
javax.jms.Queue
javax.jms.TemporaryQueue
javax.jms.Topic
javax.jms.TemporaryTopic
In order to reproduce this semantic via text in the web console of ActiveMQ Artemis you need to prefix your destination's name with one of the following respectively:
queue://
temp-queue://
topic://
temp-topic://
So when you set the JMSReplyTo header try using queue://RESPONSE_QUEUE.
When your application then receives this message and invokes getJMSReplyTo() it will receive a javax.jms.Queue implementation (i.e. ActiveMQQueue) and then you can use getQueueName() to get the String name of the queue if necessary.

How to manually ack/nack a PubSub message in Camel Route

I am setting up a Camel Route with ackMode=NONE meaning acknowlegements are not done automatically. How do I explicitly acknowledge the message in the route?
In my Camel Route definition I've set ackMode to NONE. According to the documentation, I should be able to manually acknowledge the message downstream:
https://github.com/apache/camel/blob/master/components/camel-google-pubsub/src/main/docs/google-pubsub-component.adoc
"AUTO = exchange gets ack’ed/nack’ed on completion. NONE = downstream process has to ack/nack explicitly"
However I cannot figure out how to send the ack.
from("google-pubsub:<project>:<subscription>?concurrentConsumers=1&maxMessagesPerPoll=1&ackMode=NONE")
.bean("processingBean");
My PubSub subscription has an acknowledgement deadline of 10 seconds and so my message keeps getting re-sent every 10 seconds due to ackMode=NONE. This is as expected. However I cannot find a way to manually acknowledge the message once processing is complete and stop the re-deliveries.
I was able to dig through the Camel components and figure out how it is done. First I created a GooglePubSubConnectionFactory bean:
#Bean
public GooglePubsubConnectionFactory googlePubsubConnectionFactory() {
GooglePubsubConnectionFactory connectionFactory = new GooglePubsubConnectionFactory();
connectionFactory.setCredentialsFileLocation(pubsubKey);
return connectionFactory;
}
Then I was able to reference the ack id of the message from the header:
#Header(GooglePubsubConstants.ACK_ID) String ackId
Then I used the following code to acknowledge the message:
List<String > ackIdList = new ArrayList<>();
ackIdList.add(ackId);
AcknowledgeRequest ackRequest = new AcknowledgeRequest().setAckIds(ackIdList);
Pubsub pubsub = googlePubsubConnectionFactory.getDefaultClient();
pubsub.projects().subscriptions().acknowledge("projects/<my project>/subscriptions/<my subscription>", ackRequest).execute();
I think it is best if you look how the Camel component does it with ackMode=AUTO. Have a look at this class (method acknowledge)
But why do you want to do this extra work? Camel is your fried to simplify integration by abstracting away low level code.
So when you use ackMode=AUTO Camel automatically commits your successfully processed messages (when the message has successfully passed the whole route) and rolls back your not processable messages.

Resume Activiti task from Camel ActiveMQ route

I'm trying to send a message from an Activiti Camel task to an ActiveMQ queue, which should resume the activity when it is received by Camel. As I understand it, when the message is received from the queue lacks the properties that would enable it to be identified by Camel in order to be routed to the correct activity task.
As such a Business key is Null Exception is raised and the route fails.
from("activiti:SampleProcess:myCamelTask")
.to("activemq:queue:myQueue");
As expected, if I hardcode either the PROCESS_ID_PROPERTY or the PROCESS_KEY_PROPERTY in the receiving route, the message is routed correctly (when the ID matches).
from("activemq:queue:myQueue")
.setBody(constant("test body"))
.setProperty(PROCESS_ID_PROPERTY, constant("50"))
// .setProperty(PROCESS_KEY_PROPERTY, constant("CUSTOM-KEY"))
.to("activiti:SampleProcess:receiveAsyncPing");
How can I get either property in the receiving route so I can set them accordingly?
Or is there a more recommended way to approach this?
A good question.
The way I handled this is to inject the PROCESS_KEY within the route using the setProperty() command:
See below where I set the process key (business key) to "bar":
from(startProcessEndpoint)
.log(LoggingLevel.INFO, logMsg3)
.setProperty("PROCESS_KEY_PROPERTY",foo)
.setBody(constant("bar"))
.to("activiti:testCamelTask:receive")
Now, if you dont want to use a constant, then you have access to the exchange object within the route and can use an Expression as shown below:
Expression foo = new Expression() {
#Override
public <T> T evaluate(Exchange exchange, Class<T> aClass) {
return (T) "foo";
}
};
Hope this helps,
Greg

Avoid automatic binding with RabbitMQ and Camel

I'm trying to use RabbitMQ with Camel. I am using Camel 2.14.1.
I want to open an fanout exchange on RabbitMQ and then later bind queues to it. This seems to work fine. However, everytime I create an Exchange, it is automatically bound to queue with a system name (a number). Can't I avoid that?
Here is a simple example which posts 100 messages to an Exchange. But they get delivered to an automatically created queue, I want to avoid this.
#Override
public void configure() throws Exception
{
final String testGUID = "xxxx";
from("timer://publish?repeatCount=100&period=10&fixedRate=true").process(new Processor()
//from("timer://publish?repeatCount=100&period=1&fixedRate=true").process(new Processor()
{
#Override
public void process(Exchange _exchange) throws Exception
{
String message = String.valueOf(_exchange.getProperty(Exchange.TIMER_COUNTER));
_exchange.getOut().setBody(message+testGUID);
}
})
.to("rabbitmq://localhost/exchange=logs1237?autoDelete=false&username=guest&password=guest&exchangeType=fanout");
}
Best regards,
Morten Knudsen
UPDATE:
It seems from looking at the source, that the triggering of the automatic queue happens if "queue" in RabbitMQEndPoint is not null. But "queue" is automatically assigned to "String.valueOf(UUID.randomUUID().toString().hashCode());" at construction.
If you don't want to bind the exchange with queue, you can setup the declare option to be false. BTW, declare option is new added since Camel 2.14.0.
As Bal has already described here add "declare=false" to your RabbitMQ URI. This should solve your problem.
Optionally, you can also use "skipQueueDeclare=true&skipQueueBind=true" this properties in your URI as well.
declare: If the option is true, camel declare the exchange and queue name and bind them together. If the option is false, camel won’t declare the exchange and queue name on the server.
skipQueueDeclare: If true the producer will not declare and bind a queue. This can be used for directing messages via an existing routing key.
skipQueueBind: If true the queue will not be bound to the exchange after declaring it
You can reach out all the properties you can use in Camel for RabbitMQ here.
From Camel 2.16.1 on, there's a new option for the rabbitmq component, skipQueueDeclare, which properly solves this issue.

Does Apache Camel supports activemq wildcard consumers?

I need a way to consume messages from multiple activemq jms queues.
As per activemq documentation, it supports wildcard consumers
I am using camel as a messaging bus.Is it possible to look at below named queues
aaa.processQueue
bbb.processQueue
ccc.processQueue
By configuring camel route to look at activemq:*.processQueue endpoint?
Also let me know, if there is more cleaner alternative for this.
Yes. It should be doable as Camel is using the OpenWire/JMS client.
Your options are:
from("activemq:*.processQueue")
from("activemq:aaa.processQueue,bbb.processQueue,ccc.processQueue")
Multiple routes with a sub route for logic:
from("activemq:aaa.processQueue").to("direct:doProcess");
from("activemq:bbb.processQueue").to("direct:doProcess");
from("activemq:ccc.processQueue").to("direct:doProcess");
from("direct:doProcess").whatever..
This way, you can easily turn on/off routes as well as assigning more consumers to one, given you need to have more priority on aaa.processQueue messages than the rest.
They have an example on their github of a routebuilder using wildcards:
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
// use wildcard to consume from all sports
from("activemq:queue:sport.>")
.to("log:received?showHeaders=true")
.choice()
// the JMSDestination contains from which queue the message was consumed from
.when(header("JMSDestination").isEqualTo("queue://sport.pl.chelsea"))
.to("mock:chelsea")
// we can use a reg exp to match any message from 1st division
.when(header("JMSDestination").regex("queue://sport.1st.*"))
.to("mock:1st")
.otherwise()
.to("mock:other")
.end();
}
};
}
Ref: https://github.com/apache/camel/blob/master/components/camel-jms/src/test/java/org/apache/camel/component/jms/activemq/ActiveMQConsumeWildcardQueuesTest.java

Resources