java捕获定时器抛出的异常_如何aop监控spring 定时器的异常

展开全部

package com.tiamaes.gjds.dxp.aop;import java.util.Date;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.springframework.beans.factory.annotation.Autowired;import com.tiamaes.gjds.dxp.annotation.Task;import com.tiamaes.gjds.dxp.bean.TbScheduledExcuteLog;import com.tiamaes.gjds.dxp.repository.TbScheduledExcuteLogRepository;import com.tiamaes.gjds.dxp.task.DxpScheduled;/**

@Aspect

public class ScheduledStatisticsHandler {

@Autowired

private TbScheduledExcuteLogRepository tbScheduledExcuteLogRepository;

@Pointcut("@annotation(org.springframework.scheduling.annotation.Scheduled)")

public void proxyAspect() {

}

@Around("proxyAspect()")

public Object doInvoke(ProceedingJoinPoint joinPoint) throws Throwable{

Date date = new Date();

long start = System.currentTimeMillis();

Object result = joinPoint.proceed();

long end = System.currentTimeMillis();

Object target = joinPoint.getTarget();

TbScheduledExcuteLog log = new TbScheduledExcuteLog();

log.setClassName(joinPoint.getTarget().getClass().getName());

log.setConsum(end-start);

log.setExcuteDate(date);

log.setExcuteTime(date);

log.setIsError(false);

if (target instanceof DxpScheduled) {

DxpScheduled scheduled = (DxpScheduled) target;

Task task = scheduled.getClass().getAnnotation(Task.class);

log.setContentName(task.value());

log.setRemark(scheduled.getTaskExcuteInfo());

log.setGetRecCount(scheduled.getRemoteCount());

log.setSyncRecCount(scheduled.getSyncCount());

}

this.tbScheduledExcuteLogRepository.save(log);

return result;

}

}

这个并不难,主要是知道注入的位62616964757a686964616fe59b9ee7ad9431333363373665置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring 与 Mybatis 整合后,可以通过 AOP 切面来捕获 Mybatis 执行 SQL 时的异常。 具体步骤如下: 1. 创建一个切面类,使用 @Aspect 注解标注为切面类,并在类中定义一个方法用于捕获异常。 2. 在切面方法上使用 @AfterThrowing 注解指定需要捕获异常类型和处理方法。 3. 在 Spring 配置文件中配置切面类和异常处理方法。 以下是示例代码: ```java @Aspect public class SqlExceptionAspect { // 定义切入点,这里选择 Mybatis 的 Executor 类 @Pointcut("execution(* org.apache.ibatis.executor.Executor.*(..))") public void pointcutExecutor() {} // 异常处理方法 @AfterThrowing(pointcut = "pointcutExecutor()", throwing = "ex") public void handleSqlException(Exception ex) { if (ex instanceof SQLException) { // 处理 SQL 异常 System.out.println("捕获到 SQL 异常:" + ex.getMessage()); } else { // 处理其他异常 System.out.println("捕获到其他异常:" + ex.getMessage()); } } } ``` 在 Spring 配置文件中添加以下代码: ```xml <!-- 配置切面类 --> <bean id="sqlExceptionAspect" class="com.example.SqlExceptionAspect"/> <!-- 配置 AOP 自动代理 --> <aop:aspectj-autoproxy/> <!-- 配置切面 --> <aop:config> <aop:aspect ref="sqlExceptionAspect"> <aop:after-throwing method="handleSqlException" throwing="ex" pointcut="execution(* org.apache.ibatis.executor.Executor.*(..))"/> </aop:aspect> </aop:config> ``` 这样就可以在执行 Mybatis SQL 时捕获异常了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值