Io uso EventBus di guava, sfortunatamente esso cattura e registra l'InvocationTargetException che si verifica quando un gestore di eventi lancia una RuntimeException. Posso disabilitare questo comportamento?EventBus guava: non catturare RuntimeException
risposta
Così com'è, questa è una decisione deliberata, e discusso nella documentazione EventBus:
gestori non dovrebbero, in generale, buttare. In tal caso, EventBus rileverà e registrerà l'eccezione. Questa è raramente la soluzione giusta per la gestione degli errori e non dovrebbe essere invocata; è pensato esclusivamente per aiutare a trovare problemi durante lo sviluppo.
soluzioni alternative sono being considered, anche se dubito seriamente che faranno in versione 12.
Ecco il codice per pigri
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);
}
}
}
Ora, è possibile iscriversi ExceptionEvent
.
Ecco il mio ExceptionEvent
solo per copiare & 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;
}
}
Proprio ereditare EventBus guava, e scrivere si possiede EventBus personalizzato. Suggerimenti: questa classe deve scrivere nel pacchetto com.google.common.eventbus, in modo che il metodo interno possa essere sovrascritto.
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);
}
}
È possibile correggere il collegamento? –
Collegato fisso, provalo ora. –