spring(3)定时任务小结

一 配置

spring的任务默认是单线程的,会有阻塞问题,所以需要配置线程池

<task:scheduler id="myScheduler" pool-size="5"/>

详见:spring定时任务详解(@Scheduled注解)

二 异步

@Async

参考:

  1. @Async会导致fixedDelay失效
	@Async
	@Scheduled(initialDelay=1000,fixedDelay=2000)
	public void test2() {
		logger.info(Thread.currentThread().getName()+"===test2 start");
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logger.info(Thread.currentThread().getName()+"===test2 end");
	}

结果:间隔两秒执行

2019-04-25 23:19:28.381  INFO 19272 --- [cTaskExecutor-1] com.example.demo.task.Mytask             : SimpleAsyncTaskExecutor-1===test2 start
2019-04-25 23:19:30.371  INFO 19272 --- [cTaskExecutor-2] com.example.demo.task.Mytask             : SimpleAsyncTaskExecutor-2===test2 start
2019-04-25 23:19:32.372  INFO 19272 --- [cTaskExecutor-3] com.example.demo.task.Mytask             : SimpleAsyncTaskExecutor-3===test2 start
2019-04-25 23:19:33.381  INFO 19272 --- [cTaskExecutor-1] com.example.demo.task.Mytask             : SimpleAsyncTaskExecutor-1===test2 end
2019-04-25 23:19:34.374  INFO 19272 --- [cTaskExecutor-4] com.example.demo.task.Mytask             : SimpleAsyncTaskExecutor-4===test2 start
2019-04-25 23:19:35.372  INFO 19272 --- [cTaskExecutor-2] com.example.demo.task.Mytask             : SimpleAsyncTaskExecutor-2===test2 end
2019-04-25 23:19:36.375  INFO 19272 --- [cTaskExecutor-5] com.example.demo.task.Mytask             : SimpleAsyncTaskExecutor-5===test2 start
2019-04-25 23:19:37.373  INFO 19272 --- [cTaskExecutor-3] com.example.demo.task.Mytask             : SimpleAsyncTaskExecutor-3===test2 end
  1. @Async会导致fixRate可以并发执行

springboot中的多线程并行应用

Spring定时任务并行(异步)处理

SchedulingConfigurer

1.实现动态定时任务

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		taskRegistrar.addFixedRateTask(() -> System.out.println("执行定时任务1: " + new Date()), 1000);
		TriggerTask triggrtTask = new TriggerTask( // 任务内容.拉姆达表达式
				() -> {System.out.println("执行定时任务2: " + new Date());},
				// 设置触发器,这里是一个拉姆达表达式,传入的TriggerContext类型,返回的是Date类型
				triggerContext -> {
					// 2.3 返回执行周期(Date)
					return new CronTrigger("*/2 * * * * ?").nextExecutionTime(triggerContext);
				});
 
		taskRegistrar.addTriggerTask(triggrtTask);
	}

参考:https://cloud.tencent.com/developer/article/1362794

2.实现并行,设置线程池

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;

@Configuration 
@EnableScheduling 
@EnableAsync( mode = AdviceMode.PROXY, proxyTargetClass = false, order=Ordered.HIGHEST_PRECEDENCE ) 
@ComponentScan( basePackages = “hello” ) 
public class RootContextConfiguration implements AsyncConfigurer, SchedulingConfigurer { 

@Bean 
public ThreadPoolTaskScheduler taskScheduler() { 
  ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); 
  scheduler.setPoolSize(20); 
  scheduler.setThreadNamePrefix(“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); 
} 
} 

AsyncConfigurer

主要也是配置线程池
SpringBoot 线程池配置 实现AsyncConfigurer接口

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
- chapter1:[基本项目构建(可作为工程脚手架),引入web模块,完成一个简单的RESTful API](http://blog.didispace.com/spring-boot-learning-1/) - [使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程](http://blog.didispace.com/spring-initializr-in-intellij/) ### 工程配置 - chapter2-1-1:[配置文件详解:自定义属性、随机数、多环境配置等](http://blog.didispace.com/springbootproperties/) ### Web开发 - chapter3-1-1:[构建一个较为复杂的RESTful API以及单元测试](http://blog.didispace.com/springbootrestfulapi/) - chapter3-1-2:[使用Thymeleaf模板引擎渲染web视图](http://blog.didispace.com/springbootweb/) - chapter3-1-3:[使用Freemarker模板引擎渲染web视图](http://blog.didispace.com/springbootweb/) - chapter3-1-4:[使用Velocity模板引擎渲染web视图](http://blog.didispace.com/springbootweb/) - chapter3-1-5:[使用Swagger2构建RESTful API](http://blog.didispace.com/springbootswagger2/) - chapter3-1-6:[统一异常处理](http://blog.didispace.com/springbootexception/) ### 数据访问 - chapter3-2-1:[使用JdbcTemplate](http://blog.didispace.com/springbootdata1/) - chapter3-2-2:[使用Spring-data-jpa简化数据访问层(推荐)](http://blog.didispace.com/springbootdata2/) - chapter3-2-3:[多数据源配置(一):JdbcTemplate](http://blog.didispace.com/springbootmultidatasource/) - chapter3-2-4:[多数据源配置(二):Spring-data-jpa](http://blog.didispace.com/springbootmultidatasource/) - chapter3-2-5:[使用NoSQL数据库(一):Redis](http://blog.didispace.com/springbootredis/) - chapter3-2-6:[使用NoSQL数据库(二):MongoDB](http://blog.didispace.com/springbootmongodb/) - chapter3-2-7:[整合MyBatis](http://blog.didispace.com/springbootmybatis/) - chapter3-2-8:[MyBatis注解配置详解](http://blog.didispace.com/mybatisinfo/) ### 事务管理 - chapter3-3-1:[使用事务管理](http://blog.didispace.com/springboottransactional/) - chapter3-3-2:[分布式事务(未完成)] ### 其他内容 - chapter4-1-1:[使用@Scheduled创建定时任务](http://blog.didispace.com/springbootscheduled/) - chapter4-1-2:[使用@Async实现异步调用](http://blog.didispace.com/springbootasync/) #### 日志管理 - chapter4-2-1:[默认日志的配置](http://blog.didispace.com/springbootlog/) - chapter4-2-2:[使用log4j记录日志](http://blog.didispace.com/springbootlog4j/) - chapter4-2-3:[对log4j进行多环境不同日志级别的控制](http://blog

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值