🧑💻 增加异步事件回滚事务
🧑💻 增加异步事件回滚事务
This commit is contained in:
commit
6c9efc931e
@ -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());
|
||||
}
|
||||
|
@ -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)));
|
||||
|
@ -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();
|
||||
|
33
src/main/java/com/rymcu/forest/handler/FollowHandler.java
Normal file
33
src/main/java/com/rymcu/forest/handler/FollowHandler.java
Normal file
@ -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("执行完成关注相关事件...");
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
@ -18,6 +18,7 @@ public interface TagService extends Service<Tag> {
|
||||
*
|
||||
* @param article
|
||||
* @param articleContentHtml
|
||||
* @param userId
|
||||
* @return
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
|
@ -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<Follow> 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<Follow> 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;
|
||||
|
@ -111,7 +111,7 @@ public class TagServiceImpl extends AbstractService<Tag> 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) {
|
||||
|
Loading…
Reference in New Issue
Block a user