2012-02-10 34 views

Trả lời

8

Khi đứng, đây là một quyết định thận trọng, và thảo luận trong các tài liệu EventBus:

Handlers không nên, nói chung, ném. Nếu có, EventBus sẽ bắt và ghi lại ngoại lệ. Điều này hiếm khi là giải pháp đúng để xử lý lỗi và không nên dựa vào; nó chỉ nhằm mục đích giúp tìm ra vấn đề trong quá trình phát triển.

giải pháp thay thế là being considered, mặc dù tôi nghiêm túc nghi ngờ họ sẽ làm cho nó thành phiên bản 12.

+0

Bạn có thể sửa liên kết đó không? –

+1

Đã liên kết cố định, hãy thử ngay bây giờ. –

4

Dưới đây là mã cho lười biếng

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); 
     } 
    } 
} 

Bây giờ, bạn có thể đăng ký ExceptionEvent.

Đây là ExceptionEvent tôi cho chỉ cần sao chép & dán

@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

Chỉ cần kế thừa EventBus ổi, và viết bạn sở hữu eventbus tùy chỉnh. Mẹo: Lớp này nên viết trong gói com.google.common.eventbus, để phương thức nội bộ có thể được ghi đè.

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); 
    } 
} 
Các vấn đề liên quan