SchedulerFactoryBean+AdaptableJobFactory+QuartzJobBean

  1. package schedule.quartz5;
  2. import org.quartz.Scheduler;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.scheduling.quartz.SchedulerFactoryBean;
  7. //Spring与Quartz集成使用的是SchedulerFactoryBean这个类
  8. @Configuration
  9. public class QuartzConfig {
  10. // @Autowired
  11. // private TaskSchedulerFactory taskSchedulerFactory;
  12. // @Bean
  13. // public SchedulerFactoryBean schedulerFactoryBean() {
  14. // SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
  15. // schedulerFactoryBean.setJobFactory(taskSchedulerFactory);
  16. // return schedulerFactoryBean;
  17. // }
  18. // 注入scheduler到spring,在quartzManege会用到
  19. @Bean(name = "scheduler")
  20. public Scheduler scheduler(TaskSchedulerFactory quartzJobFactory) throws Exception {
  21. SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
  22. factoryBean.setJobFactory(quartzJobFactory);
  23. factoryBean.afterPropertiesSet();
  24. Scheduler scheduler = factoryBean.getScheduler();
  25. scheduler.start();
  26. return scheduler;
  27. }
  28. }
  1. package schedule.quartz5;
  2. import org.quartz.spi.TriggerFiredBundle;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
  5. import org.springframework.scheduling.quartz.AdaptableJobFactory;
  6. import org.springframework.stereotype.Component;
  7. @Component("taskSchedulerFactory")
  8. public class TaskSchedulerFactory extends AdaptableJobFactory {
  9. @Autowired
  10. private AutowireCapableBeanFactory capableBeanFactory;
  11. protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception
  12. {
  13. // 首先,调用父类的方法创建好Quartz所需的Job实例
  14. Object jobInstance = super.createJobInstance(bundle);
  15. // 然后,使用BeanFactory为创建好的Job实例进行属性自动装配并将其纳入到Spring容器的管理之中,属于Spring的技术范畴.
  16. capableBeanFactory.autowireBean(jobInstance);
  17. return jobInstance;
  18. }
  19. }
  1. package schedule.quartz5;
  2. import javax.annotation.Resource;
  3. import org.quartz.JobExecutionContext;
  4. import org.quartz.JobExecutionException;
  5. import org.quartz.impl.triggers.CronTriggerImpl;
  6. import org.quartz.impl.triggers.SimpleTriggerImpl;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.scheduling.quartz.QuartzJobBean;
  10. import org.springframework.stereotype.Component;
  11. @Component
  12. public class ProcessJob extends QuartzJobBean {
  13. @Resource
  14. private SayService sayService;
  15. private Logger logger = LoggerFactory.getLogger(ProcessJob.class);
  16. @Override
  17. protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
  18. try {
  19. sayService.sayHello();
  20. if(context.getTrigger() instanceof CronTriggerImpl){
  21. CronTriggerImpl xxx = ((CronTriggerImpl)context.getTrigger());
  22. logger.info("动态定时调度测试 job group:"+xxx.getJobGroup()+", job name:"+xxx.getJobName() +", trigger group:"+xxx.getGroup()+", trigger name:"+xxx.getName()+", cronExpression "+xxx.getCronExpression());
  23. }else if(context.getTrigger() instanceof SimpleTriggerImpl){
  24. SimpleTriggerImpl xxx = ((SimpleTriggerImpl)context.getTrigger());
  25. logger.info("动态定时调度测试 job group:"+xxx.getJobGroup()+", job name:"+xxx.getJobName() +", trigger group:"+xxx.getGroup()+", trigger name:"+xxx.getName()+", cronExpression "+xxx);
  26. }
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  1.  
  2.  

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的更多相关文章

  1. 在非spring组件中注入spring bean

    1.在spring中配置如下<context:spring-configured/>     <context:load-time-weaver aspectj-weaving=&q ...

  2. 如何在静态方法或非Spring Bean中注入Spring Bean

           在项目中有时需要根据需要在自己new一个对象,或者在某些util方法或属性中获取Spring Bean对象,从而完成某些工作,但是由于自己new的对象和util方法并不是受Spring所 ...

  3. service手动实例化(new)导致类中的spring对象无法注入的问题解决

    下面说的这个画横线的可能是错误的,因为我之前用controller继承父类的注解对象的时候成功了,所以可能这次的唯一原因就是 不该把本该从ioc容器中拿出的对象通过new的方式实例化,至于继承注解对象 ...

  4. Spring+quartz 实现定时任务job集群配置

    为什么要有集群定时任务? 因为如果多server都触发相同任务,又同时执行,那在99%的场景都是不适合的.比如银行每晚24:00都要汇总营业额.像下面3台server同时进行汇总,最终计算结果可能是真 ...

  5. 关于spring的自动注入

    关于spring的自动注入 spring里面可以设置BeanDefinition自动注入类型,默认为AUTOWIRE_NO(不进行自动注入).mybatis里面的扫描接口生成MapperFactory ...

  6. Spring+quartz 实现定时任务job集群配置【原】

    为什么要有集群定时任务? 因为如果多server都触发相同任务,又同时执行,那在99%的场景都是不适合的.比如银行每晚24:00都要汇总营业额.像下面3台server同时进行汇总,最终计算结果可能是真 ...

  7. 【spring】69道Spring面试题和答案

    原文地址:http://ifeve.com/spring-interview-questions-and-answers/ 目录 Spring 概述 依赖注入 Spring beans Spring注 ...

  8. Spring quartz定时任务service注入问题

    今天想单元测试一下spring中的quartz定时任务,job类的大致结构和下面的SpringQtz1类相似,我的是实现的org.quartz.Job接口,到最后总是发现job类里注入的service ...

  9. 解决Spring+Quartz无法自动注入bean问题

    问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...

随机推荐

  1. GStreamer插件分类

    gst-plugins-base一套小而固定的插件,涵盖各种可能类型的elements; 这些在开发系列期间随着核心变化而不断更新.我们相信分销商可以安全地发行这些插件.人们编写插件应该将他们的代码基 ...

  2. MySQL Transaction--RC和RR区别

    在MySQL中,事务隔离级别RC(read commit)和RR(repeatable read)两种事务隔离级别基于多版本并发控制MVCC(multi-version concurrency con ...

  3. python 常见的内置函数

    内置函数 接下来,我们就一起来看看python里的内置函数.截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是python提供给你直接可以拿来使用的所有函数.这 ...

  4. stenciljs 学习十 服务器端渲染

      stenciljs提供了 ssr 支持,对于express 最简单的就是使用提供的中间件 express 集成 const express = require('express'); const ...

  5. SQL——ROW_NUMBER

    版权声明:欢迎转载,请注明出处 https://blog.csdn.net/suneqing/article/details/30250193 语法: ROW_NUMBER() OVER(PARTIT ...

  6. u-boot分析

    4.Bootloader:u-boot.2009.08分析与移植4.1:分析u-boot根文件夹下的Makefile,能够看到uboot编译的顺序例如以下,由此可知编译运行的第一个文件是cpu/$(C ...

  7. ThinkPHP 更新 5.0.23 和 5.1.31

    ThinkPHP 更新 5.0.23 和 5.1.31 FastAdmin 也跟着更新. V1.0.0.20181210_beta 修复 ThinkPHP5.0发布了一个重要安全更新,强烈建议更新 修 ...

  8. ZedGraph控件的使用

    http://blog.chinaunix.net/uid-20776117-id-1847015.html 在我们编写程序的时候,有时候是要做一些统计的,为了达到一目了然的效果,饼状图,曲线图,柱状 ...

  9. ExtJS xtype 一览

    基本组件: xtype Class 描述 button Ext.Button 按钮 splitbutton Ext.SplitButton 带下拉菜单的按钮 cycle Ext.CycleButton ...

  10. Redis在Windows集群中的错误

    创建集群: ./redis-trib.rb  create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:70 ...