gpt4 book ai didi

java - @TransactionalEventListener 在@EventListener 像相同上下文的魅力一样工作的地方不起作用

转载 作者:行者123 更新时间:2023-11-29 08:27:48 24 4
gpt4 key购买 nike

我正在从事 spring boot 应用程序的事件驱动设计。

代码由以下文件组成:

  1. Spring boot:ApplicationEvent 文件,即 MyBusinessEvent.{java

    @Data
    @AllArgsConstructor
    public class MyBusinessEvent {

    private String data;

    }
  2. 事件发布者文件:MyBusinessService.java

    @Slf4j
    @Service
    public class MyBusinessService {

    private final ApplicationEventPublisher applicationEventPublisher;

    @Autowired
    public MyBusinessService(
    ApplicationEventPublisher applicationEventPublisher) {

    this.applicationEventPublisher = applicationEventPublisher;
    }

    @Override
    public void save() {
    String data = "Testing event data";
    MyBusinessEvent event = new MyBusinessEvent(data);
    applicationEventPublisher.publishEvent(event);
    }
    }
  3. 事件监听器:MyBusinessEventListener.java

    @Slf4j
    @Component
    public class MyBusinessEventListener {

    @EventListener
    public void handleEvent(MyBusinessEvent myBusinessEvent) {
    log.info("[MyBusinessEventListener] New event received with following data: {}", myBusinessEvent);
    }
    }

重点是当监听器文件包含 @EventListener 时,应用程序会按预期工作,但当我执行 @TransactionEventListener 时。例如:

  1. 事务事件监听器:MyBusinessEventListener.java

    @Slf4j
    @Component
    public class MyBusinessEventListener {

    @TransactionalEventListener
    public void handleEvent(MyBusinessEvent myBusinessEvent) {
    log.info("[MyBusinessEventListener] New event received with following data: {}", myBusinessEvent);
    }
    }

使用@TransactionalEventListener,它根本不起作用。

应用程序没有遇到任何异常(甚至在运行时也没有),但没有按预期进行日志记录。

是否缺少任何配置?

最佳答案

@Transactional 注释添加到 MyBusinessService.save 应该可以解决这个问题。

根据 java 文档,@TransactionalEventListener@Transactional 边界内工作。

这是文档所说的,

If the event is not published within the boundaries of a managed transaction, the
* event is discarded unless the {@link #fallbackExecution} flag is explicitly set.

如果您不想在事务边界中使用您的服务方法,请使用 @TransactionalEventListener(fallbackExecution = true)

编辑:对于上述问题,存在三种可能的解决方案。

我。将 class 标记为 @Transactional 以被视为事务性

@Transactional
@Service
public class MyBusinessService {

private final ApplicationEventPublisher applicationEventPublisher;

@Autowired
public MyBusinessService(
ApplicationEventPublisher applicationEventPublisher) {

this.applicationEventPublisher = applicationEventPublisher;
}

@Override
public void save() {
String data = "Testing event data";
MyBusinessEvent event = new MyBusinessEvent(data);
applicationEventPublisher.publishEvent(event);
}
}

二。将service的具体方法做成@Transactional

@Override
@Transactional
public void save() {
String data = "Testing event data";
MyBusinessEvent event = new MyBusinessEvent(data);
applicationEventPublisher.publishEvent(event);
}

三。如果您不想在事务边界中使用您的服务方法,请使用

@Slf4j
@Component
public class MyBusinessEventListener {

@TransactionalEventListener(fallbackExecution = true)
public void handleEvent(MyBusinessEvent myBusinessEvent) {
log.info("[MyBusinessEventListener] New event received with following data: {}", myBusinessEvent);
}
}

关于java - @TransactionalEventListener 在@EventListener 像相同上下文的魅力一样工作的地方不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51097916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com