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.

  功能就是发布事件,也就是把某个事件告诉的所有与这个事件相关的监听器。

二:使用@EventLister

1.示例程序【同步】

接口:

package com.jun.practice.service;

public interface StudentEventRegisterService {
    /**
     * 发布事件,注册学生
     */
    void register();
}

  

接口实现:

@Service
public class StudentEventRegisterServiceImpl implements StudentEventRegisterService {
@Resource
private ApplicationEventPublisher applicationEventPublisher;

@Override
public void register() {
Student student = new Student();
student.setId(1);
student.setName("tom");
applicationEventPublisher.publishEvent(student);
System.out.println("结束了");
}
}

  

监听:


@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);
}
}

  

测试:

package com.jun.practice.controller;

import com.jun.practice.service.StudentEventRegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@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();
        }
    }
}

  效果:

2020-07-09 18:57:14.414  INFO 16704 --- [nio-9094-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 12 ms
Student(name=tom, id=1)
结束了

  

2.进行异步

进行配置类:

/**
 * 开启异步支持
 */
@Configuration
@EnableAsync
public class AsyncEventConfiguration implements AsyncConfigurer {
    @Override
    public Executor getAsyncExecutor() {
        return Executors.newFixedThreadPool(10);
    }
}

  

在监听方法上添加@Async

package com.jun.practice.listener;

import com.jun.practice.dto.Student;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@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)

  

3.实际的使用

  等写完后粘贴

三:使用@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

  

四:一个小需求实战

1.说明

  这里是参考晚上的介绍,尝试自己实现

五:最原生的实现

1.url

https://www.it610.com/article/1282423505006116864.htm

原文地址:https://www.cnblogs.com/juncaoit/p/13275339.html