4.0.0
com.kevin.quartz
quartz
war
1.0
quartz1.x
http://maven.apache.org
UTF-8
3.2.14.RELEASE
org.apache.maven.plugins
maven-war-plugin
false
org.apache.maven.plugins
maven-source-plugin
attach-sources
jar-no-fork
org.apache.maven.plugins
maven-compiler-plugin
1.6
${artifactId}
javax.servlet
servlet-api
2.5
provided
junit
junit
4.10
test
log4j
log4j
1.2.16
org.springframework
spring-core
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-aspects
${spring.version}
aopalliance
aopalliance
1.0
org.aspectj
aspectjweaver
1.8.6
org.quartz-scheduler
quartz
1.8.6
package com.kevin.quartz.job;
import java.util.Date;
public class MyJob {
public void work() {
System.out.println("current datetime: " + new Date().toString());
}
}
work
0/1 * * * * ?
contextConfigLocation
/WEB-INF/classes/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
4.0.0
com.kevin.quartz
example1
jar
1.0
example1
http://maven.apache.org
UTF-8
4.0.0.RELEASE
org.apache.maven.plugins
maven-compiler-plugin
1.6
org.apache.maven.plugins
maven-source-plugin
attach-sources
jar-no-fork
${artifactId}
junit
junit
4.10
test
log4j
log4j
1.2.16
org.springframework
spring-core
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-aspects
${spring.version}
aopalliance
aopalliance
1.0
org.aspectj
aspectjweaver
1.8.6
org.quartz-scheduler
quartz
2.2.1
package com.kevin.quartz;
public class MyJob {
public void execute(){
System.out.println("starting ...");
System.out.println("this is quartz job executing ...");
System.out.println("ending ...");
}
}
package com.kevin.quartz;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:quartz-beans.xml")
public class QuartzTest {
@Test
public void testExecute() {
try {
Thread.sleep(1000 * 10); //此处是为了看到效果
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
starting ...
this is quartz job executing...
ending ...
starting ...
this is quartz job executing...
ending ...
因为项目仅执行10秒
任务每5秒执行一次
所以一共就只有2次执行
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Import(value=SchedulingConfiguration.class)
@Documentedpublic
@interface EnableScheduling
@Configuration
@EnableScheduling
public class AppConfig {
// 各种@bean的定义
// various @Bean definitions
}
package com.myco.tasks;
public class MyTask {
@Scheduled(fixedRate=1000)
public void work() {
// 任务执行逻辑
// task execution logic
}
}
@Configuration
@EnableScheduling
public class AppConfig {
@Bean
public MyTask task() {
return new MyTask();
}
}
@Component //import org.springframework.stereotype.Component;
public class MyTask {
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
@Override
public void myTest(){
System.out.println("进入测试");
}
}
@Configuration
@ComponentScan(basePackages="com.myco.tasks")
public class AppConfig {
}
使用了@Scheduled注解方法也可以在使用了@Configuration注解的类里面使用:
@Configuration
@EnableScheduling
public class AppConfig {
@Scheduled(fixedRate=1000)
public void work() {
// task execution logic
}
}
如果希望进行更多的控制,我们可以让使用@Configuration注解的类实现SchedulingConfigurer接口,这样就可以访问底层的ScheduledRegistrar实例。
下面的例子演示如何定制Executer去执行任务计划:
@Configuration
@EnableScheduling
public class AppConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
@Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(100);
}
}
这样是为了确保当Spring应用上下文关闭的时候任务执行者也被正确地关闭。
实现SchedulingConfigurar接口还允许细粒度控制任务通过ScheduledTaskRegistrar进行登记。
例如,下面的配置使用自定义的Trigger执行bean的方法
@Configuration
@EnableScheduling
public class AppConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler());
taskRegistrar.addTriggerTask(
new Runnable() {
public void run() {
myTask().work();
}
},
new CustomTrigger()
);
}
@Bean(destroyMethod="shutdown")
public Executor taskScheduler() {
return Executors.newScheduledThreadPool(42);
}
@Bean
public MyTask myTask() {
return new MyTask();
}
}