1:AutowiringSpringBeanJobFactory.java

package com.microwisdom.grgzpt.jobs;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory; /**
* Created by yan on 2017/5/17.
*/
public class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory
implements ApplicationContextAware { private transient AutowireCapableBeanFactory beanFactory; @Override
public void setApplicationContext(final ApplicationContext context) { beanFactory = context.getAutowireCapableBeanFactory();
} @Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
final Object job = super.createJobInstance(bundle); beanFactory.autowireBean(job); return job;
} }

2:GrdbJob.java

package com.microwisdom.grgzpt.jobs;

import com.google.gson.Gson;
import com.microwisdom.grgzpt.dbsy.DbsyVo;
import com.microwisdom.grgzpt.redis.RedisService;
import com.microwisdom.grgzpt.utils.DateUtil;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired; /**
* Created by yan on 2017/5/16.
* 个人待办任务
*/
@DisallowConcurrentExecution
public class GrdbJob implements Job{ @Autowired
RedisService redisService; @Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Now:"+ DateUtil.getCurrTime()); redisService.set(vo.getSwrydm(), new Gson().toJson(vo));
}
}

3:SchedulerConfig.java

package com.microwisdom.grgzpt.jobs;

import org.quartz.JobDetail;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean; import java.io.IOException;
import java.util.Properties; /**
* Created by yan on 2017/5/17.
*/
@Configuration
public class SchedulerConfig { @Bean
public JobFactory jobFactory(ApplicationContext applicationContext) {
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory(); jobFactory.setApplicationContext(applicationContext); return jobFactory;
} @Bean
public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory, Trigger simpleJobTrigger)
throws IOException{
SchedulerFactoryBean factory = new SchedulerFactoryBean(); factory.setJobFactory(jobFactory);
factory.setQuartzProperties(quartzProperties());
factory.setTriggers(simpleJobTrigger); return factory;
} @Bean
public CronTriggerFactoryBean simpleJobTrigger(@Qualifier("simpleJobDetail") JobDetail jobDetail) {
CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean (); factoryBean.setJobDetail(jobDetail);
factoryBean.setStartDelay(1000L);
factoryBean.setName("trigger1");
factoryBean.setGroup("group1");
//周1至周5,每天上午8点至下午18点,每分钟执行一次
factoryBean.setCronExpression("0 0/1 8-18 ? * 2-6"); return factoryBean;
} @Bean
public JobDetailFactoryBean simpleJobDetail() {
JobDetailFactoryBean factoryBean = new JobDetailFactoryBean(); factoryBean.setJobClass(GrdbJob.class);
factoryBean.setGroup("group1");
factoryBean.setName("job1"); return factoryBean;
} @Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties")); propertiesFactoryBean.afterPropertiesSet(); return propertiesFactoryBean.getObject();
} }

Spring boot 与quart集成并在Job中注入服务的更多相关文章

  1. spring boot / cloud (三) 集成springfox-swagger2构建在线API文档

    spring boot / cloud (三) 集成springfox-swagger2构建在线API文档 前言 不能同步更新API文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...

  2. Spring Boot HikariCP 一 ——集成多数据源

    其实这里介绍的东西主要是参考的另外一篇文章,数据库读写分离的. 参考文章就把链接贴出来,里面有那位的代码,简单明了https://gitee.com/comven/dynamic-datasource ...

  3. Spring Boot系列——如何集成Log4j2

    上篇<Spring Boot系列--日志配置>介绍了Spring Boot如何进行日志配置,日志系统用的是Spring Boot默认的LogBack. 事实上,除了使用默认的LogBack ...

  4. 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作 完整版+kibana管理ES的index操作

    spring boot 2.X集成ES 进行CRUD操作  完整版 内容包括: ============================================================ ...

  5. 15、Spring Boot 2.x 集成 Swagger UI

    1.15.Spring Boot 2.x 集成 Swagger UI 完整源码: Spring-Boot-Demos 1.15.1 pom文件添加swagger包 <swagger2.versi ...

  6. 14、Spring Boot 2.x 集成 Druid 数据源

    14.Spring Boot 2.x 集成 Druid 数据源 完整源码: Spring-Boot-Demos

  7. 12、Spring Boot 2.x 集成 MongoDB

    1.12 Spring Boot 2.x 集成 MongoDB 完整源码: Spring-Boot-Demos

  8. 11、Spring Boot 2.x 集成 HBase

    1.11 Spring Boot 2.x 集成 HBase 完整源码: Spring-Boot-Demos

  9. 10、Spring Boot 2.x 集成 Log4j2

    1.10 Spring Boot 2.x 集成 Log4j2 完整源码: Spring-Boot-Demos

随机推荐

  1. Codeforces Round #295 (Div. 2)A - Pangram 水题

    A. Pangram time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  2. SQL Plus和PL/SQL

    一.SQL Plus是oracle提供的一种用户接口.类似于操作系统的命令行.用户可以通过在SQL Plus中输入命令来向数据库发            送命令,数据库也将处理结果通过SQL Plus ...

  3. javascript小记-javascript运行机制

    任何语言的运行过程中,都会有编译和执行: 对于传统编译型语言来说,编译步骤分为:词法分析.语法分析.语义检查.代码优化和字节生成.但对于解释型语言来说,通过词法分析和语法分析得到语法树后,就可以开始解 ...

  4. Any way to start Google Chrome in headless mode?

    Any way to start Google Chrome in headless mode? - Stack Overflow Any way to start Google Chrome in ...

  5. 通过adb shell操作android真机的SQLite数据库

    要通过命令行直接操作android真机上的SQLite数据库,可以直接通过adb shell来完成,不过,前提是必须获得root权限. 另外,android系统其实就是linux的shell,这个应该 ...

  6. Linux gcc编译参数

    最近编译一份开源代码,一编译就直接报错.我看了下报错信息,有点诧异.这些信息,放平常顶多就是个warnning而已啊,他这里怎么变成了error呢?我看了下Makefile,发现编译参数多了个-Wer ...

  7. C++空类产生哪些成员函数 || C++类可以自动生成的6个成员函数

    class Empty {     public:     Empty(); // 缺省构造函数     Empty( const Empty& ); // 拷贝构造函数     ~Empty ...

  8. OllyDbg 使用笔记 (十二)

    OllyDbg 使用笔记 (十二) 參考 书:<加密与解密> 视频:小甲鱼 解密系列 视频 演示样例程序下载地址:http://pan.baidu.com/s/1eQiV6aI 安装好程序 ...

  9. 在win7下安装VC6.0

    一.以系统管理员的身份执行VC6.0安装文件 二.在安装或者使用VisualC++6.0时,凡是出现兼容性问题提示对话框,一律按下面方式处理--把"不再显示此消息"打上勾,然后选择 ...

  10. merge into优化sql(转)

    使用Merge INTO优化SQL,性能提升巨大 分类: Oracle 2017-04-13 10:55:07   说说背景:开发有个需求,需要对新加的一个字段根据特定的业务逻辑更新数据.TPS_TR ...