mirror of
https://github.com/luoye663/e5.git
synced 2024-12-25 19:28:49 +00:00
删除 quartz ,使用rabbitMq
This commit is contained in:
parent
8aced448b4
commit
760e26d192
19
pom.xml
19
pom.xml
@ -143,22 +143,11 @@
|
||||
<version>RELEASE</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!--quartz 定时任务-->
|
||||
<!--这个包同时包含了一些其它的包:spring-context、spring-tx、spring-web、spring-messaging、spring-retry、spring-amqp、amqp-client,如果想单纯一点,可以单独引入。-->
|
||||
<dependency>
|
||||
<groupId>org.quartz-scheduler</groupId>
|
||||
<artifactId>quartz</artifactId>
|
||||
<version>2.2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.quartz-scheduler</groupId>
|
||||
<artifactId>quartz-jobs</artifactId>
|
||||
<version>2.2.1</version>
|
||||
</dependency>
|
||||
<!--因为quartz 需要有Spring context 所有引入mail包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
<groupId>org.springframework.amqp</groupId>
|
||||
<artifactId>spring-rabbit</artifactId>
|
||||
<!--<version>${version}</version>-->
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<ContextRefreshedEvent> {
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
@ -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<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||
|
||||
RedisTemplate<String, Object> 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<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||
@ -116,5 +78,4 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||
return template;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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<Outlook> queryWrapper = new QueryWrapper<>();
|
||||
List<Outlook> list = outlookService.findAll();
|
||||
logger.info(String.valueOf(list.size()));
|
||||
for (Outlook outlook :list) {
|
||||
logger.info(outlook.toString());
|
||||
outlookService.getMailList(outlook);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user