From 760e26d192bc65ab2cb8e416374090c7435abf18 Mon Sep 17 00:00:00 2001 From: APLS Date: Sun, 15 Mar 2020 23:44:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=20quartz=20=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8rabbitMq?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 19 +-- .../io/qyi/e5/config/quartz/JobFactory.java | 28 ---- .../qyi/e5/config/quartz/SchedulerConfig.java | 63 -------- .../io/qyi/e5/config/redis/RedisConfig.java | 79 +++------ .../controller/quartz/QuartzController.java | 53 ------ .../io/qyi/e5/service/quartz/JobUtil.java | 152 ------------------ .../e5/service/quartz/MyQuartzJobService.java | 38 ----- 7 files changed, 24 insertions(+), 408 deletions(-) delete mode 100644 src/main/java/io/qyi/e5/config/quartz/JobFactory.java delete mode 100644 src/main/java/io/qyi/e5/config/quartz/SchedulerConfig.java delete mode 100644 src/main/java/io/qyi/e5/controller/quartz/QuartzController.java delete mode 100644 src/main/java/io/qyi/e5/service/quartz/JobUtil.java delete mode 100644 src/main/java/io/qyi/e5/service/quartz/MyQuartzJobService.java diff --git a/pom.xml b/pom.xml index 43c73a8..a916eae 100644 --- a/pom.xml +++ b/pom.xml @@ -143,22 +143,11 @@ RELEASE compile - - + - org.quartz-scheduler - quartz - 2.2.1 - - - org.quartz-scheduler - quartz-jobs - 2.2.1 - - - - org.springframework.boot - spring-boot-starter-mail + org.springframework.amqp + spring-rabbit + diff --git a/src/main/java/io/qyi/e5/config/quartz/JobFactory.java b/src/main/java/io/qyi/e5/config/quartz/JobFactory.java deleted file mode 100644 index 41bcf61..0000000 --- a/src/main/java/io/qyi/e5/config/quartz/JobFactory.java +++ /dev/null @@ -1,28 +0,0 @@ -package io.qyi.e5.config.quartz; - -import org.quartz.spi.TriggerFiredBundle; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.AutowireCapableBeanFactory; -import org.springframework.scheduling.quartz.AdaptableJobFactory; -import org.springframework.stereotype.Component; - -/** - * @program: e5 - * @description: - * @author: 落叶随风 - * @create: 2020-03-03 16:14 - **/ -@Component -public class JobFactory extends AdaptableJobFactory { - @Autowired - private AutowireCapableBeanFactory capableBeanFactory; - - @Override - protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { - //调用父类的方法 - Object jobInstance = super.createJobInstance(bundle); - //进行注入 - capableBeanFactory.autowireBean(jobInstance); - return jobInstance; - } -} diff --git a/src/main/java/io/qyi/e5/config/quartz/SchedulerConfig.java b/src/main/java/io/qyi/e5/config/quartz/SchedulerConfig.java deleted file mode 100644 index d460952..0000000 --- a/src/main/java/io/qyi/e5/config/quartz/SchedulerConfig.java +++ /dev/null @@ -1,63 +0,0 @@ -package io.qyi.e5.config.quartz; - -import org.quartz.Scheduler; -import org.quartz.ee.servlet.QuartzInitializerListener; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.PropertiesFactoryBean; -import org.springframework.context.ApplicationListener; -import org.springframework.context.annotation.Bean; -import org.springframework.context.event.ContextRefreshedEvent; -import org.springframework.core.io.ClassPathResource; -import org.springframework.scheduling.quartz.SchedulerFactoryBean; - -import java.io.IOException; - -/** - * @program: e5 - * @description: - * @author: 落叶随风 - * @create: 2020-03-03 16:16 - **/ -public class SchedulerConfig implements ApplicationListener { - - @Autowired - private JobFactory jobFactory; - - @Override - public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { - System.out.println("任务已经启动..." + contextRefreshedEvent.getSource()); - } - @Bean - public SchedulerFactoryBean schedulerFactoryBean() throws IOException { - //获取配置属性 - PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); - propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties")); - //在quartz.properties中的属性被读取并注入后再初始化对象 - propertiesFactoryBean.afterPropertiesSet(); - //创建SchedulerFactoryBean - SchedulerFactoryBean factory = new SchedulerFactoryBean(); - factory.setQuartzProperties(propertiesFactoryBean.getObject()); - //使用数据源,自定义数据源 -// factory.setDataSource(this.primaryDataSource); - factory.setJobFactory(jobFactory); - factory.setWaitForJobsToCompleteOnShutdown(true);//这样当spring关闭时,会等待所有已经启动的quartz job结束后spring才能完全shutdown。 - factory.setOverwriteExistingJobs(false); - factory.setStartupDelay(1); - return factory; - } - - - /* - * 通过SchedulerFactoryBean获取Scheduler的实例 - */ - @Bean(name="scheduler") - public Scheduler scheduler() throws IOException { - return schedulerFactoryBean().getScheduler(); - } - - - @Bean - public QuartzInitializerListener executorListener() { - return new QuartzInitializerListener(); - } -} diff --git a/src/main/java/io/qyi/e5/config/redis/RedisConfig.java b/src/main/java/io/qyi/e5/config/redis/RedisConfig.java index 08cdeeb..cf3744f 100644 --- a/src/main/java/io/qyi/e5/config/redis/RedisConfig.java +++ b/src/main/java/io/qyi/e5/config/redis/RedisConfig.java @@ -3,12 +3,9 @@ package io.qyi.e5.config.redis; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; @@ -17,41 +14,34 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; -import java.lang.reflect.Method; - /** * @author JiaweiWu * @description: redis配置 */ @Configuration //@AutoConfigureAfter(RedisAutoConfiguration.class) -//@EnableCaching +@EnableCaching public class RedisConfig extends CachingConfigurerSupport { - /** - * 自定义生成key的规则 - */ - /* @Override - public KeyGenerator keyGenerator() { - return new KeyGenerator() { - @Override - public Object generate(Object o, Method method, Object... objects) { - //格式化缓存key字符串 - StringBuilder sb = new StringBuilder(); - //追加类名 - sb.append(o.getClass().getName()); - //追加方法名 - sb.append(method.getName()); - //遍历参数并且追加 - for (Object obj : objects) { - sb.append(obj.toString()); - } - System.out.println("调用Redis缓存Key : " + sb.toString()); - return sb.toString(); - } - }; - } -*/ + + /*@Bean + RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory + ,MessageListenerAdapter listenerAdapter + ,MessageListenerAdapter catAdapter) { + RedisMessageListenerContainer container = new RedisMessageListenerContainer(); + container.setConnectionFactory(connectionFactory); + //订阅了一个叫chat 的通道 + container.addMessageListener(listenerAdapter, new PatternTopic("__keyevent@1__:expired")); + + container.addMessageListener(catAdapter, new PatternTopic("__keyevent@1__:expired")); + return container; + }*/ + + /* @Bean + MessageListenerAdapter catAdapter() { + return new MessageListenerAdapter(new FishListener()); + }*/ + /** * 采用RedisCacheManager作为缓存管理器 * @@ -65,34 +55,6 @@ public class RedisConfig extends CachingConfigurerSupport { return redisCacheManager; } - /** - * 配置自定义redisTemplate - * @return - */ - /*@Bean - RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { - - RedisTemplate template = new RedisTemplate<>(); - template.setConnectionFactory(redisConnectionFactory); - - //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 - Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); - - ObjectMapper mapper = new ObjectMapper(); - mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); - mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); - serializer.setObjectMapper(mapper); - - template.setValueSerializer(serializer); - //使用StringRedisSerializer来序列化和反序列化redis的key值 - template.setKeySerializer(new StringRedisSerializer()); - template.setHashKeySerializer(new StringRedisSerializer()); - template.setHashValueSerializer(serializer); - template.afterPropertiesSet(); - return template; - }*/ - - @Bean @SuppressWarnings("all") public RedisTemplate redisTemplate(RedisConnectionFactory factory) { @@ -116,5 +78,4 @@ public class RedisConfig extends CachingConfigurerSupport { return template; } - } diff --git a/src/main/java/io/qyi/e5/controller/quartz/QuartzController.java b/src/main/java/io/qyi/e5/controller/quartz/QuartzController.java deleted file mode 100644 index 0850ab8..0000000 --- a/src/main/java/io/qyi/e5/controller/quartz/QuartzController.java +++ /dev/null @@ -1,53 +0,0 @@ -package io.qyi.e5.controller.quartz; - -import io.qyi.e5.bean.AppQuartz; -import io.qyi.e5.service.quartz.JobUtil; -import io.qyi.e5.service.quartz.MyQuartzJobService; -import org.quartz.JobDataMap; -import org.quartz.JobKey; -import org.quartz.TriggerKey; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; - -/** - * @program: e5 - * @description: - * @author: 落叶随风 - * @create: 2020-03-03 14:59 - **/ -@Controller -@RequestMapping("/quartz") -public class QuartzController { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - @Value("${qz_cron}") - private String qz_cron; - - @Autowired - private JobUtil jobUtil; - - @GetMapping("/add") - public void add() throws Exception { - logger.info("添加定时任务"); - JobDataMap jobDataMap = new JobDataMap(); - jobDataMap.put("aaa", "test"); - JobKey jobKey = new JobKey("crom任务", "我的cron任务组名1"); - TriggerKey triggerKey1 = new TriggerKey("我的cron触发器名1", "我的cron触发器组名1"); - - AppQuartz appQuartz = new AppQuartz(); - appQuartz.setJobGroup("t1"); - appQuartz.setJobName("t1"); - appQuartz.setQuartzId(1); - appQuartz.setCronExpression(qz_cron); - appQuartz.setStartTime("2020-03-03 16:03:11"); - jobUtil.addJob(appQuartz,MyQuartzJobService.class); - -// quartzManager.addJob(jobKey,triggerKey1, MyQuartzJobService.class, "0/20 * * * * ?", jobDataMap); - - } -} diff --git a/src/main/java/io/qyi/e5/service/quartz/JobUtil.java b/src/main/java/io/qyi/e5/service/quartz/JobUtil.java deleted file mode 100644 index 7e5d8be..0000000 --- a/src/main/java/io/qyi/e5/service/quartz/JobUtil.java +++ /dev/null @@ -1,152 +0,0 @@ -package io.qyi.e5.service.quartz; - -import io.qyi.e5.bean.AppQuartz; -import org.quartz.*; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; - -import java.text.SimpleDateFormat; -import java.util.Date; - -/** - * @program: e5 - * @description: - * @author: 落叶随风 - * @create: 2020-03-03 16:18 - **/ -@Service -public class JobUtil { - @Autowired - private Scheduler scheduler; - - - /** - * 新建一个任务 - */ - public String addJob(AppQuartz appQuartz, Class aClass) throws Exception { - - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); -// Date date = df.parse(appQuartz.getStartTime()); - - if (!CronExpression.isValidExpression(appQuartz.getCronExpression())) { - return "Illegal cron expression"; //表达式格式不正确 - } -// JobDetail jobDetail = null; - //构建job信息 - /*if("JobOne".equals(appQuartz.getJobGroup())) { - jobDetail = JobBuilder.newJob(JobOne.class).withIdentity(appQuartz.getJobName(), appQuartz.getJobGroup()).build(); - } - if("JobTwo".equals(appQuartz.getJobGroup())) { - jobDetail = JobBuilder.newJob(JobTwo.class).withIdentity(appQuartz.getJobName(), appQuartz.getJobGroup()).build(); - }*/ - JobDetail jobDetail = JobBuilder.newJob(aClass).withIdentity(appQuartz.getJobName(), appQuartz.getJobGroup()).build(); - - //表达式调度构建器(即任务执行的时间,不立即执行) - CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(appQuartz.getCronExpression()).withMisfireHandlingInstructionDoNothing(); - - //按新的cronExpression表达式构建一个新的trigger - CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(appQuartz.getJobName(), appQuartz.getJobGroup()).startNow() - .withSchedule(scheduleBuilder).build(); - - //传递参数 - if (appQuartz.getInvokeParam() != null && !"".equals(appQuartz.getInvokeParam())) { - trigger.getJobDataMap().put("invokeParam", appQuartz.getInvokeParam()); - } - scheduler.scheduleJob(jobDetail, trigger); - // pauseJob(appQuartz.getJobName(),appQuartz.getJobGroup()); - return "success"; - } - - /** - * 获取Job状态 - * - * @param jobName - * @param jobGroup - * @return - * @throws SchedulerException - */ - public String getJobState(String jobName, String jobGroup) throws SchedulerException { - TriggerKey triggerKey = new TriggerKey(jobName, jobGroup); - return scheduler.getTriggerState(triggerKey).name(); - } - - //暂停所有任务 - public void pauseAllJob() throws SchedulerException { - scheduler.pauseAll(); - } - - //暂停任务 - public String pauseJob(String jobName, String jobGroup) throws SchedulerException { - JobKey jobKey = new JobKey(jobName, jobGroup); - JobDetail jobDetail = scheduler.getJobDetail(jobKey); - if (jobDetail == null) { - return "fail"; - } else { - scheduler.pauseJob(jobKey); - return "success"; - } - - } - - //恢复所有任务 - public void resumeAllJob() throws SchedulerException { - scheduler.resumeAll(); - } - - // 恢复某个任务 - public String resumeJob(String jobName, String jobGroup) throws SchedulerException { - - JobKey jobKey = new JobKey(jobName, jobGroup); - JobDetail jobDetail = scheduler.getJobDetail(jobKey); - if (jobDetail == null) { - return "fail"; - } else { - scheduler.resumeJob(jobKey); - return "success"; - } - } - - //删除某个任务 - public String deleteJob(AppQuartz appQuartz) throws SchedulerException { - JobKey jobKey = new JobKey(appQuartz.getJobName(), appQuartz.getJobGroup()); - JobDetail jobDetail = scheduler.getJobDetail(jobKey); - if (jobDetail == null) { - return "jobDetail is null"; - } else if (!scheduler.checkExists(jobKey)) { - return "jobKey is not exists"; - } else { - scheduler.deleteJob(jobKey); - return "success"; - } - - } - - //修改任务 - public String modifyJob(AppQuartz appQuartz) throws SchedulerException { - if (!CronExpression.isValidExpression(appQuartz.getCronExpression())) { - return "Illegal cron expression"; - } - TriggerKey triggerKey = TriggerKey.triggerKey(appQuartz.getJobName(), appQuartz.getJobGroup()); - JobKey jobKey = new JobKey(appQuartz.getJobName(), appQuartz.getJobGroup()); - if (scheduler.checkExists(jobKey) && scheduler.checkExists(triggerKey)) { - CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); - //表达式调度构建器,不立即执行 - CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(appQuartz.getCronExpression()).withMisfireHandlingInstructionDoNothing(); - //按新的cronExpression表达式重新构建trigger - trigger = trigger.getTriggerBuilder().withIdentity(triggerKey) - .withSchedule(scheduleBuilder).build(); - //修改参数 - if (!trigger.getJobDataMap().get("invokeParam").equals(appQuartz.getInvokeParam())) { - trigger.getJobDataMap().put("invokeParam", appQuartz.getInvokeParam()); - } - //按新的trigger重新设置job执行 - scheduler.rescheduleJob(triggerKey, trigger); - return "success"; - } else { - return "job or trigger not exists"; - } - - } - -} diff --git a/src/main/java/io/qyi/e5/service/quartz/MyQuartzJobService.java b/src/main/java/io/qyi/e5/service/quartz/MyQuartzJobService.java deleted file mode 100644 index b5159c8..0000000 --- a/src/main/java/io/qyi/e5/service/quartz/MyQuartzJobService.java +++ /dev/null @@ -1,38 +0,0 @@ -package io.qyi.e5.service.quartz; - -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import io.qyi.e5.outlook.entity.Outlook; -import io.qyi.e5.outlook.service.IOutlookService; -import org.quartz.Job; -import org.quartz.JobExecutionContext; -import org.quartz.JobExecutionException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; - -import java.util.List; - -/** - * @program: e5 - * @description: - * @author: 落叶随风 - * @create: 2020-03-03 14:37 - **/ -public class MyQuartzJobService implements Job { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - @Autowired - IOutlookService outlookService; - - @Override - public void execute(JobExecutionContext context) throws JobExecutionException { - logger.info("定时任务被调用~"); -// QueryWrapper queryWrapper = new QueryWrapper<>(); - List list = outlookService.findAll(); - logger.info(String.valueOf(list.size())); - for (Outlook outlook :list) { - logger.info(outlook.toString()); - outlookService.getMailList(outlook); - } - } -}