🐛 正确使用 Spring Event

This commit is contained in:
ronger 2023-05-13 08:51:38 +08:00
parent e89e4fa49d
commit d66a515da0
4 changed files with 16 additions and 33 deletions

View File

@ -3,10 +3,8 @@ package com.rymcu.forest.handler;
import com.rymcu.forest.handler.event.AccountEvent;
import com.rymcu.forest.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;
import javax.annotation.Resource;
@ -24,9 +22,7 @@ public class AccountHandler {
@Resource
private UserMapper userMapper;
@Async("taskExecutor")
@EventListener
@Transactional(rollbackFor = Exception.class)
@TransactionalEventListener
public void processAccountLastOnlineTimeEvent(AccountEvent accountEvent) {
userMapper.updateLastOnlineTimeByAccount(accountEvent.getAccount());
}

View File

@ -7,10 +7,8 @@ import com.rymcu.forest.handler.event.ArticleEvent;
import com.rymcu.forest.lucene.service.LuceneService;
import com.rymcu.forest.util.NotificationUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;
import javax.annotation.Resource;
@ -26,11 +24,8 @@ public class ArticleHandler {
@Resource
private LuceneService luceneService;
@EventListener
@Async("taskExecutor")
@Transactional(rollbackFor = Exception.class)
public void processArticlePostEvent(ArticleEvent articleEvent) throws InterruptedException {
Thread.sleep(1000);
@TransactionalEventListener
public void processArticlePostEvent(ArticleEvent articleEvent) {
log.info(String.format("执行文章发布相关事件:[%s]", JSON.toJSONString(articleEvent)));
// 发送系统通知
if (articleEvent.getNotification()) {
@ -57,10 +52,8 @@ public class ArticleHandler {
log.info("执行完成文章发布相关事件...id={}", articleEvent.getIdArticle());
}
@EventListener
@Async("taskExecutor")
public void processArticleDeleteEvent(ArticleDeleteEvent articleDeleteEvent) throws InterruptedException {
Thread.sleep(1000);
@TransactionalEventListener
public void processArticleDeleteEvent(ArticleDeleteEvent articleDeleteEvent) {
log.info(String.format("执行文章删除相关事件:[%s]", JSON.toJSONString(articleDeleteEvent)));
luceneService.deleteArticle(articleDeleteEvent.getIdArticle());
log.info("执行完成文章删除相关事件...id={}", articleDeleteEvent.getIdArticle());

View File

@ -8,12 +8,11 @@ import com.rymcu.forest.mapper.CommentMapper;
import com.rymcu.forest.util.Html2TextUtil;
import com.rymcu.forest.util.NotificationUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;
import javax.annotation.Resource;
import javax.mail.MessagingException;
/**
* Created on 2022/8/17 7:38.
@ -30,10 +29,8 @@ public class CommentHandler {
@Resource
private CommentMapper commentMapper;
@Async("taskExecutor")
@EventListener
@Transactional(rollbackFor = Exception.class)
public void processCommentCreatedEvent(CommentEvent commentEvent) {
@TransactionalEventListener
public void processCommentCreatedEvent(CommentEvent commentEvent) throws MessagingException {
log.info(String.format("开始执行评论发布事件:[%s]", JSON.toJSONString(commentEvent)));
String commentContent = commentEvent.getContent();
int length = commentContent.length();

View File

@ -5,10 +5,10 @@ import com.rymcu.forest.core.constant.NotificationConstant;
import com.rymcu.forest.handler.event.FollowEvent;
import com.rymcu.forest.util.NotificationUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;
import javax.mail.MessagingException;
/**
* Created on 2023/4/28 16:07.
@ -20,11 +20,8 @@ import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Component
public class FollowHandler {
@Async("taskExecutor")
@EventListener
@Transactional(rollbackFor = Exception.class)
public void processFollowEvent(FollowEvent followEvent) throws InterruptedException {
Thread.sleep(1000);
@TransactionalEventListener
public void processFollowEvent(FollowEvent followEvent) throws MessagingException {
log.info(String.format("执行关注相关事件: [%s]", JSON.toJSONString(followEvent)));
// 发送系统通知
NotificationUtils.saveNotification(followEvent.getFollowingId(), followEvent.getIdFollow(), NotificationConstant.Follow, followEvent.getSummary());