Quartz全注解使用

来源:互联网 发布:混沌战域美人数据 编辑:程序博客网 时间:2024/05/18 03:37

title:Quartz全注解使用

date:2017年10月26日23:52:09


最近需要将原有项目中的一个发送邮件的批处理迁移到新项目中,之前的项目里,是使用的Spring quartz实现的定时任务,现在新的项目也打算使用quartz,但是想着抛弃原来的配置文件形式实现,改用注解实现,随着公司打算全面通过Spring boot,实现全注解开发模式,所以这次quartz的使用也用上了注解。

quartz定时任务的实现在Spring中使用的注解名称为:@Scheduled
可以作用于方法和类上

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Repeatable(Schedules.class)public @interface Scheduled {    String cron() default "";    String zone() default "";    long fixedDelay() default -1;    String fixedDelayString() default "";    long fixedRate() default -1;    String fixedRateString() default "";    long initialDelay() default -1;    String initialDelayString() default "";}

我们主要用的就是第一个参数,cron,配置定时任务运行的周期。
我们来通过@Scheduled来实现我们的第一个注解配置的定时任务

package com.wangcc.quartz;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.PropertySource;import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;/** * @ClassName: HelloJob * @Description: http://blog.csdn.net/tanyongbing1988/article/details/45689987 * @author wangcc * @date 2017年10月23日 下午3:26:57 *  */@Servicepublic class HelloJob {    public HelloJob() {        System.out.println("HelloJob创建成功");    }     @Scheduled(cron = "0/1 * * * * ? ") // 每隔1秒隔行一次    public void run() {        System.out.println("Hello MyJob  " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));    }}

要想@Scheduled注解发挥作用,我们还需要在配置文件中加上一些东西。
1.在Spring配置文件中添加 配置

我们知道在spring中要想类中使用的注解被Spring容器检测到,要在配置文件中指定对应的报名,使用

    <context:component-scan base-package="com.wangcc.ssm,com.wangcc.quartz,com.wangcc.test.properties" />  

而我们要使得Spring quartz定时任务的相应注解生效,也需要在Spring配置文件中加一句话。

             <task:annotation-driven/>    

这个annotation-driven是不是很熟悉,我们在配置springmvc的时候也应用到了相似的配置。

<mvc:annotation-driven/>

是告知Spring,我们启用注解驱动。然后Spring会自动为我们注册springmvc开发相关的Bean到工厂中,来处理我们的请求。这个在我们分析SpringMVC源码的时候会细说。
同理<> 也是告知Spring,我们的quartz定时任务使用注解驱动的,会自动去找使用了@Scheduled的注解方法,完成定时任务的处理。

2.在Spring配置文件中为task增加相应的命名空间。
xmlns:末尾加上

xmlns:task="http://www.springframework.org/schema/task" 

xsi:schemaLocation加上

              http://www.springframework.org/schema/task    http://www.springframework.org/schema/task/spring-task-3.0.xsd 

然后,我们可以开始编写测试类来测一下程序是否成功执行了。

package com.wangcc.test.quartz;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) // 表示继承了SpringJUnit4ClassRunner类@ContextConfiguration(locations = { "classpath:mybatis-spring.xml" })public class TestQuartz {    @Test    public void testquartz() throws InterruptedException {        Thread.sleep(100000L);        System.out.println("Test quartz");    }}

这里为了测试到效果,需要使用 Thread.sleep(100000L);
让测试方法执行的线程休眠一下,要不然这个测试类的执行只会初始化SpringBean注册等工作,然后直接执行测试方法, 无法测试定时任务了。
为了让运行周期的配置更加灵活,我们在实际项目中打算使用配置文件来配置cron的value,那我们应该怎么做呢。
这里我们就需要使用一个新接触的注解:@PropertySource

这个注解只能作用在类上

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Repeatable(PropertySources.class)public @interface PropertySource {    String name() default "";    String[] value();    boolean ignoreResourceNotFound() default false;}

使用这个注解,我们就可以将配置文件中的东西直接在Java代码中读取了。

在使用时,我们一般需要指定value,value指向文件所在路径,如下

package com.wangcc.quartz;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.PropertySource;import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;/** * @ClassName: HelloJob * @Description:  * @author wangcc * @date 2017年10月23日 下午3:26:57 *  */@Service@PropertySource(value = "classpath:batch.properties")public class HelloJob {    public HelloJob() {        System.out.println("HelloJob创建成功");    }    // @Scheduled(cron = "0/1 * * * * ? ")    @Scheduled(cron = "${helloBatch}") // 每隔1秒隔行一次    public void run() {        System.out.println("Hello MyJob  " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));    }}

当我们这样配置后重新执行测试类,我们发现报错了,报错信息提示:Could not resolve placeholder ‘batch.properties’ in string value “${helloBatch}

一开始看到这样的报错信息我是蒙蔽的,心想是不是这个@PropertySource注解不好使呀,还是我这个注解配的有问题呀,上网查找原因后发现,原来是这样的。

在spring的xml配置文件中当有多个*.properties文件需要加载时。我们需要进行一些特殊的配置,

加上

               <property name="ignoreUnresolvablePlaceholders" value="true" /> 

使得配置文件配置节点如下:

  <bean id="propertyConfigurer"          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          <property name="location" value="classpath:jdbc.properties" />                 <property name="ignoreUnresolvablePlaceholders" value="true" />     </bean>  

原因如下:

Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。

而这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。 然后我们使用注解,相当于又创建一个PropertyPlaceholderConfigurer Bean,就造成了有多个Bean就会有问题。

加上这个配置后,我又跑了一遍,信心满满的认为这次总稳了吧,然而并没有,还是报错,我的天,报错信息大意为cron需要指定6或7个域,而我只提供了一个域,刚开始很纳闷为啥会报这个错呀,明明我就是配的6个域呀,怎么会错呢,这不科学呀。这肯定还是读文件的时候出问题了,上网查找,发现果然是这样,仅仅上面的配置可以读取配置文件,但是是无法正确的读取配置文件的内容的,如果想要正确的读取,需要配置

@Bean    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {        return new PropertySourcesPlaceholderConfigurer();    }

否则会造成没法正确读取中配置文件中的值。

更改后的类为:

package com.wangcc.quartz;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.PropertySource;import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;/** * @ClassName: HelloJob * @Description:  * @author wangcc * @date 2017年10月23日 下午3:26:57 *  */@Service@PropertySource(value = "classpath:quartz.properties")public class HelloJob {    public HelloJob() {        System.out.println("HelloJob创建成功");    }    // @Scheduled(cron = "0/1 * * * * ? ")    @Scheduled(cron = "${helloBatch}") // 每隔1秒隔行一次    public void run() {        System.out.println("Hello MyJob  " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));    }    /*     *      * 要注意的是,要使用     *      * @Bean public static PropertySourcesPlaceholderConfigurer     * propertyConfigInDev() { return new PropertySourcesPlaceholderConfigurer(); }     *      * 才能让spring正确解析出${} 中的值 http://blog.csdn.net/itchiang/article/details/51144218     */    @Bean    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {        return new PropertySourcesPlaceholderConfigurer();    }}

batch.proerties

helloBatch=0/1 * * * * ?

再次执行,终于成功。关于注解配置定时任务就先讲到这里,这里还有cron表达式的具体设置的问题和@PropertySource注解相关配置原理的问题没有解决,先留个坑。