ApplicationEventPublisher的使用学习

一、介绍

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
Spring ApplicationEventPublisherSpring 框架中的一个接口,用于发布应用程序的事件。通过 ApplicationEventPublisher,我们可以在应用程序中定义和发布自定义事件,并将其传播到对该事件感兴趣的监听器。 Spring 框架基于观察者设计模式,通过 ApplicationEventPublisherApplicationListener 来实现事件发布和监听。当我们需要在应用程序中触发一些事件时,可以创建一个实现了 ApplicationEvent 类的自定义事件,并使用 ApplicationEventPublisher 将该事件发布出去。 使用 ApplicationEventPublisher 的方式有两种: 1. 通过实现 ApplicationEventPublisherAware 接口自动注入 ApplicationEventPublisher 实例。这样就可以在任何需要发布事件的地方直接调用 ApplicationEventPublisherpublishEvent 方法。 2. 在 Spring 容器中注入 ApplicationEventPublisher 实例,然后在需要发布事件的地方手动调用 publishEvent 方法。 发布事件之后,事件会被传播给所有实现了 ApplicationListener 接口的监听器。通过实现 ApplicationListener 接口,我们可以自定义事件的监听器,并在监听器中编写逻辑,处理事件触发后的业务逻辑。 Spring 框架的事件机制可以使我们的应用程序更加模块化和可扩展。我们可以定义多个事件和监听器,实现不同模块之间的解耦。同时,通过事件机制,我们也可以更方便地实现日志记录、业务流程控制等功能。 总之,Spring ApplicationEventPublisherSpring 框架中的一个重要组件,用于实现事件发布和监听。通过定义自定义事件和监听器,我们可以实现应用程序的模块化和解耦,提高代码的可维护性和可扩展性。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值