Throw exception from consumer to producer

194 views
Skip to first unread message

Pepa

unread,
Jan 26, 2012, 8:09:16 AM1/26/12
to lmax-di...@googlegroups.com
Hi,
I use disruptor as a background audit logger which logs audit information to the database and i have simple question. Is there some possibilities to throw exception from consumer to producer? Because if there is problem with database connection or something like that I need to stop current user operation.

Thank you for answer
P.

Pepa

unread,
Jan 26, 2012, 10:13:10 AM1/26/12
to lmax-di...@googlegroups.com
I find the way for ecxception handling using disrup.handleExceptionsWith(producer) but i have another problem. When some consumer throws exception, processed entry stay in buffer and after while buffer is full. is there some way to "remove" entry from buffer or mark his position as ready to use?

Michael Barker

unread,
Jan 26, 2012, 2:30:04 PM1/26/12
to lmax-di...@googlegroups.com
There is no way to pass the exception back to the producer. The
default exception handler (FatalExceptionHandler) will log an error
message and throw a RuntimeException. This exception will cause the
currently running thread to exit and the ring buffer will eventually
fill up and block. If you want your EventProcessor to continue when
an exception occurs define your own handler by implementing the
ExceptionHandler interface, but don't rethrow the exception. This
will allow the EventProcessor to continue processing events. The
event that caused the exception will simply be overwritten in the
normal way when the ring buffer wraps.

Mike.

Pepa

unread,
Jan 27, 2012, 5:00:11 AM1/27/12
to lmax-di...@googlegroups.com
Hi Mike,
Thank you for answer. I have code like this. But when i throw exception from EventHandler, default FatalExceptionHandler is used and thread is stoped as you write in answer. Can you tell me please, where is problem? Producer can't be ExceptionHandler or i miss something important?

class Producer implements ExceptionHandler {
   .
   public void createDisruptor{
      disruptor=new Disruptor(...
      Consumer consumer=new Consumer();
      disrup.handleEventsWith(consumer);
      disrup.handleExceptionsWith(this);
   }
   .
  public void handleEventException....{
   rollback transaction;
  }

}

class Consumer implements EventHandler {
   public void onEvent(.... )throws Exception{
      .
      .
      if something wrong throw new MyException.....;
   }
}


Michael Barker

unread,
Jan 27, 2012, 5:37:37 AM1/27/12
to lmax-di...@googlegroups.com
You could implement it that way, but you should be away of the
threading model. The call handleEventException will come from the
thread that is running the consumer not the thread that is running the
producer. So you will need to be careful about data is shared within
the Producer implementation. If your implementation of "rollback
transaction" shares data with the parts of the producer that are
writing to the ring buffer they you'll potentially run into
concurrency issues and contention. I personally think it is cleaner
to have the exception handled by a class that is separate from the
producer, so that you a much cleaner asynchronous eventing model.

Mike.

Reply all
Reply to author
Forward
0 new messages