springboot开启、结束定时任务

一、需求:

	定时从redis中取缓存数据并存入MySQL

二、实现步骤

1.controller层

package com.itsys.monitor.controller;

import com.itsys.monitor.common.RedisUtil;
import com.itsys.monitor.conf.MyRunnable;
import com.itsys.monitor.model.ReturnDTO;
import com.itsys.monitor.model.ReturnState;
import com.itsys.monitor.service.ElectricRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ScheduledFuture;

@Slf4j
@RestController
@RequestMapping("/task")
public class DynamicTaskController {

    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ElectricRecordService service;

    private ScheduledFuture<?> future;

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

    @PostMapping("/start")
    public ReturnDTO startCron1() {
        if (future != null) {
            future.cancel(true);
        }
        future = threadPoolTaskScheduler.schedule(new MyRunnable(service,redisUtil), triggerContext -> {
            String cTime = (String) redisUtil.get("cTime");
            String cron;
            //0 0/2 * * * ?    表示每2分钟 执行任务
            //0/2 * * * * ?    表示每2秒钟 执行任务
            if (Integer.parseInt(cTime) == 60){
                 cron = "0 0 */1 * * ?";
            }else {
                cron = "0 0/"+cTime+" * * * ?";
            }
            return new CronTrigger(cron).nextExecutionTime(triggerContext);
        });
        return ReturnState.setSuccessStatus();
    }

    @PostMapping("/stop")
    public ReturnDTO stopCron1() {
        if (future != null) {
            future.cancel(true);
        }
        System.out.println("DynamicTask.stopCron1()");
        return ReturnState.setSuccessStatus();
    }

}

2.任务实现类

package com.itsys.monitor.conf;

import com.itsys.monitor.common.RedisUtil;
import com.itsys.monitor.model.ElectricRecord;
import com.itsys.monitor.service.ElectricRecordService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class MyRunnable implements Runnable {

    private ElectricRecordService service;

    private RedisUtil redisUtil;

    public MyRunnable(ElectricRecordService service, RedisUtil redisUtil) {
        this.service = service;
        this.redisUtil = redisUtil;
    }

    @SneakyThrows
    @Override
    public void run() {
        Map<Object, Object> map = redisUtil.hmget("electric");
        log.info("持久化电流数据【{}】", map);
        map.forEach((key, value) -> service.add((ElectricRecord) value));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值