Exceptions in Spring Event Listeners are invisible, but blocking

I had an issue which was missed due to exceptions not being thrown within spring event listeners.

Essentially the series of events went:

  1. MyEvent published
  2. listener1 is invoked
  3. listener2 is invoked, but encounters an exception
  4. No indication that an exception occurred (eg in console/logs)
  5. listener3 is NOT invoked

However, when I added configuration for the SimpleApplicationEventMulticaster, defining an errorHandler which just logs the exception, the following happened:

  1. MyEvent published
  2. listener1 is invoked
  3. listener2 is invoked, but encounters an exception
  4. listener3 is invoked
  5. exception is logged via my errorHandler

My Question
If the application will not proceed to the next listener following an exception in the default configuration, why does it do it when I configure it to print the exception?

I have a feeling that the exception being ‘invisible’ (no indication that the exception occurred) may be down to my use of Executors? However, I don’t understand the inconsistency in the exceptions being ‘blocking’ or not.

My Test Setup:

@Configuration
public class AppConfig {

    // If the following configuration is present (just logging the NPE), all listeners are invoked.
    // However, if it is removed, the NPE is not recorded, and only listeners 1 and 2 are invoked.

    @Bean
    public SimpleApplicationEventMulticaster applicationEventMulticaster() {
        SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
        multicaster.setErrorHandler(throwable -> {
            System.err.println("Exception in event listener: " + throwable.getMessage());
        });
        return multicaster;
    }
}
@EnableScheduling
@Component
public class EventsTesting {

    private final ApplicationEventPublisher publisher;
    private final ExecutorService executor = Executors.newSingleThreadExecutor();

    @Autowired
    private EventsTesting(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }


    // PUBLISHING MyEvent EVERY 5 SECONDS

    @Scheduled(fixedRate = 5000)
    private void publishEvent() {
        System.out.println("PUBLISH EVENT");
        executor.submit(() -> publisher.publishEvent(new MyEvent(this)));
    }


    // EVENT LISTENERS HANDLE THE EVENT SEQUENTIALLY

    @Order(1)
    @EventListener
    private void handleMyEvent1(MyEvent event) {
        System.out.println("LISTENER 01");
    }

    @Order(2)
    @EventListener
    private void handleMyEvent2(MyEvent event) {
        System.out.println("LISTENER 02");
        String npe = event.getNull().toLowerCase();
    }

    @Order(3)
    @EventListener
    private void handleMyEvent3(MyEvent event) {
        System.out.println("LISTENER 03");
    }





    public static class MyEvent extends ApplicationEvent {

        public MyEvent(Object source) {
            super(source);
        }

        public String getNull() { return null;}
    }
}

With the SimpleApplicationEventMulticaster configuration present, I see the following logs:

PUBLISH EVENT
LISTENER 01
LISTENER 02
LISTENER 03
Exception in event listener: Cannot invoke "String.toLowerCase()" because the return value of "com.example.eventhandlingbug.EventsTesting$MyEvent.getNull()" is null
PUBLISH EVENT
LISTENER 01
LISTENER 02
LISTENER 03
Exception in event listener: Cannot invoke "String.toLowerCase()" because the return value of "com.example.eventhandlingbug.EventsTesting$MyEvent.getNull()" is null

However, without the configuration I see this:

PUBLISH EVENT
LISTENER 01
LISTENER 02
PUBLISH EVENT
LISTENER 01
LISTENER 02

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật