camel-example-spring-boot-rest-jpa
I'm looking at Order.java :
@Consumed
public void setProcessed() {
}
I understand this is required so Camel won't need to re-process an already processed Order. However, how can that be achieved without adding a camel dependency to Order.java pojo?
Reason: In real life we need to achieve the same functionality without adding external dependency to pojo.
Edit 1: As suggested by claus, created a custom delete processor
public class OrderProcessedHandler implements DeleteHandler<Order>{
@Override
public void deleteObject(EntityManager arg0, Object arg1, Exchange arg2) {
((Order) arg1).setProcessed(true);
}
}
And specified that in the endpoint uri:
// A second route polls the DB for new orders and processes them
from("jpa:sb.app.bo.Order"
+ "?consumer.namedQuery=new-orders"
+ "&consumer.delay={{example.processOrderPeriod:25s}}"
+ "&consumeDelete=false"
+ "&deleteHandler=sb.app.camel.routes.OrderProcessedHandler")
.routeId("process-order")
.log("Processed order #id ${body.id} with ${body.amount} ");
Not able to use the Handler, Error log:
2017-10-31 15:45:33.688 ERROR 1429 --- [ restartedMain] o.s.boot.SpringApplication : Application startup failed
org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed to create route process-order: Route(process-order)[[From[jpa:sb.app.bo.Order?consumer.name... because of Failed to resolve endpoint: jpa://sb.app.bo.Order?consumeDelete=false&consumer.delay=25s&consumer.namedQuery=new-orders&deleteHandler=sb.app.camel.routes.OrderProcessedHandler due to: Could not find a suitable setter for property: deleteHandler as there isn't a setter method with same type: java.lang.String nor type conversion possible: No type converter available to convert from type: java.lang.String to the required type: org.apache.camel.component.jpa.DeleteHandler with value sb.app.camel.routes.OrderProcessedHandler
at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1831) ~[camel-core-2.20.0.jar:2.20.0]
at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:136) ~[camel-spring-2.20.0.jar:2.20.0]
at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:174) ~[camel-spring-2.20.0.jar:2.20.0]
Solution (based on @claus advice):
// A second route polls the DB for new orders and processes them
from("jpa:sb.app.bo.Order" //select * from orders where consumed = false
+ "?consumer.namedQuery=query_new_orders"
+ "&consumer.delay={{example.processOrderPeriod:5s}}"
+ "&consumeDelete=false"
+ "&deleteHandler=#orderProcessedMarker")
.routeId("process-order")
.log("Processed order #id ${body.id} with ${body.amount} copies of the «${body.book.description}» book");
DeleteHandler bean:
@Bean(name = "orderProcessedMarker")
public DeleteHandler<Order> createOrderProcessHandler() {
return new DeleteHandler<Order>() {
@Override
public void deleteObject(EntityManager arg0, Object arg1, Exchange arg2) {
((Order) arg1).setProcessed(true);
}
};
}