diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/SpringBootCallable.java b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/SpringBootCallable.java index 0ad7929f..5ea2884c 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/SpringBootCallable.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/SpringBootCallable.java @@ -250,7 +250,7 @@ public class SpringBootCallable implements Callable { freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "CacheConfig.java"), option + "/java/config/CacheConfig.java", ctx); freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "FreeMarkerConfig.java"), option + "/java/config/FreeMarkerConfig.java", ctx); freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "SecurityConfig.java"), option + "/java/config/SecurityConfig.java", ctx); - freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "TaskConfig.java"), option + "/java/config/TaskConfig.java", ctx); + freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "ScheduleConfig.java"), option + "/java/config/ScheduleConfig.java", ctx); freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "ThreadPoolConfig.java"), option + "/java/config/ThreadPoolConfig.java", ctx); freeMarkerManager.outputTemp(Tool.createFile(root.getAbsolutePath(), "WebMvcConfig.java"), option + "/java/config/WebMvcConfig.java", ctx); if (dataBase.name().equals(DataBase.SQLite)) { diff --git a/src/main/resources/modules/SpringBoot/java/config/ActionConfig.java b/src/main/resources/modules/SpringBoot/java/config/ActionConfig.java index ce80e6a2..dc7ed949 100644 --- a/src/main/resources/modules/SpringBoot/java/config/ActionConfig.java +++ b/src/main/resources/modules/SpringBoot/java/config/ActionConfig.java @@ -1,8 +1,5 @@ package ${basePackage}.config; -import ${basePackage}.frame.base.Control; -import ${basePackage}.frame.base.Screen; -import ${basePackage}.frame.utils.LogUtil; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; @@ -13,7 +10,11 @@ import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; import org.springframework.context.annotation.Configuration; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.core.type.filter.AssignableTypeFilter; import org.springframework.core.type.filter.TypeFilter; +import ${basePackage}.frame.base.Control; +import ${basePackage}.frame.base.Screen; +import ${basePackage}.frame.utils.LogUtil; import java.io.IOException; import java.util.regex.Matcher; @@ -58,15 +59,7 @@ public class ActionConfig implements BeanDefinitionRegistryPostProcessor { private int registryScreen(String basePackage, BeanDefinitionRegistry beanDefinitionRegistry) { ClassPathBeanDefinitionScanner classPathBeanDefinitionScanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry); - classPathBeanDefinitionScanner.addIncludeFilter(new TypeFilter() { - @Override - public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { - if (metadataReader.getClassMetadata().getSuperClassName().equals(Screen.class.getName())) { - return true; - } - return false; - } - }); + classPathBeanDefinitionScanner.addIncludeFilter(new AssignableTypeFilter(Screen.class)); classPathBeanDefinitionScanner.setBeanNameGenerator(new BeanNameGenerator() { @Override public String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry beanDefinitionRegistry) { @@ -81,15 +74,7 @@ public class ActionConfig implements BeanDefinitionRegistryPostProcessor { private int registryControl(String basePackage, BeanDefinitionRegistry beanDefinitionRegistry) { ClassPathBeanDefinitionScanner classPathBeanDefinitionScanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry); - classPathBeanDefinitionScanner.addIncludeFilter(new TypeFilter() { - @Override - public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { - if (metadataReader.getClassMetadata().getSuperClassName().equals(Control.class.getName())) { - return true; - } - return false; - } - }); + classPathBeanDefinitionScanner.addIncludeFilter(new AssignableTypeFilter(Control.class)); classPathBeanDefinitionScanner.setBeanNameGenerator(new BeanNameGenerator() { @Override public String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry beanDefinitionRegistry) { diff --git a/src/main/resources/modules/SpringBoot/java/config/ScheduleConfig.java b/src/main/resources/modules/SpringBoot/java/config/ScheduleConfig.java new file mode 100644 index 00000000..87d6d28a --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/config/ScheduleConfig.java @@ -0,0 +1,83 @@ +package ${basePackage}.config; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; +import org.springframework.beans.factory.support.BeanNameGenerator; +import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.core.type.filter.AssignableTypeFilter; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.stereotype.Component; +import ${basePackage}.frame.schedule.RunTask; +import ${basePackage}.frame.utils.LogUtil; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ScheduledFuture; + +/** + * 计划调度配置,可以指定环境生效,根据实际情况是否需要开启 + * 开启方法:将@Profile("dev") 删除(所有环境生效) 或改为对应环境 + */ +@Configuration +@Component +@Profile("dev") +public class ScheduleConfig extends ThreadPoolTaskScheduler implements BeanDefinitionRegistryPostProcessor { + + private Map> classMap; + + private Map> futureMap; + + public ScheduleConfig() { + classMap = new HashMap<>(); + futureMap = new HashMap<>(); + setPoolSize(4); + initialize(); + } + + public boolean createOrRepeat(RunTask task) { + if (futureMap.containsKey(task.taskId())){ + ScheduledFuture scheduledFuture = futureMap.get(task.taskId()); + scheduledFuture.cancel(false); + } + classMap.put(task.taskId(), (Class) task.getClass()); + futureMap.put(task.taskId(), task.schedule(this)); + return true; + } + + @Override + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException { + registryTask("${basePackage}.task", beanDefinitionRegistry); + } + + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { + + } + + private int registryTask(String basePackage, BeanDefinitionRegistry beanDefinitionRegistry) { + ClassPathBeanDefinitionScanner classPathBeanDefinitionScanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry); + classPathBeanDefinitionScanner.addIncludeFilter(new AssignableTypeFilter(RunTask.class)); + classPathBeanDefinitionScanner.setBeanNameGenerator(new BeanNameGenerator() { + @Override + public String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry beanDefinitionRegistry) { + String beanClassName = beanDefinition.getBeanClassName(); + try { + Class aClass = Class.forName(beanClassName); + Object instance = aClass.newInstance(); + RunTask task = (RunTask) instance; + ScheduleConfig.this.createOrRepeat(task); + } catch (Exception e) { + e.printStackTrace(); + } + LogUtil.i("registry task " + beanClassName); + return beanClassName; + } + }); + return classPathBeanDefinitionScanner.scan(basePackage); + } +} diff --git a/src/main/resources/modules/SpringBoot/java/config/TaskConfig.java b/src/main/resources/modules/SpringBoot/java/config/TaskConfig.java deleted file mode 100644 index 8bf11609..00000000 --- a/src/main/resources/modules/SpringBoot/java/config/TaskConfig.java +++ /dev/null @@ -1,81 +0,0 @@ -package ${basePackage}.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; -import org.springframework.scheduling.Trigger; -import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; -import org.springframework.scheduling.support.CronTrigger; -import org.springframework.stereotype.Component; -import ${basePackage}.frame.utils.LogUtil; - -import javax.annotation.PostConstruct; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ScheduledFuture; - -/** - * Task配置,默认只在开发环境生效,根据实际情况是否需要Task选择开启 - * 开启方法:将@Profile("dev") 删除(所有环境生效) 或改为 (只对生产环境生效) - */ -@Configuration -@Component -@Profile("dev") -public class TaskConfig extends ThreadPoolTaskScheduler { - - public TaskConfig() { - futureMap = new HashMap<>(); - setPoolSize(4); - initialize(); - } - - @PostConstruct - public void ss() { - TaskConfig.this.addTask(1L, new Runnable() { - @Override - public void run() { - SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); - LogUtil.d(format.format(new Date())); - } - }, new CronTrigger("0/3 * * * * ? ")); - } - - private Map> futureMap; - - - public boolean addTask(Long taskId, Runnable runnable, Trigger trigger) { - ScheduledFuture schedule = this.schedule(runnable, trigger); - futureMap.put(taskId.toString(), schedule); - System.out.println("DynamicTask.startCron()"); - return true; - } - -// public void configureTasks(ScheduledTaskRegistrar registrar) { -// // 开启新线程模拟外部更改了任务执行周期 -// new Thread(new Runnable() { -// @Override -// public void run() { -// try { -// Thread.sleep(15 * 1000); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// -// cron = "0/10 * * * * ? "; -// System.err.println("cron change to: " + cron); -// } -// }).start(); - -// this.registrar = registrar; -// this.registrar.setScheduler(Executors.newScheduledThreadPool(3)); -// CronTrigger cronTrigger = new CronTrigger(cron) { -//// @Override -//// public Date nextExecutionTime(TriggerContext triggerContext) { -//// // 任务触发,可修改任务的执行周期 -//// CronTrigger trigger = new CronTrigger(cron); -//// return trigger.nextExecutionTime(triggerContext); -//// } -// }; -// } -} diff --git a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunCronTask.java b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunCronTask.java index 9c324a09..edbb08fa 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunCronTask.java +++ b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunCronTask.java @@ -1,14 +1,18 @@ -package ${basePackage}.frame.schedule; +package xyz.wbsite.frame.schedule; -import org.springframework.scheduling.config.CronTask; -import org.springframework.scheduling.config.Task; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.scheduling.support.CronTrigger; +import org.springframework.util.Assert; + +import java.util.concurrent.ScheduledFuture; public abstract class RunCronTask extends RunTask { abstract String cron(); @Override - public Task build() { - return new CronTask(this, cron()); + public ScheduledFuture schedule(ThreadPoolTaskScheduler poolTaskScheduler) { + Assert.notNull(poolTaskScheduler, "ThreadPoolTaskScheduler must not be null"); + return poolTaskScheduler.schedule(this, new CronTrigger(cron())); } } diff --git a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunDelayRepeatTask.java b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunDelayRepeatTask.java new file mode 100644 index 00000000..f52c639a --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunDelayRepeatTask.java @@ -0,0 +1,18 @@ +package xyz.wbsite.frame.schedule; + +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.util.Assert; + +import java.time.Duration; +import java.util.concurrent.ScheduledFuture; + +public abstract class RunDelayRepeatTask extends RunTask { + + public abstract Duration interval(); + + @Override + public ScheduledFuture schedule(ThreadPoolTaskScheduler poolTaskScheduler) { + Assert.notNull(poolTaskScheduler, "ThreadPoolTaskScheduler must not be null"); + return poolTaskScheduler.scheduleWithFixedDelay(this, interval()); + } +} diff --git a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunFixRepeatTask.java b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunFixRepeatTask.java new file mode 100644 index 00000000..1abf7e69 --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunFixRepeatTask.java @@ -0,0 +1,18 @@ +package xyz.wbsite.frame.schedule; + +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.util.Assert; + +import java.time.Duration; +import java.util.concurrent.ScheduledFuture; + +public abstract class RunFixRepeatTask extends RunTask { + + public abstract Duration interval(); + + @Override + public ScheduledFuture schedule(ThreadPoolTaskScheduler poolTaskScheduler) { + Assert.notNull(poolTaskScheduler, "ThreadPoolTaskScheduler must not be null"); + return poolTaskScheduler.scheduleAtFixedRate(this, interval()); + } +} diff --git a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunRepeatTask.java b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunRepeatTask.java deleted file mode 100644 index d898ee83..00000000 --- a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunRepeatTask.java +++ /dev/null @@ -1,28 +0,0 @@ -package ${basePackage}.frame.schedule; - -import org.springframework.scheduling.config.FixedDelayTask; -import org.springframework.scheduling.config.FixedRateTask; -import org.springframework.scheduling.config.Task; - -public abstract class RunRepeatTask extends RunTask { - - public abstract void config(Config config); - - public class Config { - int interval = 10 * 60 * 1000; - int initialDelay = 0; - boolean fix = false; - } - - @Override - public Task build() { - Config config = new Config(); - config(config); - - if (config.fix) { - return new FixedRateTask(this, config.interval, config.initialDelay); - } else { - return new FixedDelayTask(this, config.interval, config.initialDelay); - } - } -} diff --git a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunSqlTask.java b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunSqlTask.java index e6ccea67..1582ab6f 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunSqlTask.java +++ b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunSqlTask.java @@ -1,14 +1,9 @@ -package ${basePackage}.frame.schedule; +package xyz.wbsite.frame.schedule; -public abstract class RunSqlTask extends RunRepeatTask { +public abstract class RunSqlTask extends RunFixRepeatTask { public abstract String getSql(); - @Override - public void config(Config config) { - - } - @Override public void run() { //exec sql diff --git a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunTask.java b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunTask.java index 036e88f6..897dea69 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunTask.java +++ b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunTask.java @@ -1,8 +1,7 @@ -package ${basePackage}.frame.schedule; +package xyz.wbsite.frame.schedule; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; -import org.springframework.scheduling.config.Task; import java.util.Date; import java.util.concurrent.ScheduledFuture; @@ -11,7 +10,7 @@ public abstract class RunTask implements Runnable { public abstract String taskId(); - public abstract Task build(); + public abstract ScheduledFuture schedule(ThreadPoolTaskScheduler poolTaskScheduler); public void configChange(ThreadPoolTaskScheduler scheduler) { ScheduledFuture schedule = scheduler.schedule(this, new Date());