ApplicationEventPublisher的使用學(xué)習(xí)
今天在程序中看到有使用這個(gè)接口充坑,學(xué)習(xí)了一下减江,感覺作為觀察者模式的一個(gè)實(shí)現(xiàn)方式,使用起來還是不錯(cuò)的捻爷。查了一些資料,結(jié)合自己的程序份企,分三個(gè)部分進(jìn)行介紹也榄。 等程序自測(cè)完成后,補(bǔ)充完成司志,先寫一部分甜紫。
一、介紹
1.ApplicationEventPublisherAware
ApplicationEventPublisherAware 是由 Spring 提供的用于為 Service 注入 ApplicationEventPublisher 事件發(fā)布器的接口骂远,使用這個(gè)接口囚霸,我們自己的 Service
就擁有了發(fā)布事件的能力。
用戶注冊(cè)后激才,不再是顯示調(diào)用其他的業(yè)務(wù) Service拓型,而是發(fā)布一個(gè)用戶注冊(cè)事件。
2.ApplicationListener
ApplicationListener接口是由 Spring 提供的事件訂閱者必須實(shí)現(xiàn)的接口瘸恼,我們一般把該 Service 關(guān)心的事件類型作為泛型傳入劣挫。處理事件,通過 event.getSource() 即可拿到事件的具體內(nèi)容
3.ApplicationEventPublisher
ApplicationEventPublisher是ApplicationContext的父接口之一东帅。這接口的作用是:Interface that encapsulates event publication functionality.
功能就是發(fā)布事件压固,也就是把某個(gè)事件告訴的所有與這個(gè)事件相關(guān)的監(jiān)聽器。
二靠闭、使用@EventLister
1.示例程序【同步】
接口:
package com.jun.practice.service;
public interface StudentEventRegisterService {
/**
* 發(fā)布事件帐我,注冊(cè)學(xué)生
*/
void register();
}
接口實(shí)現(xiàn):
@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("結(jié)束了");
}
}
監(jiān)聽:
@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);
}
}
測(cè)試:
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 = "事件監(jiān)控", tags = "事件監(jiān)控")
public class EventListenerController {
@Resource
private StudentEventRegisterService studentEventRegisterService;
@ApiOperation("@EventListener測(cè)試")
@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)
結(jié)束了
2.進(jìn)行異步
進(jìn)行配置類:
/**
* 開啟異步支持
*/
@Configuration
@EnableAsync
public class AsyncEventConfiguration implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
return Executors.newFixedThreadPool(10);
}
}
在監(jiān)聽方法上添加@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);
}
}
效果:
結(jié)束了
Student(name=tom, id=1)
3.實(shí)際的使用
等寫完后粘貼
三、使用@TransactionalEventListener
Spring事務(wù)監(jiān)聽機(jī)制---使用@TransactionalEventListener處理數(shù)據(jù)庫(kù)事務(wù)提交成功后再執(zhí)行操作
1.為什么使用
在項(xiàng)目中愧膀,往往需要執(zhí)行數(shù)據(jù)庫(kù)操作后拦键,發(fā)送消息或事件來異步調(diào)用其他組件執(zhí)行相應(yīng)的操作,例如: 用戶注冊(cè)后發(fā)送激活碼扇调; 配置修改后發(fā)送更新事件等矿咕。 但是,數(shù)據(jù)庫(kù)的操作如果還未完成狼钮,此時(shí)異步調(diào)用的方法查詢數(shù)據(jù)庫(kù)發(fā)現(xiàn)沒有數(shù)據(jù)碳柱,這就會(huì)出現(xiàn)問題。
為了解決上述問題熬芜,Spring為我們提供了兩種方式:
(1) @TransactionalEventListener注解
(2) 事務(wù)同步管理器TransactionSynchronizationManager 以便我們可以在事務(wù)提交后再觸發(fā)某一事件莲镣。
2.示例
@Transaction<br>void saveUser(User u) {
//保存用戶信息
userDao.save(u);
//觸發(fā)保存用戶事件
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);
}
這樣,只有當(dāng)前事務(wù)提交之后涎拉,才會(huì)執(zhí)行事件監(jiān)聽器的方法瑞侮。其中參數(shù)phase默認(rèn)為AFTER_COMMIT的圆,共有四個(gè)枚舉:
/**
* 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