spring quartz 任务注入spring service
SchedulerFactoryBean+AdaptableJobFactory+QuartzJobBean
package schedule.quartz5;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
//Spring与Quartz集成使用的是SchedulerFactoryBean这个类
@Configuration
public class QuartzConfig {
// @Autowired
// private TaskSchedulerFactory taskSchedulerFactory;
// @Bean
// public SchedulerFactoryBean schedulerFactoryBean() {
// SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
// schedulerFactoryBean.setJobFactory(taskSchedulerFactory);
// return schedulerFactoryBean;
// }
// 注入scheduler到spring,在quartzManege会用到
@Bean(name = "scheduler")
public Scheduler scheduler(TaskSchedulerFactory quartzJobFactory) throws Exception {
SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
factoryBean.setJobFactory(quartzJobFactory);
factoryBean.afterPropertiesSet();
Scheduler scheduler = factoryBean.getScheduler();
scheduler.start();
return scheduler;
}
}
package schedule.quartz5;
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;
@Component("taskSchedulerFactory")
public class TaskSchedulerFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception
{
// 首先,调用父类的方法创建好Quartz所需的Job实例
Object jobInstance = super.createJobInstance(bundle);
// 然后,使用BeanFactory为创建好的Job实例进行属性自动装配并将其纳入到Spring容器的管理之中,属于Spring的技术范畴.
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
package schedule.quartz5;
import javax.annotation.Resource;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.quartz.impl.triggers.SimpleTriggerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
@Component
public class ProcessJob extends QuartzJobBean {
@Resource
private SayService sayService;
private Logger logger = LoggerFactory.getLogger(ProcessJob.class);
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
try {
sayService.sayHello();
if(context.getTrigger() instanceof CronTriggerImpl){
CronTriggerImpl xxx = ((CronTriggerImpl)context.getTrigger());
logger.info("动态定时调度测试 job group:"+xxx.getJobGroup()+", job name:"+xxx.getJobName() +", trigger group:"+xxx.getGroup()+", trigger name:"+xxx.getName()+", cronExpression "+xxx.getCronExpression());
}else if(context.getTrigger() instanceof SimpleTriggerImpl){
SimpleTriggerImpl xxx = ((SimpleTriggerImpl)context.getTrigger());
logger.info("动态定时调度测试 job group:"+xxx.getJobGroup()+", job name:"+xxx.getJobName() +", trigger group:"+xxx.getGroup()+", trigger name:"+xxx.getName()+", cronExpression "+xxx);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
java.lang.NoSuchMethodError
org.springframework.util.ReflectionUtils.accessibleConstructor(Ljava/lang/Class;[Ljava/lang/Class;)Ljava/lang/reflect/Constructor;
一般是引入了两个重复的spring Jar包,如既引入了spring.framework 又引入了spring.core,去掉一个就好
用tree方式查看
spring quartz 任务注入spring service的更多相关文章
- 在非spring组件中注入spring bean
1.在spring中配置如下<context:spring-configured/> <context:load-time-weaver aspectj-weaving=&q ...
- 如何在静态方法或非Spring Bean中注入Spring Bean
在项目中有时需要根据需要在自己new一个对象,或者在某些util方法或属性中获取Spring Bean对象,从而完成某些工作,但是由于自己new的对象和util方法并不是受Spring所 ...
- service手动实例化(new)导致类中的spring对象无法注入的问题解决
下面说的这个画横线的可能是错误的,因为我之前用controller继承父类的注解对象的时候成功了,所以可能这次的唯一原因就是 不该把本该从ioc容器中拿出的对象通过new的方式实例化,至于继承注解对象 ...
- Spring+quartz 实现定时任务job集群配置
为什么要有集群定时任务? 因为如果多server都触发相同任务,又同时执行,那在99%的场景都是不适合的.比如银行每晚24:00都要汇总营业额.像下面3台server同时进行汇总,最终计算结果可能是真 ...
- 关于spring的自动注入
关于spring的自动注入 spring里面可以设置BeanDefinition自动注入类型,默认为AUTOWIRE_NO(不进行自动注入).mybatis里面的扫描接口生成MapperFactory ...
- Spring+quartz 实现定时任务job集群配置【原】
为什么要有集群定时任务? 因为如果多server都触发相同任务,又同时执行,那在99%的场景都是不适合的.比如银行每晚24:00都要汇总营业额.像下面3台server同时进行汇总,最终计算结果可能是真 ...
- 【spring】69道Spring面试题和答案
原文地址:http://ifeve.com/spring-interview-questions-and-answers/ 目录 Spring 概述 依赖注入 Spring beans Spring注 ...
- Spring quartz定时任务service注入问题
今天想单元测试一下spring中的quartz定时任务,job类的大致结构和下面的SpringQtz1类相似,我的是实现的org.quartz.Job接口,到最后总是发现job类里注入的service ...
- 解决Spring+Quartz无法自动注入bean问题
问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...
随机推荐
- calc()语法
什么是calc()? 学习calc()之前,我们有必要先知道calc()是什么?只有知道了他是个什么东东?在实际运用中更好的使用他. calc()从字面我们可以把他理解为一个函数function.其实 ...
- Linux驱动中常用的宏
.module_i2c_driver(adxl34x_driver)展开为 static int __int adxl34x_driver_init(void) { return i2c_regist ...
- 【转】每天一个linux命令(42):kill命令
原文网址:http://www.cnblogs.com/peida/archive/2012/12/20/2825837.html Linux中的kill命令用来终止指定的进程(terminate a ...
- devExpress Gridview添加按钮或链接
1.进入view设计 2.增加列 3.修改Repository中相关内容
- 51nod 1965 奇怪的式子——min_25筛
题目:http://www.51nod.com/Challenge/Problem.html#!#problemId=1965 考虑 \( \prod_{i=1}^{n}\sigma_0^i \) \ ...
- JavaScriptSerializer类 对象序列化为JSON,JSON反序列化为对象 。
JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.说白了就是能够直接将一个C#对象传送到前台页面成为javascript对 ...
- Modbus tcp 格式说明 通讯机制 附C#测试工具用于学习,测试
前言: 之前的博客介绍了如何用C#来读写modbus tcp服务器的数据,文章:http://www.cnblogs.com/dathlin/p/7885368.html 当然也有如何创建一个服务器文 ...
- jsoncpp解析拼装数组
Cocos2d-x添加jsoncpp应该资料都有了,今天来讲讲数组的解析和拼装- int main() { 数组创建与分析: 例子一: string strValue = "{\" ...
- 峰Spring4学习(3)注入参数的几种类型
People.java model类: package com.cy.entity; import java.util.ArrayList; import java.util.HashMap; im ...
- java正则表达式学习
1.简单认识正则: public class Test { public static void main(String[] args) { //简单认识正则 p("abc".ma ...