Skip to content

Instantly share code, notes, and snippets.

@mgyongyosi
Last active September 3, 2017 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgyongyosi/fa71a52da65a9dd3cddd537fb4feca66 to your computer and use it in GitHub Desktop.
Save mgyongyosi/fa71a52da65a9dd3cddd537fb4feca66 to your computer and use it in GitHub Desktop.
camel-rabbitmq requeue example route
package com.mgyongyosi.rabbitmq.test.routes;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.rabbitmq.RabbitMQConstants;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQRequeueRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
// @formatter:off
from("rabbitmq://localhost:5672/first?queue=test&concurrentConsumers=10&username=user&password=bitnami&prefetchSize=1&autoAck=false&autoDelete=false")
.onException(NotReadyException.class)
.log("Error for ${body}! Requeue")
.asyncDelayedRedelivery().redeliveryDelay(5000) // wait 5 secs to redeliver and requeue if the redeliver fails
.maximumRedeliveries(1)
.setHeader(RabbitMQConstants.REQUEUE, constant(true))
.handled(true)
.setFaultBody(constant(true))
.end()
.convertBodyTo(Integer.class)
.log("Received: ${body}")
.process((e) -> {
int num = e.getIn().getBody(Integer.class);
// Throw exception if the number is even
if(num % 2 == 0) {
throw new NotReadyException(); // create a new Exception and throw it if the status is not ready
}
})
.log("Not even number processed successfully: ${body}");
// @formatter:on
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment