You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by jlpedrosa <jl...@gmail.com> on 2013/11/14 15:42:52 UTC

Rollback/NACK a single message

Hi All,We are coding an application to process events in a queue. We are
using NMS - ActiveMQ 1.6.
In my mental model the best transactionality comes with:
IndividualAcknowledge.
If the proccess goes OK, the message is acknoledge, up to there no problem.
But message has no rollback/nack method. So I don't really know how to
perform that patter. Should I open a session per message? (As I want
multiple threads running at the same time, sequential is not an option).
somthing like:
/IMessage m = messageListener.GetMessage();
ThreadPool.QueueUserWorkItem(new WaitCallback(this.DispatchMessage), m);
/
/public void DispatchMessage(Object messageToDispatch)
        {
            IMessage m = messageToDispatch as IMessage;
            
            try
            {
                //Convert to Event.
                Event ev = UnMarshallMessage(m); --> not relevant code
                //Create an instance of the executor
                EventProccessor executor =
Activator.CreateInstance(ExecutorType) as EventProccessor ;
                //Invoke the executor
                executor.ProcessEvent(ev);
                //commit the message                
                m.Acknowledge();
            }
            catch (Exception ex)
            {   
                log.Error("Error processing the message", ex); 
                m.Rollback() --> does not exist
            }  
}/
Thanks in advance for any suggestion,
Jose Luis




--
View this message in context: http://activemq.2283324.n4.nabble.com/Rollback-NACK-a-single-message-tp4674444.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: Rollback/NACK a single message

Posted by jlpedrosa <jl...@gmail.com>.
Hi Tim

I will do that. Thanks!

Rgds

JL



--
View this message in context: http://activemq.2283324.n4.nabble.com/Rollback-NACK-a-single-message-tp4674444p4674559.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: Rollback/NACK a single message

Posted by Timothy Bish <ta...@gmail.com>.
On 11/15/2013 09:37 AM, jlpedrosa wrote:
> Hi
>
> When I inspected the API,  I thought about it, but it looks like for every
> message I would need to create a session, and a consumer per message, which
> sounds extremely heavy and we expect a significant amount of messages.  (for
> a given consumer I need a session to rollback that message), so this really
> is not an option for us.
>
> Other option is create a pool of consumers tied to it's sessions, and
> allowing only one message per consumer at a time, so i can rollback the
> single message. This adds the limitation of a fixed size number of workers
> (one worker per consumer). Unless a complex dynamic pooling mechanism is
> implemented.
>
> I'll try this second option of a pool.
>
> In my opinion, if the API of a message supports ACK, it is natural to have a
> NACK in the same level.
>
> Thanks for your time Tim!
>
> Jose Luis
>
>
>
>
>
> --
> View this message in context: http://activemq.2283324.n4.nabble.com/Rollback-NACK-a-single-message-tp4674444p4674557.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>
The JMS API doesn't support the idea of a direct NACK or a message. If 
you want to report a message as failed you need to use transactions and 
rollback the TX if processing fails.

-- 
Tim Bish
Sr Software Engineer | RedHat Inc.
tim.bish@redhat.com | www.fusesource.com | www.redhat.com
skype: tabish121 | twitter: @tabish121
blog: http://timbish.blogspot.com/


Re: Rollback/NACK a single message

Posted by jlpedrosa <jl...@gmail.com>.
Hi

When I inspected the API,  I thought about it, but it looks like for every
message I would need to create a session, and a consumer per message, which
sounds extremely heavy and we expect a significant amount of messages.  (for
a given consumer I need a session to rollback that message), so this really
is not an option for us. 

Other option is create a pool of consumers tied to it's sessions, and
allowing only one message per consumer at a time, so i can rollback the
single message. This adds the limitation of a fixed size number of workers 
(one worker per consumer). Unless a complex dynamic pooling mechanism is
implemented. 

I'll try this second option of a pool.

In my opinion, if the API of a message supports ACK, it is natural to have a
NACK in the same level.

Thanks for your time Tim!

Jose Luis





--
View this message in context: http://activemq.2283324.n4.nabble.com/Rollback-NACK-a-single-message-tp4674444p4674557.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: Rollback/NACK a single message

Posted by Timothy Bish <ta...@gmail.com>.
On 11/15/2013 07:36 AM, jlpedrosa wrote:
> Hi All,This also leads to the same problem: the execution of the event must
> be done in the same thread that ActiveMQ uses, otherwise I can't throw the
> exception to the listener.
> When the message arrives, I just put the message on a thread pool. That is
> the approach to implement multi thread processing. So If I want to rise an
> exception to the NMS stack, I can't use multithread..
> IE: [*ActiveMQ Task*] INFO  EventReceiver - Message handler received a
> message in thread: ActiveMQ Task.
> As you can see the listener gets executed in "ActiveMQ Task" thread, in the
> moment I put the message in the thread pool and I raise an exception, my
> code must handle it, I don't have any way to say NMS "Hey this message
> failed!".
> Is there any way I can rollback the message from another thread?
>   Once again, Thanks
> Rgds
> JL
>
>
>
>
> --
> View this message in context: http://activemq.2283324.n4.nabble.com/Rollback-NACK-a-single-message-tp4674444p4674541.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
Why not just use a transacted session and commit or rollback based on 
your processing of the Message?

-- 
Tim Bish
Sr Software Engineer | RedHat Inc.
tim.bish@redhat.com | www.fusesource.com | www.redhat.com
skype: tabish121 | twitter: @tabish121
blog: http://timbish.blogspot.com/


Re: Rollback/NACK a single message

Posted by jlpedrosa <jl...@gmail.com>.
Hi All,This also leads to the same problem: the execution of the event must
be done in the same thread that ActiveMQ uses, otherwise I can't throw the
exception to the listener.
When the message arrives, I just put the message on a thread pool. That is
the approach to implement multi thread processing. So If I want to rise an
exception to the NMS stack, I can't use multithread..
IE: [*ActiveMQ Task*] INFO  EventReceiver - Message handler received a
message in thread: ActiveMQ Task.
As you can see the listener gets executed in "ActiveMQ Task" thread, in the
moment I put the message in the thread pool and I raise an exception, my
code must handle it, I don't have any way to say NMS "Hey this message
failed!".
Is there any way I can rollback the message from another thread?
 Once again, Thanks
Rgds
JL




--
View this message in context: http://activemq.2283324.n4.nabble.com/Rollback-NACK-a-single-message-tp4674444p4674541.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: Rollback/NACK a single message

Posted by jlpedrosa <jl...@gmail.com>.
Hi!
First of all, Thanks for the support.
I am not really sure how to implement what you said:
I am actively listening to messages: 
/consumer.Receive();/ 
So AFAIK I can't throw an exception up to NMS stack:
Are you suggesting that I listen in event mode? IE:
/consumer.Listener+=new MessageListener(consumer_Listener);/ 
then I would throw the exception inside the consumer_listener?
what you said about the poison pill... would be done by the NMS stack or
should I send it manually?
BTW: I think what I am trying to do make sense, I am falling into a pitfall?
I wonder if there is a reason because of nack/rollback is not implemented at
message level.
Rgds
JL




--
View this message in context: http://activemq.2283324.n4.nabble.com/Rollback-NACK-a-single-message-tp4674444p4674533.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: Rollback/NACK a single message

Posted by Timothy Bish <ta...@gmail.com>.
Correct, NMS.ActiveMQ behaves the same as a JMS client so this should work.

