/* * Copyright (c) 2021-2025, LXQ All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the com developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: LXQ (609827073@qq.com) */ package com.lxq.daemon.quartz.util; import cn.hutool.core.util.StrUtil; import com.lxq.daemon.quartz.constants.SkyQuartzEnum; import com.lxq.daemon.quartz.exception.TaskException; import com.lxq.daemon.quartz.entity.SysJob; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * 定时任务java class反射实现 * * @author LXQ */ @Component("javaClassTaskInvoke") @Slf4j public class JavaClassTaskInvoke implements ITaskInvoke { @Override public void invokeMethod(SysJob sysJob) throws TaskException { Object obj; Class clazz; Method method; Object returnValue; try { if (StrUtil.isNotEmpty(sysJob.getMethodParamsValue())) { clazz = Class.forName(sysJob.getClassName()); obj = clazz.newInstance(); method = clazz.getDeclaredMethod(sysJob.getMethodName(), String.class); returnValue = method.invoke(obj, sysJob.getMethodParamsValue()); } else { clazz = Class.forName(sysJob.getClassName()); obj = clazz.newInstance(); method = clazz.getDeclaredMethod(sysJob.getMethodName()); returnValue = method.invoke(obj); } if (StrUtil.isEmpty(returnValue.toString()) || SkyQuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(returnValue.toString())) { log.error("定时任务javaClassTaskInvok异常,执行任务:{}", sysJob.getClassName()); throw new TaskException("定时任务javaClassTaskInvok业务执行失败,任务:" + sysJob.getClassName()); } } catch (ClassNotFoundException e) { log.error("定时任务java反射类没有找到,执行任务:{}", sysJob.getClassName()); throw new TaskException("定时任务java反射类没有找到,执行任务:" + sysJob.getClassName()); } catch (IllegalAccessException e) { log.error("定时任务java反射类异常,执行任务:{}", sysJob.getClassName()); throw new TaskException("定时任务java反射类异常,执行任务:" + sysJob.getClassName()); } catch (InstantiationException e) { log.error("定时任务java反射类异常,执行任务:{}", sysJob.getClassName()); throw new TaskException("定时任务java反射类异常,执行任务:" + sysJob.getClassName()); } catch (NoSuchMethodException e) { log.error("定时任务java反射执行方法名异常,执行任务:{}", sysJob.getClassName()); throw new TaskException("定时任务java反射执行方法名异常,执行任务:" + sysJob.getClassName()); } catch (InvocationTargetException e) { log.error("定时任务java反射执行异常,执行任务:{}", sysJob.getClassName()); throw new TaskException("定时任务java反射执行异常,执行任务:" + sysJob.getClassName()); } } }