quartz定時任務(wù)的實(shí)現(xiàn)在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 "";
}
我們主要用的就是第一個參數(shù),cron,配置定時任務(wù)運(yùn)行的周期。
我們來通過@Scheduled來實(shí)現(xiàn)我們的第一個注解配置的定時任務(wù)
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
*
*/
@Service
public class HelloJob {
public HelloJob() {
System.out.println("HelloJob創(chuàng)建成功");
}
@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注解發(fā)揮作用,我們還需要在配置文件中加上一些東西。
1.在Spring配置文件中添加 配置
我們知道在spring中要想類中使用的注解被Spring容器檢測到,要在配置文件中指定對應(yīng)的報(bào)名,使用
<context:component-scan base-package="com.wangcc.ssm,com.wangcc.quartz,com.wangcc.test.properties" />
而我們要使得Spring quartz定時任務(wù)的相應(yīng)注解生效,也需要在Spring配置文件中加一句話。
<task:annotation-driven/>
這個annotation-driven是不是很熟悉,我們在配置springmvc的時候也應(yīng)用到了相似的配置。
<mvc:annotation-driven/>
是告知Spring,我們啟用注解驅(qū)動。然后Spring會自動為我們注冊springmvc開發(fā)相關(guān)的Bean到工廠中,來處理我們的請求。這個在我們分析SpringMVC源碼的時候會細(xì)說。
同理<> 也是告知Spring,我們的quartz定時任務(wù)使用注解驅(qū)動的,會自動去找使用了@Scheduled的注解方法,完成定時任務(wù)的處理。
2.在Spring配置文件中為task增加相應(yīng)的命名空間。
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
然后,我們可以開始編寫測試類來測一下程序是否成功執(zhí)行了。
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);
讓測試方法執(zhí)行的線程休眠一下,要不然這個測試類的執(zhí)行只會初始化SpringBean注冊等工作,然后直接執(zhí)行測試方法, 無法測試定時任務(wù)了。
為了讓運(yùn)行周期的配置更加靈活,我們在實(shí)際項(xiàng)目中打算使用配置文件來配置cron的value,那我們應(yīng)該怎么做呢。
這里我們就需要使用一個新接觸的注解:@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創(chuàng)建成功");
}
// @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()));
}
當(dāng)我們這樣配置后重新執(zhí)行測試類,我們發(fā)現(xiàn)報(bào)錯了,報(bào)錯信息提示:Could not resolve placeholder ‘batch.properties’ in string value “${helloBatch}
一開始看到這樣的報(bào)錯信息我是蒙蔽的,心想是不是這個@PropertySource注解不好使呀,還是我這個注解配的有問題呀,上網(wǎng)查找原因后發(fā)現(xiàn),原來是這樣的。
在spring的xml配置文件中當(dāng)有多個*.properties文件需要加載時。我們需要進(jìn)行一些特殊的配置,
加上
<property name="ignoreUnresolvablePlaceholders" value="true" />
使得配置文件配置節(jié)點(diǎn)如下:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
原因如下:
Spring容器采用反射掃描的發(fā)現(xiàn)機(jī)制,在探測到Spring容器中有一個org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就會停止對剩余PropertyPlaceholderConfigurer的掃描(Spring 3.1已經(jīng)使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。
而這個基于命名空間的配置,其實(shí)內(nèi)部就是創(chuàng)建一個PropertyPlaceholderConfigurer Bean而已。換句話說,即Spring容器僅允許最多定義一個PropertyPlaceholderConfigurer(或),其余的會被Spring忽略掉(其實(shí)Spring如果提供一個警告就好了)。 然后我們使用注解,相當(dāng)于又創(chuàng)建一個PropertyPlaceholderConfigurer Bean,就造成了有多個Bean就會有問題。
加上這個配置后,我又跑了一遍,信心滿滿的認(rèn)為這次總穩(wěn)了吧,然而并沒有,還是報(bào)錯,我的天,報(bào)錯信息大意為cron需要指定6或7個域,而我只提供了一個域,剛開始很納悶為啥會報(bào)這個錯呀,明明我就是配的6個域呀,怎么會錯呢,這不科學(xué)呀。這肯定還是讀文件的時候出問題了,上網(wǎng)查找,發(fā)現(xiàn)果然是這樣,僅僅上面的配置可以讀取配置文件,但是是無法正確的讀取配置文件的內(nèi)容的,如果想要正確的讀取,需要配置
@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創(chuàng)建成功");
}
// @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 * * * * ?