activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Strachan" <james.strac...@gmail.com>
Subject Re: Consuming a group of messages in a single transaction
Date Thu, 20 Jul 2006 05:28:15 GMT
On 7/19/06, nlif <naaman@dbnet.co.il> wrote:
>
> Hi,
>
> I am using ActiveMQ's MessageGroups feature in order to send and receive
> several related messages as a single unit-of-work. I am using a transacted
> producer, so all messages are sent atomically, but I don't know how to
> implement the consumer.

Its actually the session which is transacted; which then implies the
producers and consumers created on the transacted session take part in
the same transaction.


> The way I see it - the consumer should accumulate
> the messages until it has all of them and then construct the aggregate, save
> it and only then - acknowledge them.
>
> Just to clarfiy, the problem I am trying to solve is that of the consumer
> crashing after acknowleding some, but not all, of the messages in the group.
> This will result in loss of messages.

If you want to consume several messages in a transaction, its
sometimes easiest to just use a consumer, pull all the messages out,
aggregate them and send them using JMS - as you are kinda doing a
custom 'transaction batch' using your own business logic to decide the
batch size.

e.g.

session = connection.createSession(true, 0);
consumer = session.createConsumer(destination);

while (true) {
  m1 = consumer.receive();
  ...
  mn = consumer.receive();

  producer.send(someAggregationMessage);
  session.commit();
}


> So I started-off with a MessageListener configured with Jencks, for pooling.
> The MessageListener should collect all messages in the group, and once it
> has all of them - it should acknowledge all of the together. Now, ensuring
> all messages are indeed delivered to the same MessageListener is taken care
> of by the MessageGroups feature.  But since Jencks commits the transaction
> when the onMessage() method returns - this prevents me from acknowledging
> all messages together.

Yeah. Am wondering if we could add some custom batching plugin into
the Resource Adapter in ActiveMQ maybe - its a little tricky though


> I know some people throw a RuntimeException in the onMessage() method in
> order to make Jencks rollback, but that's awkward to say the least, since it
> will cause messages I already have to be redlivered again and again!

Yeah - if you are doing this kind of batch processing, you either have
to go with the whole batch being redelivered again - or you catch
errors and send bad messages to dead letter queues.


> Furthermore, if I am not mistaken, although all Jencks' threads share the
> same MessageListener instance, each message is (or may be) delivered by a
> different session. This means, that even if I did find a way to commit the
> transaction myself - it would still not achieve the behavior I need.

Yes - Jencks generally load balances messages across sessions; though
the batching feature in the ActiveMQResourceAdapter does AFAIK use the
same session for 1 batch.

-- 

James
-------
http://radio.weblogs.com/0112098/

Mime
View raw message