我使用guava的EventBus,不幸的是它捕获并记录了事件处理程序抛出RuntimeException时发生的InvocationTargetException。我可以禁用此行为吗?Guava EventBus:不捕获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);
}
}
相关问题
- 1. Guava EventBus单元测试
- 2. 回滚捕获的RuntimeException
- 3. Guava java EventBus实例化错误
- 4. 谷歌guava singleton Eventbus多次触发
- 5. 为什么Guava EventBus不能实现接口?
- 6. 什么时候可以捕获RuntimeException
- 7. Guava EventBus:它在GUI应用程序中的位置?
- 8. 在Vaadin处理不可序列化的Guava EventBus与会话序列化
- 9. 获取的RuntimeException甚至有尝试捕捉
- 10. RuntimeException捕获请求侦听器 - > Twig错误模板
- 11. 致命错误:未捕获的异常 '的RuntimeException'
- 12. 在Multimaps中获取值 - Guava
- 13. Mosby&EventBus
- 14. Guava ImmutableSortedSetMultimap?
- 15. 捕获不能捕获类型'Microsoft.Office.Interop.Outlook.Exception'
- 16. PlayFramework 1.2.x的EventBus实现
- 17. 如果RunTimeException被抛出并且未被捕获,则返回类型值
- 18. Seam不捕获ViewExpiredException
- 19. HttpURLConnection RuntimeException
- 20. ClassNotFoundException/RuntimeException
- 21. Catch RuntimeException
- 22. Java RuntimeException
- 23. PopupMenu RuntimeException
- 24. Guava Mutlimap to HashMap
- 25. Guava Group按键
- 26. Guava MultiSet vs Map?
- 27. 尝试捕捉list_to_integer不捕获错误
- 28. 番石榴EventBus:订阅没有注释?
- 29. codeigniter遇到未捕获的异常类型:RuntimeException在Linux服务器中不起作用
- 30. 的Android得EventBus事件不工作
你能解决这个问题吗? – 2012-02-10 21:41:04
链接固定,现在就试试。 – 2012-02-10 22:40:24