On 11/14/2013 11:04 AM, Christian Posta wrote:
> I'm not too familiar with the NMS libs... but for the Java JMS impl,
> you would just throw an exception + combine with the redelivery policy
> (eg, disable redelivery?) and that would send back the "poison pill"
> or "nack"
>
> On Thu, Nov 14, 2013 at 6:42 AM, jlpedrosa <jl...@gmail.com> wrote:
>> Hi All,We are coding an application to process events in a queue. We are
>> using NMS - ActiveMQ 1.6.
>> In my mental model the best transactionality comes with:
>> IndividualAcknowledge.
>> If the proccess goes OK, the message is acknoledge, up to there no problem.
>> But message has no rollback/nack method. So I don't really know how to
>> perform that patter. Should I open a session per message? (As I want
>> multiple threads running at the same time, sequential is not an option).
>> somthing like:
>> /IMessage m = messageListener.GetMessage();
>> ThreadPool.QueueUserWorkItem(new WaitCallback(this.DispatchMessage), m);
>> /
>> /public void DispatchMessage(Object messageToDispatch)
>>          {
>>              IMessage m = messageToDispatch as IMessage;
>>
>>              try
>>              {
>>                  //Convert to Event.
>>                  Event ev = UnMarshallMessage(m); --> not relevant code
>>                  //Create an instance of the executor
>>                  EventProccessor executor =
>> Activator.CreateInstance(ExecutorType) as EventProccessor ;
>>                  //Invoke the executor
>>                  executor.ProcessEvent(ev);
>>                  //commit the message
>>                  m.Acknowledge();
>>              }
>>              catch (Exception ex)
>>              {
>>                  log.Error("Error processing the message", ex);
>>                  m.Rollback() --> does not exist
>>              }
>> }/
>> Thanks in advance for any suggestion,
>> Jose Luis
>>
>>
>>
>>
>> --
>> View this message in context: http://activemq.2283324.n4.nabble.com/Rollback-NACK-a-single-message-tp4674444.html
>> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>
>


-- 
Tim Bish
Sr Software Engineer | RedHat Inc.
tim.bish@redhat.com | www.fusesource.com | www.redhat.com
skype: tabish121 | twitter: @tabish121
blog: http://timbish.blogspot.com/


Re: Rollback/NACK a single message

Posted by Christian Posta <ch...@gmail.com>.
I'm not too familiar with the NMS libs... but for the Java JMS impl,
you would just throw an exception + combine with the redelivery policy
(eg, disable redelivery?) and that would send back the "poison pill"
or "nack"

On Thu, Nov 14, 2013 at 6:42 AM, jlpedrosa <jl...@gmail.com> wrote:
> Hi All,We are coding an application to process events in a queue. We are
> using NMS - ActiveMQ 1.6.
> In my mental model the best transactionality comes with:
> IndividualAcknowledge.
> If the proccess goes OK, the message is acknoledge, up to there no problem.
> But message has no rollback/nack method. So I don't really know how to
> perform that patter. Should I open a session per message? (As I want
> multiple threads running at the same time, sequential is not an option).
> somthing like:
> /IMessage m = messageListener.GetMessage();
> ThreadPool.QueueUserWorkItem(new WaitCallback(this.DispatchMessage), m);
> /
> /public void DispatchMessage(Object messageToDispatch)
>         {
>             IMessage m = messageToDispatch as IMessage;
>
>             try
>             {
>                 //Convert to Event.
>                 Event ev = UnMarshallMessage(m); --> not relevant code
>                 //Create an instance of the executor
>                 EventProccessor executor =
> Activator.CreateInstance(ExecutorType) as EventProccessor ;
>                 //Invoke the executor
>                 executor.ProcessEvent(ev);
>                 //commit the message
>                 m.Acknowledge();
>             }
>             catch (Exception ex)
>             {
>                 log.Error("Error processing the message", ex);
>                 m.Rollback() --> does not exist
>             }
> }/
> Thanks in advance for any suggestion,
> Jose Luis
>
>
>
>
> --
> View this message in context: http://activemq.2283324.n4.nabble.com/Rollback-NACK-a-single-message-tp4674444.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.



-- 
Christian Posta
http://www.christianposta.com/blog
twitter: @christianposta