public class QuartzJobServiceImpl implements IQuartzJobService {
@Autowired
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
private ScheduledFuture<?> future;
public static ConcurrentHashMap<String, ScheduledFuture> map = new ConcurrentHashMap<String, ScheduledFuture>();
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
return new ThreadPoolTaskScheduler();
}
public void startCron(String jobClassName, String cronExpression) {
try {
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(sra, true);
Class c1 = Class.forName(jobClassName);
Object o = c1.newInstance();
future = threadPoolTaskScheduler.schedule((Runnable) o, new org.springframework.scheduling.support.CronTrigger(cronExpression));
map.put(jobClassName,future);
System.out.println("DynamicTaskController.startCron()");
}catch (Exception e) {
throw new JeecgBootException("后台找不到该类名:" + jobClassName, e);
}
}
@Override
public void pause(String jobClassName) {
ScheduledFuture scheduledFuture = map.get(jobClassName);
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
}
}
private void TaskDelete(String jobClassName) {
try {
pause(jobClassName);
map.remove(jobClassName);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new JeecgBootException("删除定时任务失败");
}
}
@Override
public void editAndScheduleJob(QuartzJob quartzJob) throws SchedulerException {
if (quartzJob.getJobClassNameData().trim().equals(quartzJob.getJobClassName().trim()) {
pause(quartzJob.getJobClassName().trim());
startCron(quartzJob.getJobClassName().trim(), quartzJob.getCronExpression().trim(), quartzJob.getParameter());
}else {
pause(quartzJob.getJobClassNameData().trim());
map.remove(quartzJob.getJobClassNameData().trim());
startCron(quartzJob.getJobClassName().trim(), quartzJob.getCronExpression().trim(), quartzJob.getParameter());
}
}
}
package cn.com.skyvis.hlwft.controller;
import cn.com.skyvis.hlwft.service.GroupDemoFeignService;
import lombok.extern.log4j.Log4j2;
import org.jeecg.common.util.SpringContextUtils;
import java.util.Date;
public class MyRunnable implements Runnable {
public void run() {
GroupDemoFeignService bean = SpringContextUtils.getBean(GroupDemoFeignService.class);
bean.SynchronousData();
}
}
@Component
public class SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
@Component
public class ApplicationTask implements ApplicationRunner {
@Autowired
private IQuartzJobService quartzJobService;
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
quartzJobService.startCron("cn.com.skyvis.hlwft.controller.MyRunnable","1/5 * * * * ?",null);
System.out.println("执行开机启动项目");
}
}