基于ThreadPoolTaskScheduler的定时任务自动执行和手动开启关闭

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;

@RestController
@RequestMapping("task")
public class TestController {
    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    public static Map<String, ScheduledFuture<?>> taskMap=new HashMap<>();

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        return new ThreadPoolTaskScheduler();
    }

    @RequestMapping("/is_start")
    public boolean isStart() {
        TestController.TaskThread tt=new TestController.TaskThread();
        ScheduledFuture<?> future=taskMap.get("taskName");
        if(future != null && !future.isCancelled()){
            return true;
        }else {
            return false;
        }
    }

    @RequestMapping("/start")
    public String start() {
        TestController.TaskThread tt=new TestController.TaskThread();
        ScheduledFuture<?> future=threadPoolTaskScheduler.schedule(tt, new CronTrigger("0/5 * * * * *"));
        taskMap.put("taskName", future);
        System.out.println("开始");
        return "start";
    }

    @RequestMapping("/stop")
    public String stop() {
        ScheduledFuture<?> future=taskMap.get("taskName");
        if (future != null) {
            future.cancel(true);
        }
        System.out.println("停止");
        return "stop";
    }

    @RequestMapping("/change")
    public String change() {
        stop();// 先停止,在开启.
        ScheduledFuture<?> future=taskMap.get("checkTask");
        TestController.TaskThread tt=new TestController.TaskThread( );
        future=threadPoolTaskScheduler.schedule(tt, new CronTrigger("*/10 * * * * *"));
        System.out.println("修改");
        return "changeCron";
    }

    //内部类
    public class TaskThread implements Runnable {
        @Override
        public void run() {
            //根据传来的参数执行要定时的任务
            //...
        }
    }

    @Component
    public class ApplicationTask implements ApplicationListener<ContextRefreshedEvent> {
        @Override
        public void onApplicationEvent(ContextRefreshedEvent contextStartedEvent) {
            System.out.println("执行开机启动项目");
            start();
        }
    }
}
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ThreadPoolTaskScheduler 是 Spring 框架中提供的一个线程池定时任务调度器,可以在预定的时间间隔或固定的时间执行任务。它可以用于执行异步任务,如发送邮件或者短信,定时从数据库或者第三方接口获取数据等。 ThreadPoolTaskScheduler 可以通过配置线程池的大小来控制并发执行任务的数量,从而避免系统资源浪费。它也提供了一些方法来控制定时任务执行,如取消任务、暂停任务、恢复任务等。 下面是一个使用 ThreadPoolTaskScheduler 调度定时任务的示例: 首先,需要在 Spring 配置文件中配置 ThreadPoolTaskScheduler定时任务: ```xml <bean id="taskScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"> <property name="poolSize" value="5"/> </bean> <bean id="myTask" class="com.example.MyTask"/> <task:scheduled-tasks> <task:scheduled ref="myTask" method="run" fixed-delay="5000"/> </task:scheduled-tasks> ``` 其中,taskScheduler 配置了线程池大小为 5,myTask 是一个实现了 Runnable 接口的类,run 方法是定时任务执行的方法,fixed-delay 属性表示每隔 5 秒执行一次。 接着,在 MyTask 类中实现 run 方法: ```java public class MyTask implements Runnable { @Override public void run() { // 执行定时任务的逻辑 System.out.println("定时任务执行了!"); } } ``` 这样就可以使用 ThreadPoolTaskScheduler 定时执行 MyTask 中的 run 方法了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值