private static String message(SubscriberExceptionContext context) { Method method = context.getSubscriberMethod(); return "Exception thrown by subscriber method " + method.getName() + '(' + method.getParameterTypes()[0].getName() + ')' + " on subscriber " + context.getSubscriber() + " when dispatching event: " + context.getEvent(); } }
public void testSubscriberThrowsException() throws Exception { final RecordingSubscriberExceptionHandler handler = new RecordingSubscriberExceptionHandler(); final EventBus eventBus = new EventBus(handler); final RuntimeException exception = new RuntimeException("but culottes have a tendancy to ride up!"); final Object subscriber = new Object() { @Subscribe public void throwExceptionOn(String message) { throw exception; } }; eventBus.register(subscriber); eventBus.post(EVENT); assertEquals("Cause should be available.", exception, handler.exception); assertEquals("EventBus should be available.", eventBus, handler.context.getEventBus()); assertEquals("Event should be available.", EVENT, handler.context.getEvent()); assertEquals("Subscriber should be available.", subscriber, handler.context.getSubscriber()); assertEquals( "Method should be available.", subscriber.getClass().getMethod("throwExceptionOn", String.class), handler.context.getSubscriberMethod()); }
private static String message(SubscriberExceptionContext context) { Method method = context.getSubscriberMethod(); return "Exception thrown by subscriber method " + method.getName() + '(' + method.getParameterTypes()[0].getName() + ')' + " on subscriber " + context.getSubscriber() + " when dispatching event: " + context.getEvent(); } }
public void testSubscriberThrowsException() throws Exception{ final RecordingSubscriberExceptionHandler handler = new RecordingSubscriberExceptionHandler(); final EventBus eventBus = new EventBus(handler); final RuntimeException exception = new RuntimeException("but culottes have a tendancy to ride up!"); final Object subscriber = new Object() { @Subscribe public void throwExceptionOn(String message) { throw exception; } }; eventBus.register(subscriber); eventBus.post(EVENT); assertEquals("Cause should be available.", exception, handler.exception); assertEquals("EventBus should be available.", eventBus, handler.context.getEventBus()); assertEquals("Event should be available.", EVENT, handler.context.getEvent()); assertEquals("Subscriber should be available.", subscriber, handler.context.getSubscriber()); assertEquals("Method should be available.", subscriber.getClass().getMethod("throwExceptionOn", String.class), handler.context.getSubscriberMethod()); }
private static String message(SubscriberExceptionContext context) { Method method = context.getSubscriberMethod(); return "Exception thrown by subscriber method " + method.getName() + '(' + method.getParameterTypes()[0].getName() + ')' + " on subscriber " + context.getSubscriber() + " when dispatching event: " + context.getEvent(); } }
@Override public void handleException(final Throwable exception, final SubscriberExceptionContext context) { final Object event = context.getEvent(); processException(exception, event); }
import com.google.common.eventbus.SubscriberExceptionContext; import com.google.common.eventbus.SubscriberExceptionHandler; class CustomExceptionHandler implements SubscriberExceptionHandler { @Override public void handleException(Throwable exception, SubscriberExceptionContext context) { System.out.println("Handler '"+context.getSubscriber()+"' could not handle '"+context.getEvent().getClass().getName()+"'["+context.getEvent()+"]: " + exception.getMessage()); } }
@Override public void handleException(Throwable exception, SubscriberExceptionContext context) { log.error("异步消息队列异常: [subscribeMethod={}, event={} ]",context.getSubscriberMethod(), context.getEvent().toString(),exception); } });
@Override public void handleException(Throwable exception, SubscriberExceptionContext context) { log.error("异步消息队列异常: [subscribeMethod={}, event={} ]",context.getSubscriberMethod(), context.getEvent().toString(),exception); } });
private static String message(SubscriberExceptionContext context) { Method method = context.getSubscriberMethod(); return "Exception thrown by subscriber method " + method.getName() + '(' + method.getParameterTypes()[0].getName() + ')' + " on subscriber " + context.getSubscriber() + " when dispatching event: " + context.getEvent(); } }
private static String message(SubscriberExceptionContext context) { Method method = context.getSubscriberMethod(); return "Exception thrown by subscriber method " + method.getName() + '(' + method.getParameterTypes()[0].getName() + ')' + " on subscriber " + context.getSubscriber() + " when dispatching event: " + context.getEvent(); } }
@Override public void handleException(final Throwable exception, final SubscriberExceptionContext context) { logger.error("Could not dispatch event {} to subscriber {} method [{}]", context.getEvent(), context.getSubscriber(), context.getSubscriberMethod(), exception); } }
@Override public void handleException(Throwable exception, SubscriberExceptionContext context) { log.error("同步消息总线异常: [subscribeMethod={}, event={} ]",context.getSubscriberMethod(),context.getEvent().toString(),exception); throw new ServiceException(ErrorCode.BUS_ERROR.getErrorCode(), ErrorCode.BUS_ERROR.getErrorMsg(), exception); } });