पकड़ो मैं अमरूद के इवेंटबस का उपयोग नहीं करता, दुर्भाग्यवश यह InvocationTargetException को पकड़ता है और लॉग करता है जो तब होता है जब कोई ईवेंट-हैंडलर RuntimeException फेंकता है। क्या मैं इस व्यवहार को अक्षम कर सकता हूं?अमरू इवेंटबस: RuntimeException
8
A
उत्तर
8
यह खड़ा के रूप में, यह एक विचार निर्णय है, और EventBus डॉक्स में चर्चा:
हैंडलर नहीं, सामान्य रूप में, फेंक देना चाहिए। यदि वे करते हैं, तो EventBus अपवाद को पकड़ और लॉग करेगा। यह त्रुटि प्रबंधन के लिए शायद ही कभी सही समाधान है और इस पर भरोसा नहीं किया जाना चाहिए; यह पूरी तरह से विकास के दौरान समस्याओं को खोजने में मदद करने के लिए है।
वैकल्पिक समाधान being considered हैं, हालांकि मैं गंभीरता से संदेह है कि वे इसे रिलीज 12.
4
में बनाती हूँ यहाँ आलसी
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);
}
}
}
के लिए कोड अब है, तो आप ExceptionEvent
सदस्यता ले सकते हैं।
यहाँ मेरी ExceptionEvent
सिर्फ & पेस्ट
@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
कॉपी बस अमरूद EventBus वारिस, और आप कस्टम eventbus ही लिखना के लिए है। युक्तियाँ: इस कक्षा को com.google.common.eventbus पैकेज में लिखना चाहिए, ताकि आंतरिक विधि को ओवरराइट किया जा सके।
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);
}
}
क्या आप उस लिंक को ठीक कर सकते हैं? –
जुड़ा हुआ तय, इसे अभी आज़माएं। –