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.url

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM