ApplicationEventPublisher的使用学习

本文介绍了Spring中的事件处理机制,包括ApplicationEventPublisherAware接口用于服务发布事件,ApplicationListener接口用于订阅事件。通过示例展示了同步和异步事件的处理,并详细解释了@TransactionalEventListener注解如何在事务提交后执行操作,确保数据一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、介绍

1.ApplicationEventPublisherAware
  ApplicationEventPublisherAware 是由 Spring 提供的用于为 Service 注入 ApplicationEventPublisher 事件发布器的接口,使用这个接口,我们自己的 Service 就拥有了发布事件的能力。
  用户注册后,不再是显示地调用其他的业务 Service,而是发布一个用户注册事件。
2.ApplicationListener
  ApplicationListener接口是由 Spring 提供的事件订阅者必须实现的接口,我们一般把该 Service 关心的事件类型作为泛型传入。处理事件,通过 event.getSource() 即可拿到事件的具体内容

3.ApplicationEventPublisher
  ApplicationEventPublisher是ApplicationContext的父接口之一。这接口的作用是:Interface that encapsulates event publication functionality.
  功能就是发布事件,也就是把某个事件告诉的所有与这个事件相关的监听器。

二、定义事件

自定义需要发布的事件类型,自定义的event继承ApplicationEvent

public class StudentEvent extends ApplicationEvent {
    private String name;
    private String msg;
    public StudentEvent (Object source){
        super(source);
    }
    public StudentEvent (Object source, String name, String msg) {
        super(source);
        this.name = name;
        this.msg = msg;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

三、使用@EventLister

1. 同步

  1. 示例程序
    接口
public interface StudentRegisterService {
    /**
     * 发布事件,注册学生
     */
    void register();
}
  1. 接口实现
@Service
public class StudentRegisterServiceImpl implements StudentRegisterService {
    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void register() {
        Student student = new Student();
        student.setId(1);
        student.setName("tom");
        applicationEventPublisher.publishEvent(student);
        System.out.println("结束了");
    }
}
  1. 监听
@Component
public class StudentEventListener {
    @EventListener(condition = "#student.id != null")
    public void handleEvent(Student student){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(student);
    }
}
  1. 测试
@RestController
@RequestMapping("/event")
@Api(value = "事件监控", tags = "事件监控")
public class EventListenerController {
    @Resource
    private StudentEventRegisterService studentEventRegisterService;
 
    @ApiOperation("@EventListener测试")
    @GetMapping("/registerUser")
    public void register()  {
        try {
            studentEventRegisterService.register();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

效果:

Student(name=tom, id=1)
结束了

2. 异步

  1. 配置异步线程池
/**
 * 开启异步支持
 */
@Configuration
@EnableAsync
public class AsyncEventConfiguration implements AsyncConfigurer {
    @Override
    public Executor getAsyncExecutor() {
        return Executors.newFixedThreadPool(10);
    }
}
  1. 在监听方法上添加@Async
@Component
public class StudentEventListener {
    @Async
    @EventListener(condition = "#student.id != null")
    public void handleEvent(Student student){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(student);
    }
}

效果

结束了
Student(name=tom, id=1)

四、使用@TransactionalEventListener

Spring事务监听机制—使用@TransactionalEventListener处理数据库事务提交成功后再执行操作

  1. 为什么使用  
      在项目中,往往需要执行数据库操作后,发送消息或事件来异步调用其他组件执行相应的操作,例如:
      用户注册后发送激活码;
      配置修改后发送更新事件等。
      但是,数据库的操作如果还未完成,此时异步调用的方法查询数据库发现没有数据,这就会出现问题。

为了解决上述问题,Spring为我们提供了两种方式:
  (1) @TransactionalEventListener注解
  (2) 事务同步管理器TransactionSynchronizationManager
  以便我们可以在事务提交后再触发某一事件。
2. 示例

@Transaction
void saveUser(User u) {
    //保存用户信息
    userDao.save(u);
    //触发保存用户事件
    applicationContext.publishEvent(new SaveUserEvent(u.getId()));
}
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
void onSaveUserEvent(SaveUserEvent event) {
    Integer id = event.getEventData();
    User u = userDao.getUserById(id);
    String phone = u.getPhoneNumber();
    MessageUtils.sendMessage(phone);
}

这样,只有当前事务提交之后,才会执行事件监听器的方法。其中参数phase默认为AFTER_COMMIT,共有四个枚举:

/**
     * Fire the event before transaction commit.
     * @see TransactionSynchronization#beforeCommit(boolean)
     */
    BEFORE_COMMIT,
 
    /**
     * Fire the event after the commit has completed successfully.
     * <p>Note: This is a specialization of {@link #AFTER_COMPLETION} and
     * therefore executes in the same after-completion sequence of events,
     * (and not in {@link TransactionSynchronization#afterCommit()}).
     * @see TransactionSynchronization#afterCompletion(int)
     * @see TransactionSynchronization#STATUS_COMMITTED
     */
    AFTER_COMMIT,
 
    /**
     * Fire the event if the transaction has rolled back.
     * <p>Note: This is a specialization of {@link #AFTER_COMPLETION} and
     * therefore executes in the same after-completion sequence of events.
     * @see TransactionSynchronization#afterCompletion(int)
     * @see TransactionSynchronization#STATUS_ROLLED_BACK
     */
    AFTER_ROLLBACK,
 
    /**
     * Fire the event after the transaction has completed.
     * <p>For more fine-grained events, use {@link #AFTER_COMMIT} or
     * {@link #AFTER_ROLLBACK} to intercept transaction commit
     * or rollback, respectively.
     * @see TransactionSynchronization#afterCompletion(int)
     */
    AFTER_COMPLETION
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值