public interface TaskExecutor extends Executor {
/**
* Execute the given {@code task}.
* <p>The call might return immediately if the implementation uses
* an asynchronous execution strategy, or might block in the case
* of synchronous execution.
* @param task the {@code Runnable} to execute (never {@code null})
* @throws TaskRejectedException if the given task was not accepted
*/
@Override
void execute(Runnable task);
}
org.springframework.core.task.SimpleAsyncTaskExecutor
public void setConcurrencyLimit(int concurrencyLimit) {
this.concurrencyThrottle.setConcurrencyLimit(concurrencyLimit);
}
org.springframework.core.task.SyncTaskExecutor
@Override
public void execute(Runnable task) {
Assert.notNull(task, "Runnable must not be null");
task.run();
}
/**
* Set the ThreadPoolExecutor's core pool size.
* Default is 1.
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
*/
public void setCorePoolSize(int corePoolSize) {
synchronized (this.poolSizeMonitor) {
this.corePoolSize = corePoolSize;
if (this.threadPoolExecutor != null) {
this.threadPoolExecutor.setCorePoolSize(corePoolSize);
}
}
}
public class DataSimulation implements Runnable{
private HourAverageValueDao hourAverageValueDao;
@Override
public void run() {
Random random = new Random();
AverageValue averageValue = new AverageValue();
averageValue.setAverageVaule(random.nextInt(100));
averageValue.setCreatedTime(new Date());
hourAverageValueDao.insert(averageValue);
}
}
public class DataFacotory {
private TaskExecutor executor;
public TaskExecutor getExecutor() {
return executor;
}
public void setExecutor(TaskExecutor executor) {
this.executor = executor;
}
public void dataFactory(){
for (int i =0; i < 10; i++){
executor.execute(new DataSimulation());
}
}
}
<bean id = "taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value = "5"></property> <property name = "maxPoolSize" value="10"></property> <property name="queueCapacity" value="25"></property> </bean>
public interface TaskScheduler {
//通过触发器来决定task是否执行
ScheduledFuture schedule(Runnable task, Trigger trigger);
//在starttime的时候执行一次
ScheduledFuture schedule(Runnable task, Date startTime);
从starttime开始每个period时间段执行一次task
ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long period);
每隔period执行一次
ScheduledFuture scheduleAtFixedRate(Runnable task, long period);
从startTime开始每隔delay长时间执行一次
ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, long delay);
每隔delay时间执行一次
ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay);
}
Date nextExecutionTime(TriggerContext triggerContext);
public interface TriggerContext {
/**
* Return the last <i>scheduled</i> execution time of the task,
* or {@code null} if not scheduled before.
*/
Date lastScheduledExecutionTime();
/**
* Return the last <i>actual</i> execution time of the task,
* or {@code null} if not scheduled before.
*/
Date lastActualExecutionTime();
/**
* Return the last completion time of the task,
* or {@code null} if not scheduled before.
*/
Date lastCompletionTime();
}
<task:annotation-driven scheduler="myScheduler"/> //指定scheduler属性是可选项,不添加也可以正常使用 <task:scheduler id="myScheduler" pool-size="10"/>
@Component
public class SchedulerPoolTest {
@Scheduled(cron = "0 * * * * ?")
public void task1(){
System.out.println("test");
Thread thread = Thread.currentThread();
System.out.println("ThreadName:" + thread.getName() + ",id:" + thread.getId() + ",group:" + thread.getThreadGroup());
}
@Scheduled(fixedDelay = 5000)
public void task2(){
System.out.println("test");
Thread thread = Thread.currentThread();
System.out.println("ThreadName:" + thread.getName() + ",id:" + thread.getId() + ",group:" + thread.getThreadGroup());
}
}
<context:component-scan base-package="com.zjut.task" />
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-task.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
{秒} {分} {时} {日} {月} {周}
字段名 允许的值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日 1-31 , - * ? / L W C 月 1-12 or JAN-DEC , - * / 周几 1-7 or SUN-SAT , - * ? / L C #
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 "15-30/5 * * * * ?" 每分钟的15秒到30秒之间开始触发,每隔5秒触发一次 "0 15 10 ? * 5#3" 每个月第三周的星期四的10点15分0秒触发任务
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有