You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Tom Howe <to...@artcore.com> on 2011/12/01 10:10:58 UTC
Re: Transactions with PollingConsumerTemplate/producerTemplate
Hi Claus,
Thanks for your response - I've never done this before, would you be
able to point me to some example code?
Thanks, Tom
On Wed, Nov 30, 2011 at 7:53 AM, Claus Ibsen <cl...@gmail.com> wrote:
> On Wed, Nov 30, 2011 at 5:06 PM, Tom Howe <to...@artcore.com> wrote:
>> Hi,
>>
>> If I want to consumer from a queue using PollingConsumerTemplate and
>> move the messages to another queue, can I wrap this in a transaction
>> given that there is no actual camel route?
>>
>> I'm using pollingConsumerTemplate as I want to control when/if I
>> consume, but maybe there is an alternative way.
>>
>
> You would need to wrap the code in a transaction yourself. And control
> the begin / commit.
>
>
>
>
>> Code looks a little like this so far..
>>
>> int max = 10;
>>
>> while (true) {
>> log.info("consuming..");
>> int inprogressQty =
>> inprogressEndpoint.getExchanges().size();
>>
>> if (inprogressQty < max) {
>> log.info("There are {} items inprogress",
>> inprogressQty);
>> Exchange exchange = consumer.receive(1000);
>>
>> if (exchange != null) {
>> Object o = exchange.getIn().getBody();
>> log.info("Got message: {}", o);
>>
>> producerTemplate.send(inprogressEndpoint, exchange);
>>
>> } else {
>>
>> log.info("No message");
>> }
>> } else {
>> try {
>> Thread.sleep(1000);
>> } catch (InterruptedException e) {
>> e.printStackTrace(); //To change body of
>> catch statement use File | Settings | File Templates.
>> }
>> }
>>
>> }
>>
>>
>>
>> Thanks, Tom
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>
Re: Transactions with PollingConsumerTemplate/producerTemplate
Posted by Tom Howe <to...@artcore.com>.
Thanks Claus,
I'll take a look at your link and give it a go.
Cheers, Tom
On Thu, Dec 1, 2011 at 7:03 AM, Claus Ibsen <cl...@gmail.com> wrote:
> On Thu, Dec 1, 2011 at 10:10 AM, Tom Howe <to...@artcore.com> wrote:
>> Hi Claus,
>> Thanks for your response - I've never done this before, would you be
>> able to point me to some example code?
>>
>
> Camel uses the Spring Transaction API. So I would suggest to take a
> look at that. And how to programmatically being/commit/rollback
> transactions.
>
> For example Spring have a TransactionTemplate API you can use, which
> in fact will automatic begin / commit / rollback for you.
>
> In fact we do this in the TransactionErrorHandler in Camel. See
> https://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/TransactionErrorHandler.java
>
>
>> Thanks, Tom
>>
>> On Wed, Nov 30, 2011 at 7:53 AM, Claus Ibsen <cl...@gmail.com> wrote:
>>> On Wed, Nov 30, 2011 at 5:06 PM, Tom Howe <to...@artcore.com> wrote:
>>>> Hi,
>>>>
>>>> If I want to consumer from a queue using PollingConsumerTemplate and
>>>> move the messages to another queue, can I wrap this in a transaction
>>>> given that there is no actual camel route?
>>>>
>>>> I'm using pollingConsumerTemplate as I want to control when/if I
>>>> consume, but maybe there is an alternative way.
>>>>
>>>
>>> You would need to wrap the code in a transaction yourself. And control
>>> the begin / commit.
>>>
>>>
>>>
>>>
>>>> Code looks a little like this so far..
>>>>
>>>> int max = 10;
>>>>
>>>> while (true) {
>>>> log.info("consuming..");
>>>> int inprogressQty =
>>>> inprogressEndpoint.getExchanges().size();
>>>>
>>>> if (inprogressQty < max) {
>>>> log.info("There are {} items inprogress",
>>>> inprogressQty);
>>>> Exchange exchange = consumer.receive(1000);
>>>>
>>>> if (exchange != null) {
>>>> Object o = exchange.getIn().getBody();
>>>> log.info("Got message: {}", o);
>>>>
>>>> producerTemplate.send(inprogressEndpoint, exchange);
>>>>
>>>> } else {
>>>>
>>>> log.info("No message");
>>>> }
>>>> } else {
>>>> try {
>>>> Thread.sleep(1000);
>>>> } catch (InterruptedException e) {
>>>> e.printStackTrace(); //To change body of
>>>> catch statement use File | Settings | File Templates.
>>>> }
>>>> }
>>>>
>>>> }
>>>>
>>>>
>>>>
>>>> Thanks, Tom
>>>
>>>
>>>
>>> --
>>> Claus Ibsen
>>> -----------------
>>> FuseSource
>>> Email: cibsen@fusesource.com
>>> Web: http://fusesource.com
>>> Twitter: davsclaus, fusenews
>>> Blog: http://davsclaus.blogspot.com/
>>> Author of Camel in Action: http://www.manning.com/ibsen/
>>>
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>
Re: Transactions with PollingConsumerTemplate/producerTemplate
Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Dec 1, 2011 at 10:10 AM, Tom Howe <to...@artcore.com> wrote:
> Hi Claus,
> Thanks for your response - I've never done this before, would you be
> able to point me to some example code?
>
Camel uses the Spring Transaction API. So I would suggest to take a
look at that. And how to programmatically being/commit/rollback
transactions.
For example Spring have a TransactionTemplate API you can use, which
in fact will automatic begin / commit / rollback for you.
In fact we do this in the TransactionErrorHandler in Camel. See
https://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/TransactionErrorHandler.java
> Thanks, Tom
>
> On Wed, Nov 30, 2011 at 7:53 AM, Claus Ibsen <cl...@gmail.com> wrote:
>> On Wed, Nov 30, 2011 at 5:06 PM, Tom Howe <to...@artcore.com> wrote:
>>> Hi,
>>>
>>> If I want to consumer from a queue using PollingConsumerTemplate and
>>> move the messages to another queue, can I wrap this in a transaction
>>> given that there is no actual camel route?
>>>
>>> I'm using pollingConsumerTemplate as I want to control when/if I
>>> consume, but maybe there is an alternative way.
>>>
>>
>> You would need to wrap the code in a transaction yourself. And control
>> the begin / commit.
>>
>>
>>
>>
>>> Code looks a little like this so far..
>>>
>>> int max = 10;
>>>
>>> while (true) {
>>> log.info("consuming..");
>>> int inprogressQty =
>>> inprogressEndpoint.getExchanges().size();
>>>
>>> if (inprogressQty < max) {
>>> log.info("There are {} items inprogress",
>>> inprogressQty);
>>> Exchange exchange = consumer.receive(1000);
>>>
>>> if (exchange != null) {
>>> Object o = exchange.getIn().getBody();
>>> log.info("Got message: {}", o);
>>>
>>> producerTemplate.send(inprogressEndpoint, exchange);
>>>
>>> } else {
>>>
>>> log.info("No message");
>>> }
>>> } else {
>>> try {
>>> Thread.sleep(1000);
>>> } catch (InterruptedException e) {
>>> e.printStackTrace(); //To change body of
>>> catch statement use File | Settings | File Templates.
>>> }
>>> }
>>>
>>> }
>>>
>>>
>>>
>>> Thanks, Tom
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> FuseSource
>> Email: cibsen@fusesource.com
>> Web: http://fusesource.com
>> Twitter: davsclaus, fusenews
>> Blog: http://davsclaus.blogspot.com/
>> Author of Camel in Action: http://www.manning.com/ibsen/
>>
--
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/