Skip to content

Commit

Permalink
Fix: 解决新增内容时,数据未刷到lucene问题(切面Order顺序问题)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangrunkang committed Apr 11, 2022
1 parent 6a725ed commit d75683a
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.upupor.lucene.enums.LuceneDataType;
import com.upupor.lucene.enums.LuceneOperationType;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;

import javax.annotation.Resource;

Expand All @@ -40,6 +42,7 @@
* @date 2022年04月04日 14:13
* @email: [email protected]
*/
@Slf4j
public abstract class AbstractFlush<T> {

@Getter
Expand Down Expand Up @@ -80,6 +83,9 @@ protected void update() {
public abstract LuceneDataType runDataType();

public void flush(LuceneEvent event) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();

// 初始化抽象类基础参数
this.event = event;
// 初始化目标对象
Expand All @@ -96,6 +102,9 @@ public void flush(LuceneEvent event) {
if (LuceneOperationType.DELETE.equals(this.event.getOperationType())) {
delete();
}

stopWatch.stop();
log.info("[FlushLucene][操作类型:{}][数据类型:{}][目标Id:{}][耗时:{}ms]",event.getOperationType(),event.getDataType(),event.getTargetId(),stopWatch.getTime());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
package com.upupor.service.business.aggregation.service;

import com.upupor.service.business.aggregation.dao.entity.Radio;
import com.upupor.service.dto.OperateRadioDto;
import com.upupor.service.dto.page.common.ListRadioDto;
import com.upupor.service.outer.req.AddRadioReq;
import com.upupor.service.outer.req.DelRadioReq;

import java.util.List;

Expand Down Expand Up @@ -68,4 +71,8 @@ public interface RadioService {
*/
Boolean userHasRadio(String userId);

OperateRadioDto createNewRadio(AddRadioReq addRadioReq);

OperateRadioDto deleteRadio(DelRadioReq delRadioReq);

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -257,20 +258,17 @@ public void bindContentExtend(Content content) {
}

@Override
@Transactional(rollbackFor = Exception.class)
public OperateContentDto addContent(AddContentDetailReq addContentDetailReq) {
return AbstractEditor.execute(abstractEditorList, AbstractEditor.EditorType.CREATE, addContentDetailReq);
}


@Override
@Transactional(rollbackFor = Exception.class)
public OperateContentDto updateContent(UpdateContentReq updateContentReq) {
return AbstractEditor.execute(abstractEditorList, AbstractEditor.EditorType.EDIT, updateContentReq);
}

@Override
@Transactional(rollbackFor = Exception.class)
public OperateContentDto updateContentStatus(UpdateContentReq updateContentReq) {
return AbstractEditor.execute(abstractEditorList, AbstractEditor.EditorType.UPDATE_STATUS, updateContentReq);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

Expand All @@ -73,7 +75,6 @@ public class MemberServiceImpl implements MemberService {
private final MemberMapper memberMapper;
private final MemberExtendMapper memberExtendMapper;
private final MemberIntegralMapper memberIntegralMapper;
private final ContentDataMapper contentDataMapper;
private final MemberIntegralService memberIntegralService;
@Resource
private FanService fanService;
Expand Down Expand Up @@ -328,7 +329,7 @@ public Boolean editMemberBgStyle(UpdateCssReq updateCssReq) {
boolean result = total > 0;
if (result) {
if (!StringUtils.isEmpty(memberExtend.getBgImg())) {
ServletUtils.getSession().setAttribute(CcConstant.Session.USER_BG_IMG, memberExtend.getBgImg());
ServletUtils.getSession().setAttribute(CcConstant.Session.USER_BG_IMG, memberExtend.getBgImg());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,33 @@
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.upupor.framework.utils.CcDateUtil;
import com.upupor.service.business.aggregation.dao.entity.File;
import com.upupor.service.business.aggregation.dao.entity.Member;
import com.upupor.service.business.aggregation.dao.entity.Radio;
import com.upupor.service.business.aggregation.dao.mapper.RadioMapper;
import com.upupor.service.business.aggregation.service.ContentService;
import com.upupor.service.business.aggregation.service.FileService;
import com.upupor.service.business.aggregation.service.MemberService;
import com.upupor.service.business.aggregation.service.RadioService;
import com.upupor.service.common.BusinessException;
import com.upupor.service.common.ErrorCode;
import com.upupor.service.dto.OperateRadioDto;
import com.upupor.service.dto.page.common.ListRadioDto;
import com.upupor.service.outer.req.AddRadioReq;
import com.upupor.service.outer.req.DelRadioReq;
import com.upupor.service.types.RadioStatus;
import com.upupor.service.utils.Asserts;
import com.upupor.service.utils.CcUtils;
import com.upupor.service.utils.ServletUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand All @@ -67,6 +74,8 @@ public class RadioServiceImpl implements RadioService {

private final ContentService contentService;

private final FileService fileService;

@Override
public Boolean addRadio(Radio radio) {

Expand Down Expand Up @@ -204,4 +213,82 @@ public Boolean userHasRadio(String userId) {
return radioMapper.countRadioByUserId(userId) > 0;
}

@Override
public OperateRadioDto createNewRadio(AddRadioReq addRadioReq) {
if (Objects.isNull(addRadioReq.getFileUrl())) {
throw new BusinessException(ErrorCode.PARAM_ERROR, "音频文件为空");
}

if (Objects.isNull(addRadioReq.getRadioIntro())) {
throw new BusinessException(ErrorCode.PARAM_ERROR, "音频简介为空");
}

// 获取用户
Member member = memberService.memberInfo(ServletUtils.getUserId());
if (Objects.isNull(member)) {
throw new BusinessException(ErrorCode.MEMBER_NOT_EXISTS);
}

// 检查文件是否上传成功
File file = fileService.selectByFileUrl(addRadioReq.getFileUrl());
if (Objects.isNull(file)) {
throw new BusinessException(ErrorCode.RADIO_NOT_EXITS_IN_DB);
}

Radio radio = new Radio();
radio.setRadioId(CcUtils.getUuId());
radio.setUserId(member.getUserId());
radio.setRadioIntro(addRadioReq.getRadioIntro());
radio.setRadioUrl(file.getFileUrl());
radio.setContentId(null);
radio.setStatus(RadioStatus.NORMAL);
radio.setCreateTime(CcDateUtil.getCurrentTime());
radio.setLatestCommentTime(CcDateUtil.getCurrentTime());
radio.setSysUpdateTime(new Date());

if (!this.addRadio(radio)) {
throw new BusinessException(ErrorCode.UPLOAD_RADIO_ERROR);
}

// 初始化数据
contentService.initContendData(radio.getRadioId());


OperateRadioDto operateRadioDto = new OperateRadioDto();
operateRadioDto.setRadioId(radio.getRadioId());
operateRadioDto.setSuccess(Boolean.TRUE);
operateRadioDto.setStatus(radio.getStatus());

return operateRadioDto;
}


@Override
public OperateRadioDto deleteRadio(DelRadioReq delRadioReq) {
if (Objects.isNull(delRadioReq) || StringUtils.isEmpty(delRadioReq.getRadioId())) {
throw new BusinessException(ErrorCode.PARAM_ERROR);
}

String userId = ServletUtils.getUserId();

Radio radio = this.getByRadioId(delRadioReq.getRadioId());
if (Objects.isNull(radio)) {
throw new BusinessException(ErrorCode.RADIO_NOT_EXISTS);
}

if (!radio.getUserId().equals(userId)) {
throw new BusinessException(ErrorCode.RADIO_NOT_BELONG_TO_YOU);
}

radio.setStatus(RadioStatus.DELETED);
radio.setSysUpdateTime(new Date());
Boolean success = this.updateRadio(radio) >0;


OperateRadioDto operateRadioDto = new OperateRadioDto();
operateRadioDto.setRadioId(radio.getRadioId());
operateRadioDto.setSuccess(success);
operateRadioDto.setStatus(radio.getStatus());
return operateRadioDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ public abstract class AbstractEditor<T extends BaseContentReq> {
@Resource
protected ApplicationEventPublisher eventPublisher;

@Resource
protected UpuporLuceneService upuporLuceneService;

protected T req;

public T getReq() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ private void recordViewerHistory(String targetId, ViewTargetType targetType, Str
viewHistoryMapper.insert(viewHistory);
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@EventListener
@Async
public void createContent(PublishContentEvent createContentEvent) {
log.info("触发刷新创建新内容事件");

String contentId = createContentEvent.getContentId();
if (StringUtils.isEmpty(contentId)) {
Expand All @@ -135,6 +136,7 @@ public void createContent(PublishContentEvent createContentEvent) {

Content content = contentService.getContentByContentIdNoStatus(contentId);
if (Objects.isNull(content) || !content.getStatus().equals(ContentStatus.NORMAL)) {
log.warn("文章不存在或者状态不正常,不执行后续事件");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

package com.upupor.service.listener;

import com.upupor.lucene.LuceneEvent;
import com.upupor.lucene.AbstractFlush;
import com.upupor.lucene.LuceneEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -55,6 +55,7 @@ public class LuceneListener {
@EventListener
@Async
public void flushLucene(LuceneEvent luceneEvent) {
log.info("触发刷新Lucene事件");
if (StringUtils.isEmpty(luceneEvent.getTargetId())) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -57,6 +58,7 @@
@Aspect
@Component
@RequiredArgsConstructor
@Order(1)
public class ControllerAspectAdvice {
private final ApplicationEventPublisher publisher;
private final List<ControllerAspectChecker> controllerAspectCheckerList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
Expand All @@ -54,6 +55,7 @@
@Aspect
@Component
@RequiredArgsConstructor
@Order(2)
public class LuceneAspectAdvice {
private final ApplicationEventPublisher publisher;

Expand Down
Loading

0 comments on commit d75683a

Please sign in to comment.