zoukankan      html  css  js  c++  java
  • quartz与spring集成

    搭建环境是在MinWebEnvironment之上进行的

    要与spring整合,需要spring-context-support.jar,

    因为org.springframework.scheduling.quartz包在该jar中

    使用quart需要导入的jar包

    注意:quartz版本不能太低,我之前使用的是1.7就不行,使用了2.2.1就可以

    添加的文件如图:

    Task1.java文件内容:

    1 package quartz.task;
    2 
    3 public class Task1 {
    4     private int num = 0;
    5     public void sayHello(){
    6         System.out.println("hello,"+(++num));
    7     }
    8 }

    application-task.xml文件内容:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans
     7         http://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/context
     9         http://www.springframework.org/schema/context/spring-context.xsd
    10         http://www.springframework.org/schema/mvc
    11         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    12     
    13     <!-- 配置调度器工厂SchedulerFactoryBean 
    14          该bean的name可以不要,没有人为的引用他
    15     -->
    16     <bean name="myQuartzScheduler"
    17           class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    18         <property name="triggers">
    19             <list>
    20                 <ref bean="myJobTrigger"/>
    21             </list>
    22         </property>
    23     </bean>
    24     
    25     <!-- 配置cron触发器 -->
    26     <bean id="myJobTrigger"
    27           class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    28         <property name="jobDetail">
    29             <ref bean="myJobDetail"/>
    30         </property>
    31         <property name="cronExpression">
    32             <value>0/3 * * * * ?</value>
    33         </property>
    34     </bean>
    35     
    36     <!-- 配置方法调用任务工厂 -->
    37     <bean id="myJobDetail"
    38           class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    39         <property name="targetObject">
    40             <ref bean="myJob"/>
    41         </property>
    42         <property name="targetMethod">
    43             <value>sayHello</value>
    44         </property>
    45     </bean>
    46     
    47     <!-- 定义一个bean -->
    48     <bean id="myJob" class="quartz.task.Task1"></bean>
    49     
    50     <bean id="quartzAnno" class="quartz.task.QuartzAnno"/>
    51     
    52 </beans>

    运行结果:

     在任务中同样可以使用spring注入,比如:

     1 package quartz.task;
     2 
     3 import javax.annotation.Resource;
     4 
     5 public class Task1 {
     6     
     7     @Resource
     8     private QuartzAnno quartzAnno;
     9     
    10     private int num = 0;
    11     public void sayHello(){
    12         System.out.println("hello,"+(++num));
    13         quartzAnno.say();
    14     }
    15 }
  • 相关阅读:
    B/S学习总结
    鶴山 李正浩 博士(探访墓所)
    毕业工作五年的总结和感悟(上)
    NSURLConnection经常使用的代理方法
    14.2 事务的ACID属性
    Search a 2D Matrix -- LeetCode
    ios开发之-继承的实现运用
    【Android】读取sdcard上的图片
    BZOJ 2820 YY的GCD 莫比乌斯反演
    linux写环境变量对字符转义
  • 原文地址:https://www.cnblogs.com/TheoryDance/p/5117022.html
Copyright © 2011-2022 走看看