Former-commit-id: 556e8644433e5d4adac078fee7aae4ea9654a781
master
wangbing 5 years ago
parent 018e27b6e1
commit 8e57ac8679

@ -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)) {

@ -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) {

@ -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<String, Class<RunTask>> classMap;
private Map<String, ScheduledFuture<?>> 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<RunTask>) 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);
}
}

@ -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<String, ScheduledFuture<?>> 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);
//// }
// };
// }
}

@ -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()));
}
}

@ -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());
}
}

@ -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());
}
}

@ -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);
}
}
}

@ -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

@ -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());

Loading…
Cancel
Save

Powered by TurnKey Linux.