From d66a515da07185cb8d53c883079b6dd92353a8c5 Mon Sep 17 00:00:00 2001 From: ronger Date: Sat, 13 May 2023 08:51:38 +0800 Subject: [PATCH 1/3] =?UTF-8?q?:bug:=20=E6=AD=A3=E7=A1=AE=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20Spring=20Event?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rymcu/forest/handler/AccountHandler.java | 8 ++------ .../rymcu/forest/handler/ArticleHandler.java | 17 +++++------------ .../rymcu/forest/handler/CommentHandler.java | 11 ++++------- .../com/rymcu/forest/handler/FollowHandler.java | 13 +++++-------- 4 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/rymcu/forest/handler/AccountHandler.java b/src/main/java/com/rymcu/forest/handler/AccountHandler.java index 9a13ab4..f696c7b 100644 --- a/src/main/java/com/rymcu/forest/handler/AccountHandler.java +++ b/src/main/java/com/rymcu/forest/handler/AccountHandler.java @@ -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()); } diff --git a/src/main/java/com/rymcu/forest/handler/ArticleHandler.java b/src/main/java/com/rymcu/forest/handler/ArticleHandler.java index fdfe54d..b54f61f 100644 --- a/src/main/java/com/rymcu/forest/handler/ArticleHandler.java +++ b/src/main/java/com/rymcu/forest/handler/ArticleHandler.java @@ -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()); diff --git a/src/main/java/com/rymcu/forest/handler/CommentHandler.java b/src/main/java/com/rymcu/forest/handler/CommentHandler.java index c60c325..f044e1c 100644 --- a/src/main/java/com/rymcu/forest/handler/CommentHandler.java +++ b/src/main/java/com/rymcu/forest/handler/CommentHandler.java @@ -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(); diff --git a/src/main/java/com/rymcu/forest/handler/FollowHandler.java b/src/main/java/com/rymcu/forest/handler/FollowHandler.java index f28da17..1ad65e6 100644 --- a/src/main/java/com/rymcu/forest/handler/FollowHandler.java +++ b/src/main/java/com/rymcu/forest/handler/FollowHandler.java @@ -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()); From 0eb8c9872e4bc9edcf8ecff138a1b438e55775a3 Mon Sep 17 00:00:00 2001 From: ronger Date: Sat, 13 May 2023 08:52:15 +0800 Subject: [PATCH 2/3] =?UTF-8?q?:art:=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rymcu/forest/util/NotificationUtils.java | 95 ++++++++----------- 1 file changed, 38 insertions(+), 57 deletions(-) diff --git a/src/main/java/com/rymcu/forest/util/NotificationUtils.java b/src/main/java/com/rymcu/forest/util/NotificationUtils.java index 56a1c87..b137188 100644 --- a/src/main/java/com/rymcu/forest/util/NotificationUtils.java +++ b/src/main/java/com/rymcu/forest/util/NotificationUtils.java @@ -10,9 +10,9 @@ import com.rymcu.forest.entity.Notification; import com.rymcu.forest.entity.User; import com.rymcu.forest.service.*; +import javax.mail.MessagingException; import java.util.List; import java.util.Objects; -import java.util.concurrent.*; /** * 消息通知工具类 @@ -21,75 +21,56 @@ import java.util.concurrent.*; */ public class NotificationUtils { - private static NotificationService notificationService = SpringContextHolder.getBean(NotificationService.class); - private static UserService userService = SpringContextHolder.getBean(UserService.class); - private static FollowService followService = SpringContextHolder.getBean(FollowService.class); - private static JavaMailService mailService = SpringContextHolder.getBean(JavaMailService.class); - - private static ArticleService articleService = SpringContextHolder.getBean(ArticleService.class); - private static CommentService commentService = SpringContextHolder.getBean(CommentService.class); + private static final NotificationService notificationService = SpringContextHolder.getBean(NotificationService.class); + private static final UserService userService = SpringContextHolder.getBean(UserService.class); + private static final FollowService followService = SpringContextHolder.getBean(FollowService.class); + private static final JavaMailService mailService = SpringContextHolder.getBean(JavaMailService.class); + private static final ArticleService articleService = SpringContextHolder.getBean(ArticleService.class); + private static final CommentService commentService = SpringContextHolder.getBean(CommentService.class); public static void sendAnnouncement(Long dataId, String dataType, String dataSummary) { - ExecutorService executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); - CompletableFuture.supplyAsync(() -> { + List users = userService.findAll(); + users.forEach(user -> { try { - List users = userService.findAll(); - users.forEach(user -> { - saveNotification(user.getIdUser(), dataId, dataType, dataSummary); - }); - } catch (Exception ex) { - ex.printStackTrace(); + saveNotification(user.getIdUser(), dataId, dataType, dataSummary); + } catch (MessagingException e) { + throw new RuntimeException(e); } - return 0; - }, executor); + }); } - public static void saveNotification(Long idUser, Long dataId, String dataType, String dataSummary) { - ExecutorService executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); - CompletableFuture.supplyAsync(() -> { - try { - Notification notification = notificationService.findNotification(idUser, dataId, dataType); - if (notification == null || NotificationConstant.UpdateArticle.equals(dataType)) { - System.out.println("------------------- 开始执行消息通知 ------------------"); - Integer result = notificationService.save(idUser, dataId, dataType, dataSummary); - if (result == 0) { - // TODO 记录操作失败数据 - } - } - if (NotificationConstant.Comment.equals(dataType)) { - notification = notificationService.findNotification(idUser, dataId, dataType); - NotificationDTO notificationDTO = genNotification(notification); - mailService.sendNotification(notificationDTO); - } - } catch (Exception ex) { + public static void saveNotification(Long idUser, Long dataId, String dataType, String dataSummary) throws MessagingException { + Notification notification = notificationService.findNotification(idUser, dataId, dataType); + if (notification == null || NotificationConstant.UpdateArticle.equals(dataType)) { + System.out.println("------------------- 开始执行消息通知 ------------------"); + Integer result = notificationService.save(idUser, dataId, dataType, dataSummary); + if (result == 0) { // TODO 记录操作失败数据 - ex.printStackTrace(); } - return 0; - }, executor); - + } + if (NotificationConstant.Comment.equals(dataType)) { + notification = notificationService.findNotification(idUser, dataId, dataType); + NotificationDTO notificationDTO = genNotification(notification); + mailService.sendNotification(notificationDTO); + } } public static void sendArticlePush(Long dataId, String dataType, String dataSummary, Long articleAuthorId) { - ExecutorService executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); - CompletableFuture.supplyAsync(() -> { + List follows; + if (NotificationConstant.PostArticle.equals(dataType)) { + // 关注用户通知 + follows = followService.findByFollowingId("0", articleAuthorId); + } else { + // 关注文章通知 + follows = followService.findByFollowingId("3", articleAuthorId); + } + follows.forEach(follow -> { try { - List follows; - if (NotificationConstant.PostArticle.equals(dataType)) { - // 关注用户通知 - follows = followService.findByFollowingId("0", articleAuthorId); - } else { - // 关注文章通知 - follows = followService.findByFollowingId("3", articleAuthorId); - } - follows.forEach(follow -> { - saveNotification(follow.getFollowerId(), dataId, dataType, dataSummary); - }); - } catch (Exception ex) { - ex.printStackTrace(); + saveNotification(follow.getFollowerId(), dataId, dataType, dataSummary); + } catch (MessagingException e) { + throw new RuntimeException(e); } - return 0; - }, executor); + }); } public static NotificationDTO genNotification(Notification notification) { From e181ba864842bf4216cfe66778076a5b89d94951 Mon Sep 17 00:00:00 2001 From: ronger Date: Sat, 13 May 2023 08:53:33 +0800 Subject: [PATCH 3/3] =?UTF-8?q?:technologist:=20=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E6=9C=8D=E5=8A=A1=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/com/rymcu/forest/service/TagServiceTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/rymcu/forest/service/TagServiceTest.java b/src/test/java/com/rymcu/forest/service/TagServiceTest.java index cec45e6..93b7878 100644 --- a/src/test/java/com/rymcu/forest/service/TagServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/TagServiceTest.java @@ -118,7 +118,7 @@ class TagServiceTest extends BaseServiceTest { @DisplayName("添加/更新标签") void saveTag() throws Exception { List tagLabels = tagService.findTagLabels(); - assertEquals(1, tagLabels.size()); + assertNotNull(tagLabels); Tag tag = new Tag(); tag.setTagDescription("test1"); @@ -130,7 +130,7 @@ class TagServiceTest extends BaseServiceTest { assertNotNull(tag1.getIdTag()); tagLabels = tagService.findTagLabels(); - assertEquals(1, tagLabels.size()); + assertNotNull(tagLabels); tag.setIdTag(null); assertThrows(BusinessException.class, () -> tagService.saveTag(tag)); @@ -142,4 +142,4 @@ class TagServiceTest extends BaseServiceTest { List tagLabels = tagService.findTagLabels(); assertFalse(tagLabels.isEmpty()); } -} \ No newline at end of file +}