2012-02-10 22 views

Respuesta

8

Tal como está, esta es una decisión deliberada, y discutido en la documentación EventBus:

Los manipuladores no deben, en general, lanzar. Si lo hacen, EventBus detectará y registrará la excepción. Esta rara vez es la solución correcta para el manejo de errores y no se debe confiar en ella; está destinado únicamente a ayudar a encontrar problemas durante el desarrollo.

soluciones alternativas son being considered, aunque tengo serias dudas de que va a hacer en la liberación 12.

+0

¿Puedes reparar ese enlace? –

+1

Vinculado fijo, pruébalo ahora. –

4

Aquí es código para perezosos

public class Events 
{ 
    public static EventBus createWithExceptionDispatch() 
    { 
     final EventBus bus; 

     MySubscriberExceptionHandler exceptionHandler = new MySubscriberExceptionHandler(); 
     bus = new EventBus(exceptionHandler); 
     exceptionHandler.setBus(bus); 
     return bus; 
    } 

    private static class MySubscriberExceptionHandler implements SubscriberExceptionHandler 
    { 
     @Setter 
     EventBus bus; 

     @Override 
     public void handleException(Throwable exception, SubscriberExceptionContext context) 
     { 
      ExceptionEvent event = new ExceptionEvent(exception, context); 
      bus.post(event); 
     } 
    } 
} 

Ahora, puede suscribirse ExceptionEvent.

Aquí es mi ExceptionEvent para copiar sólo & pasta

@Data 
@Accessors(chain = true) 
public class ExceptionEvent 
{ 
    private final Throwable exception; 
    private final SubscriberExceptionContext context; 
    private final Object extra; 

    public ExceptionEvent(Throwable exception) 
    { 
     this(exception, null); 
    } 

    public ExceptionEvent(Throwable exception, Object extra) 
    { 
     this(exception,null,extra); 
    } 

    public ExceptionEvent(Throwable exception, SubscriberExceptionContext context) 
    { 
     this(exception,context,null); 
    } 

    public ExceptionEvent(Throwable exception, SubscriberExceptionContext context, Object extra) 
    { 
     this.exception = exception; 
     this.context = context; 
     this.extra = extra; 
    } 
} 
0

Sólo heredar EventBus guayaba, y escribir es el propietario EventBus personalizado. Sugerencias: esta clase debe escribir en el paquete com.google.common.eventbus, de modo que se pueda sobrescribir el método interno.

package com.google.common.eventbus; 

import com.google.common.util.concurrent.MoreExecutors; 

public class CustomEventBus extends EventBus { 

    /** 
    * Creates a new EventBus with the given {@code identifier}. 
    * 
    * @param identifier a brief name for this bus, for logging purposes. Should be a valid Java 
    *  identifier. 
    */ 
    public CustomEventBus(String identifier) { 
     super(
      identifier, 
      MoreExecutors.directExecutor(), 
      Dispatcher.perThreadDispatchQueue(), 
      LoggingHandler.INSTANCE); 
    } 

    /** 
    * Creates a new EventBus with the given {@link SubscriberExceptionHandler}. 
    * 
    * @param exceptionHandler Handler for subscriber exceptions. 
    * @since 16.0 
    */ 
    public CustomEventBus(SubscriberExceptionHandler exceptionHandler) { 
     super(
      "default", 
      MoreExecutors.directExecutor(), 
      Dispatcher.perThreadDispatchQueue(), 
      exceptionHandler); 
    } 

    @Override 
    void handleSubscriberException(Throwable e, SubscriberExceptionContext context) { 
     throw new EventHandleException(e); 
    } 
}