3

I faced the following problem with JPA but it's maybe more like a conceptional question about Camel.

I need a cron based Quartz consumer. But if it's triggered, I'd like to make a selection as a 1st step with JPA component.

<from uri="quartz://myQuartz?cron=myCronExpression/>
<to uri="jpa://home.myEntity?consumer.query=select o from home.myEntity o"/>

But if I call the JPA component with "to", then it's used as a Producer, and not as a Consumer. Can I use somehow the JPA component to handle this, or I have to follow the Service Activator (bean-based) logic and leave the JPA component behind?

Thanks in advance, Gergely

Charles
  • 50,943
  • 13
  • 104
  • 142
Gergely Kovács
  • 599
  • 1
  • 5
  • 15
  • I cannot follow this. Opposite to what you write - in your example the Quartz endpoint is the producer; and the JPA endpoint is the consumer. Right? – tbsalling Jun 07 '13 at 12:34
  • Well it's missleading... Quartz produces an Exchange, yes, but - as far as I know - in the Camel terminology 'from' endpoints are referred as Consumers and 'to' Endpoints are as Producers. So Consumers polls/waits for messages, while Producers sends them. Here, - if I'm right - the JPA component thinks that I want to persist my Exchange body. I want to tell him somehow, to 'please run the query and return with the result'. – Gergely Kovács Jun 07 '13 at 12:58
  • You are right ;-). But the terminology can be somewhat confusing when reading http://www.manning.com/ibsen/Camel_ch01_update.pdf. But back to your question: Can you explain in other words what you are trying to achieve. You need to run a JPA query at certain intervals? And then send the result of the query to another endpoint? I am just guessing now - but it sounds like you should look at http://camel.apache.org/aggregator2.html ? – tbsalling Jun 09 '13 at 07:21

2 Answers2

4

This is pretty much the Content-Enrichement pattern. You can use the

<pollEnrich uri="jpa://home.myEntity?consumer.query=select o from home.myEntity o"/>

instead to use a consumer mid-route. Keep in mind that you cannot use runtime data from the route (headers or the like) but need to keep the route URI static in this case. Seems your URI is static so that should be no issue.

Petter Nordlander
  • 22,053
  • 5
  • 50
  • 84
  • I had similar requirement to the one in question some time back and I used processors in between to do the work. Never knew then that the content enricher can be used for this.. Good solution! – U2one Jun 10 '13 at 05:45
  • Thank you very much. I felt that there're a pattern for it. I found the enricher pattern but somehow I overlooked this pollEnrich ability. :) Thank you again! – Gergely Kovács Jun 10 '13 at 08:46
1

Very good point Petter. I had a similar issue. I wanted to create a simple route that when called will retrieve data from the database. The solutions is simple.

from("direct:test")
.pollEnrich("jpa://" + User.class.getName() + "?consumer.query=select u from test.User u&consumeDelete=false")

Also check this Camel - content enricher: enrich() vs pollEnrich().

Community
  • 1
  • 1
agelbess
  • 4,249
  • 3
  • 20
  • 21