注解实现spring整合quartz,定时执行service层方法,解决无法注入问题

来源:互联网 发布:c语言break 编辑:程序博客网 时间:2024/05/20 02:53

spring相关代码配置略去.

下面看quartz相关配置

先看applicationContext-scheduler.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 定义任务bean --><bean name="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"><!-- 指定具体的job类 --><property name="jobClass" value="org.xxx.controller.MyJob" /><!-- 指定job的名称 --><property name="name" value="myJob" /><!-- 指定job的分组 --><property name="group" value="jobs" /><!-- 必须设置为true,如果为false,当没有活动的触发器与之关联时会在调度器中删除该任务  --><property name="durability" value="true"/><!-- 指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的 --><property name="applicationContextJobDataKey" value="applicationContext"/></bean><bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail" ref="myJobDetail" /><!-- 每一分钟执行一次 --><property name="cronExpression" value="0 */5 * * * ?" /></bean> <!-- 定义调度器 --><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">    <property name="triggers">        <list>            <ref bean="cronTrigger" />        </list>    </property>    <property name="jobFactory">            <bean class="org.xxx.utils.MyJobFactory" />      <!--解决spring注入失败-->        </property></bean></beans>
MyJobFactory.javaimport org.quartz.spi.TriggerFiredBundle;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.config.AutowireCapableBeanFactory;public class MyJobFactory extends  org.springframework.scheduling.quartz.SpringBeanJobFactory{    @Autowired    private AutowireCapableBeanFactory beanFactory;    /**     * 这里覆盖了super的createJobInstance方法,对其创建出来的类再进行autowire。     */    @Override    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {        Object jobInstance = super.createJobInstance(bundle);        beanFactory.autowireBean(jobInstance);        return jobInstance;    }}public class MyJob extends QuartzJobBean {    @Autowired    private IConfigService iConfigService;    @Autowired    private DataAnalyseService dataAnalyseService;    @Override    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {        System.out.println("myJob 执行了............." + context.getTrigger().getKey().getName());        ApplicationContext applicationContext = (ApplicationContext) context.getJobDetail().getJobDataMap()                .get("applicationContext");        System.out.println("获取到的Spring容器是: " + applicationContext);        Long cTime = System.currentTimeMillis();        Config config = iConfigService.findConfigWithType("INDEX","InitParam");        String[] tmp = config.getStrValue().split("#");        Long cTime1 = cTime-Integer.parseInt(tmp[2])*60*60*1000;        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        String currentTime = sdf.format(cTime);        String currentTime1 = sdf.format(cTime1);        //String startTime = sdf.format(calendar.getTime());        List<Atom> result= dataAnalyseService.getAtoms(1, currentTime1, currentTime); //调用service中方法    }}maven依赖:<dependency>  <groupId>org.quartz-scheduler</groupId>  <artifactId>quartz</artifactId>  <version>2.2.1</version>  </dependency>  <dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.0.3.RELEASE</version>  </dependency>   <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-context-support</artifactId>  <version>4.0.3.RELEASE</version>  </dependency>
 
阅读全文
0 0
原创粉丝点击