import java.util.concurrent.Executor;
import javax.annotation.Resource;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
/**
*
* @ClassName: AppConfig
* @Description: TODO(任务调度类::Note: In the above example the ThreadPoolTaskExecutor is not a fully managed Spring Bean.
* Add the @Bean annotation to the getAsyncExecutor() method if you want a fully managed bean. In such circumstances
* it is no longer necessary to manually call the executor.initialize() method as this will be invoked automatically when the bean is initialized. )
* @author (Dongcc)
* @since 2015年12月23日-下午3:22:51
* @category TODO:
*/
@Configuration
@EnableScheduling
@EnableAsync(
mode = AdviceMode.PROXY, proxyTargetClass = false,
order = Ordered.HIGHEST_PRECEDENCE
)
public class synchronizedCityConfigurer implements AsyncConfigurer, SchedulingConfigurer {
/**
*
* @author DongCC
* @since 2015年12月23日-下午3:27:11
* @category TODO:(线程池配置)
* @return
* ThreadPoolTaskScheduler
*/
@Bean
public ThreadPoolTaskScheduler taskScheduler()
{
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(5);
scheduler.setThreadNamePrefix("synchronized-city-task-");
scheduler.setAwaitTerminationSeconds(60);
scheduler.setWaitForTasksToCompleteOnShutdown(true);
return scheduler;
}
/**
* 异步执行
*/
@Override
public Executor getAsyncExecutor()
{
Executor executor = this.taskScheduler();
return executor;
}
/**
* 指挥任务执行
*/
@Override
public void configureTasks(ScheduledTaskRegistrar registrar)
{
TaskScheduler scheduler = this.taskScheduler();
registrar.setTaskScheduler(scheduler);
registrar.addCronTask(
new Runnable() {
public void run() {
task.doWork();
}
}, crontab);
}
@Resource(name="task")
private synchronizedCityTask task;
/** 任务调度间隔*/
private String crontab = "0 * * * * ?";
}
package com.mapbar.fieldwork.quartz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
*
* @ClassName: synchronizedCityTask
* @Description: TODO(要执行的任务)
* @author DongCC
* @since 2015年12月23日-下午3:26:03
* @category TODO:
*/
@Component("task")
public class synchronizedCityTask{
/**
*
* @author DongCC
* @since 2015年12月23日-下午3:29:56
* @category TODO:(具体的任务)
* void
*/
public void doWork() {
//TODO
LOGGER.info("计划任务被执行,线程id:" +Thread.currentThread().getId());
}
/** 日志对象 */
private static final Logger LOGGER = LoggerFactory.getLogger(synchronizedCityTask.class);
}
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /
- 区间
* 通配符
? 你不想设置那个字段
下面只例出几个式子
CRON表达式 含义
"0 0 12 * * ?" 每天中午十二点触发
"0 15 10 ? * *" 每天早上10:15触发
"0 15 10 * * ?" 每天早上10:15触发
"0 15 10 * * ? *" 每天早上10:15触发
"0 15 10 * * ? 2005" 2005年的每天早上10:15触发
"0 * 14 * * ?" 每天从下午2点开始到2点59分每分钟一次触发
"0 0/5 14 * * ?" 每天从下午2点开始到2:55分结束每5分钟一次触发
"0 0/5 14,18 * * ?" 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
"0 0-5 14 * * ?" 每天14:00至14:05每分钟一次触发
"0 10,44 14 ? 3 WED" 三月的每周三的14:10和14:44触发
"0 15 10 ? * MON-FRI" 每个周一、周二、周三、周四、周五的10:15触发