菜单
登录注册
欢 迎
登录
自动登录
忘记密码?
新朋友
注册
注册
老朋友
登录
笔记内容为空!
TA的笔记树 >>
SpringBoot 自定义任务(ThreadPoolTaskScheduler)工具类
kotlin工具类
> springboot 定时任务工具类,可自定义添加、删除 ``` import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.annotation.Bean import org.springframework.core.env.Environment import org.springframework.core.env.get import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler import org.springframework.scheduling.support.CronTrigger import org.springframework.stereotype.Component import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ScheduledFuture /** * Created by vanki on 2019-05-09 10:17. */ @Component class MyTaskScheduler
( env: Environment ) { @Autowired private lateinit var threadPoolTaskScheduler: ThreadPoolTaskScheduler private val taskMap = ConcurrentHashMap
>() // 线程池大小(同时执行任务数) private val poolSize = env["task.thread-pool.size"]?.toIntOrNull() ?: 4 @Bean fun threadPoolTaskScheduler(): ThreadPoolTaskScheduler { val threadPool = ThreadPoolTaskScheduler() threadPool.poolSize = this.poolSize return threadPool } fun exists(key: K): Boolean { return this.taskMap.containsKey(key) } fun addTask(key: K, task: Runnable, cron: CronTrigger): Boolean { val schedule = this.threadPoolTaskScheduler.schedule(task, cron) ?: return false return if (this.removeTask(key, false)) { taskMap[key] = schedule true } else { false } } fun removeTask(key: K, mayInterruptIfRunning: Boolean = false): Boolean { val future = this.taskMap[key] ?: return true future.cancel(mayInterruptIfRunning) val isCancelled = future.isCancelled if (isCancelled) { this.taskMap.remove(key) } return isCancelled } fun getTask(key: K): ScheduledFuture<*>? { return this.taskMap[key] } fun count(): Int { return this.taskMap.size } fun aliveCount(): Int { var count = 0 taskMap.forEach { if (!it.value.isCancelled) { count++ } } return count } } ```
vanki
故乡的云。上古的玉。随手的诗。十九岁的你。
浏览:
4788
创建:
2019-05-09 15:26:24
更新:
2019-05-09 15:30:27
TA的最新笔记
spring-boot配置redis多数据源
linux源修改(阿里)
python安装postgresql依赖
arthas使用
java基于spring的NamedParameterJdbcTemplate封装的获取sql工具类
Impala添加负载
S3常用使用
redis常用操作
hdfs相关命令
crontab使用
TA的最热笔记
java异步http请求工具类(org.asynchttpclient)
iTerm2主题配置与常用技巧
java基于spring.redisTemplate实现分布式锁工具类
Kotlin + SpringBoot + JPA(Hibernate) + Repository自定义方法
IDEA汉化
Sequel Pro连接mysql8打开数据库报错
centos-Hadoop2.7.3完全分布式搭建(HA)
SpringBoot上传文件报错(The temporary upload location [..] is not valid)
mac常用软件
kotlin对象属性值拷贝工具类