diff --git a/src/main/java/com/rymcu/forest/handler/AccountHandler.java b/src/main/java/com/rymcu/forest/handler/AccountHandler.java index 8f99204..9a13ab4 100644 --- a/src/main/java/com/rymcu/forest/handler/AccountHandler.java +++ b/src/main/java/com/rymcu/forest/handler/AccountHandler.java @@ -6,6 +6,7 @@ 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 javax.annotation.Resource; @@ -25,6 +26,7 @@ public class AccountHandler { @Async("taskExecutor") @EventListener + @Transactional(rollbackFor = Exception.class) 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 7588fb4..fdfe54d 100644 --- a/src/main/java/com/rymcu/forest/handler/ArticleHandler.java +++ b/src/main/java/com/rymcu/forest/handler/ArticleHandler.java @@ -10,6 +10,7 @@ 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 javax.annotation.Resource; @@ -27,6 +28,7 @@ public class ArticleHandler { @EventListener @Async("taskExecutor") + @Transactional(rollbackFor = Exception.class) public void processArticlePostEvent(ArticleEvent articleEvent) throws InterruptedException { Thread.sleep(1000); log.info(String.format("执行文章发布相关事件:[%s]", JSON.toJSONString(articleEvent))); diff --git a/src/main/java/com/rymcu/forest/handler/CommentHandler.java b/src/main/java/com/rymcu/forest/handler/CommentHandler.java index 1ad1661..c60c325 100644 --- a/src/main/java/com/rymcu/forest/handler/CommentHandler.java +++ b/src/main/java/com/rymcu/forest/handler/CommentHandler.java @@ -11,6 +11,7 @@ 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 javax.annotation.Resource; @@ -31,7 +32,8 @@ public class CommentHandler { @Async("taskExecutor") @EventListener - public void processCommentCreatedEvent(CommentEvent commentEvent) throws InterruptedException { + @Transactional(rollbackFor = Exception.class) + public void processCommentCreatedEvent(CommentEvent commentEvent) { 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 new file mode 100644 index 0000000..f28da17 --- /dev/null +++ b/src/main/java/com/rymcu/forest/handler/FollowHandler.java @@ -0,0 +1,33 @@ +package com.rymcu.forest.handler; + +import com.alibaba.fastjson.JSON; +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; + +/** + * Created on 2023/4/28 16:07. + * + * @author ronger + * @email ronger-x@outlook.com + * @desc : com.rymcu.forest.handler + */ +@Slf4j +@Component +public class FollowHandler { + @Async("taskExecutor") + @EventListener + @Transactional(rollbackFor = Exception.class) + public void processFollowEvent(FollowEvent followEvent) throws InterruptedException { + Thread.sleep(1000); + log.info(String.format("执行关注相关事件: [%s]", JSON.toJSONString(followEvent))); + // 发送系统通知 + NotificationUtils.saveNotification(followEvent.getFollowingId(), followEvent.getIdFollow(), NotificationConstant.Follow, followEvent.getSummary()); + log.info("执行完成关注相关事件..."); + } +} diff --git a/src/main/java/com/rymcu/forest/handler/event/FollowEvent.java b/src/main/java/com/rymcu/forest/handler/event/FollowEvent.java new file mode 100644 index 0000000..fbd031f --- /dev/null +++ b/src/main/java/com/rymcu/forest/handler/event/FollowEvent.java @@ -0,0 +1,22 @@ +package com.rymcu.forest.handler.event; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * Created on 2023/4/28 16:04. + * + * @author ronger + * @email ronger-x@outlook.com + * @desc : com.rymcu.forest.handler.event + */ +@Data +@AllArgsConstructor +public class FollowEvent { + + private Long followingId; + + private Long idFollow; + + private String summary; +} diff --git a/src/main/java/com/rymcu/forest/service/TagService.java b/src/main/java/com/rymcu/forest/service/TagService.java index d4a4cf6..50047cf 100644 --- a/src/main/java/com/rymcu/forest/service/TagService.java +++ b/src/main/java/com/rymcu/forest/service/TagService.java @@ -18,6 +18,7 @@ public interface TagService extends Service { * * @param article * @param articleContentHtml + * @param userId * @return * @throws UnsupportedEncodingException */ diff --git a/src/main/java/com/rymcu/forest/service/impl/FollowServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/FollowServiceImpl.java index 23ba447..6bf252e 100644 --- a/src/main/java/com/rymcu/forest/service/impl/FollowServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/FollowServiceImpl.java @@ -1,13 +1,14 @@ package com.rymcu.forest.service.impl; -import com.rymcu.forest.core.constant.NotificationConstant; import com.rymcu.forest.core.service.AbstractService; import com.rymcu.forest.dto.UserDTO; import com.rymcu.forest.entity.Follow; +import com.rymcu.forest.handler.event.FollowEvent; import com.rymcu.forest.mapper.FollowMapper; import com.rymcu.forest.service.FollowService; -import com.rymcu.forest.util.NotificationUtils; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; @@ -20,6 +21,8 @@ public class FollowServiceImpl extends AbstractService implements Follow @Resource private FollowMapper followMapper; + @Resource + private ApplicationEventPublisher applicationEventPublisher; @Override public Boolean isFollow(Integer followingId, String followingType, Long idUser) { @@ -27,15 +30,17 @@ public class FollowServiceImpl extends AbstractService implements Follow } @Override + @Transactional(rollbackFor = Exception.class) public Boolean follow(Follow follow, String nickname) { int result = followMapper.insertSelective(follow); if (result > 0) { - NotificationUtils.saveNotification(follow.getFollowingId(), follow.getIdFollow(), NotificationConstant.Follow, nickname + " 关注了你!"); + applicationEventPublisher.publishEvent(new FollowEvent(follow.getFollowingId(), follow.getFollowerId(), nickname + " 关注了你!")); } return result > 0; } @Override + @Transactional(rollbackFor = Exception.class) public Boolean cancelFollow(Follow follow) { int result = followMapper.delete(follow); return result == 0; diff --git a/src/main/java/com/rymcu/forest/service/impl/TagServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/TagServiceImpl.java index 6dcf914..6354826 100644 --- a/src/main/java/com/rymcu/forest/service/impl/TagServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/TagServiceImpl.java @@ -111,7 +111,7 @@ public class TagServiceImpl extends AbstractService implements TagService { @Override @Transactional(rollbackFor = Exception.class) - public Tag saveTag(Tag tag) throws Exception { + public Tag saveTag(Tag tag) { Integer result; tag.setTagDescription(XssUtils.filterHtmlCode(tag.getTagDescription())); if (tag.getIdTag() == null) {