[英]@EventListener annotated method with non-void return value doesn't trigger a new event
我有一個以下方法,我試圖返回一個非 void 類型並希望通過相同的方法(遞歸地)處理已發布的事件。:
@EventListener
@Async
public GenericSpringEvent<?> onGenericEvent(GenericSpringEvent<?> event) throws InterruptedException {
Thread.sleep(5000);
System.out.println("Received spring generic event - " + event.getWhat() + ", thread id " + Thread.currentThread().getId());
return new GenericSpringEvent<String>(String.valueOf(System.currentTimeMillis()), true);
}
該方法最初由我的應用程序中的以下調用觸發:
GenericSpringEvent<String> genericSpringEvent = new GenericSpringEvent<>("GENERIC - STRING - TRUE", true);
applicationEventPublisher.publishEvent(genericSpringEvent);
監聽器只被調用一次。 我期望一個無限循環。 有人可以解釋我如何實現它。 不過,它不必是相同的偵聽器方法,我想了解此非空返回功能是如何工作的。 謝謝!
您只發布一次GenericSpringEvent! 這就是原因。 為了再次發布它,我看到了兩種選擇:
1-您必須執行相同的操作: applicationEventPublisher.publishEvent(genericSpringEvent);
在您的onGenericEvent方法中
2-在onGenericEvent方法周圍編寫一些方面,以便在返回后執行applicationEventPublisher.publishEvent(returnedGenericSpringEvent);
返回事件不會使其公開,用@EventListener注釋的方法與其他任何方法一樣,這就是為什么它可能具有非無效返回的原因。
它以這種方式工作
@Async
@EventListener
public CompletableFuture<ResponseDTO> generateXml(RequestDTO requestDTO) {
Thread.sleep(5000);
logger.debug("First step completed");
return CompletableFuture.supplyAsync(() -> ResponseDTO.builder()
.build());
}
@Async
@EventListener
public void sendXml(ResponseDTO responseDTO) {
logger.debug("Second step completed");
}
享受
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.