ThreadPoolTaskScheduler的使用

配置线程池

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

/**
 * @author lyd
 * @Description:
 * @date 15:23
 */
@Configuration
public class ThreadSchedulerConfig {
	
	/**
	 * 线程存储器
	 */
	public static ConcurrentHashMap<String, ScheduledFuture> map = new ConcurrentHashMap<String, ScheduledFuture>();


	@Bean
	public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
		ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
		// 配置池子的大小
		threadPoolTaskScheduler.setPoolSize(20);
		// 设置名字
		threadPoolTaskScheduler.setThreadNamePrefix("taskExecutor-");
		// 设置等待任务在关机时完成
		threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
		// 设置等待终止时间
		threadPoolTaskScheduler.setAwaitTerminationSeconds(60);

		return threadPoolTaskScheduler;
	}



}

编写任务类,实现启动、停止功能

package com.example.demo.task;

import com.example.demo.DemoApplication;
import com.example.demo.config.ThreadSchedulerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.concurrent.ScheduledFuture;


/**
 * @author lyd
 * @Description:
 * @date 15:32
 */
@Component
@Scope("prototype")//多例
public class DynamicTask {

	Logger logger = LoggerFactory.getLogger(DynamicTask.class);

	String cron;

	@Autowired
	ThreadPoolTaskScheduler threadPoolTaskScheduler;

	ScheduledFuture scheduledFuture;

	/**
	 * 传入定时任务
	 */
	public void startCron() {

		cron = "0/1 * * * * ?";
		System.out.println(Thread.currentThread().getName());
		String name = Thread.currentThread().getName();
		scheduledFuture = threadPoolTaskScheduler.schedule(new myTask(name), new CronTrigger(cron));
		ThreadSchedulerConfig.map.put(name, scheduledFuture);
	}


	public void stop(ScheduledFuture future) {
		if (future != null) {
			future.cancel(true);
		}
	}

	/**
	 * 写一个任务需求
	 */
	private class myTask implements Runnable {
		private String name;

		myTask(String name) {
			this.name = name;
		}

		@Override
		public void run() {
			System.out.println("test:" + name);
		}
	}

}

测试接口


import com.example.demo.config.ThreadSchedulerConfig;
import com.example.demo.task.DynamicTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.ScheduledFuture;

/**
 * @author lyd
 * @Description:
 * @date 16:06
 */
@RestController
public class TestController {

	@Autowired
	DynamicTask dynamicTask;

	@RequestMapping("startTask")
	public void startTask() {
		// 开启定时任务,多例
		dynamicTask.startCron();
	}

	@RequestMapping("stopTask")
	public void stopTask() {
		ScheduledFuture scheduledFuture = ThreadSchedulerConfig.map.get("http-nio-8080-exec-1");
		dynamicTask.stop(scheduledFuture);
	}

}
posted @ 2021-06-30 16:58  西红柿里没有番茄  阅读(2563)  评论(3编辑  收藏  举报