Spring Boot 项目中如何使用异步任务(案例)

前置知识:

Spring Boot 项目中如何使用异步任务-CSDN博客

通过AOP和Async,将日志信息异步的记录到数据库中

步骤:

  1. 添加依赖

    <!--aop-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
  2. 创建日志切面

/**
 *
 * 操作日志切面
 */
@Component
@Aspect
public class OperLogAspect {

    @Resource
    private OperLogService operLogService;

    @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)" +
            "||@annotation(org.springframework.web.bind.annotation.PutMapping)" +
            "||@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
    public void pc() {
    }

    @Around("pc()")
    public Object around(ProceedingJoinPoint joinPoint) {
        HttpServletRequest request = ServletUtil.getRequest();
        int businessType = 0;
        switch (request.getMethod()) {
            case "POST":
                businessType = 1;
                break;
            case "PUT":
                businessType = 2;
                break;
            case "DELETE":
                businessType = 3;
                break;
        }

        OperLog operLog = new OperLog();
        operLog.setTitle(request.getRequestURI().split("/")[4]);
        operLog.setBusinessType(businessType);
        operLog.setMethod(joinPoint.getSignature().getName());
        operLog.setRequestMethod(request.getMethod());
        operLog.setOperatorType(1);
        operLog.setOperName("admin"); // 通过token获取用户名
        operLog.setRoleName("管理员"); // 通过token获取用户id,然后获取用户的角色
        operLog.setOperUrl(request.getRequestURL().toString());
        operLog.setOperIp(AddressUtil.getIpAddr(request));
        operLog.setOperLocation(AddressUtil.getLocationByIp(operLog.getOperIp()));
        operLog.setOperParam(Arrays.toString(joinPoint.getArgs()));
        operLog.setOperTime(LocalDateTime.now());

        Long start = System.currentTimeMillis();
        try {
            // 核心业务
            Object res = joinPoint.proceed();
            operLog.setJsonResult(JSONUtil.toJsonStr(res));
            operLog.setStatus(0);

            return res;
        } catch (Throwable e) {
            operLog.setJsonResult(JSONUtil.toJsonStr(AjaxResult.error(e.getMessage())));
            operLog.setStatus(1);
            operLog.setErrorMsg(e.getMessage());
            throw new RuntimeException(e.getMessage());
        }finally {
            Long end = System.currentTimeMillis();
            operLog.setCostTime(end-start);

            // 记录操作日志
            operLogService.add(operLog);
        }
    }

}

        3.创建日志服务相关接口(entity、mapper、service)

@Service
@Transactional(rollbackFor = Exception.class)
public class OperLogServiceImpl implements OperLogService {

  	@Resource
    private OperLogMapper operLogMapper;

    /**
     * 异步记录操作日志
     */
    @Async
    @Override
    public void add(OperLog operLog) {
        operLogMapper.insert(operLog);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值