亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用spring-task定時(shí)任務(wù)動(dòng)態(tài)配置修改執(zhí)行時(shí)間

 更新時(shí)間:2021年11月02日 10:00:34   作者:丶人生如夢(mèng)  
這篇文章主要介紹了使用spring-task定時(shí)任務(wù)動(dòng)態(tài)配置修改執(zhí)行時(shí)間,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

spring-task定時(shí)任務(wù)動(dòng)態(tài)配置修改執(zhí)行時(shí)間

因項(xiàng)目需要,幾個(gè)定時(shí)任務(wù)需要人為動(dòng)態(tài)設(shè)置執(zhí)行時(shí)間,于是乎吧,就查閱相關(guān)資料,是可以動(dòng)態(tài)設(shè)置的,廢話不多說,直接上代碼,一目了然。

package com.seckill.quartz;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * Created by loup on 2017/11/11.
 */
@Component
@EnableScheduling
public class DynamicScheduledTask implements SchedulingConfigurer {
    //時(shí)間表達(dá)式  每2秒執(zhí)行一次
    private String cron = "0/2 * * * * ?";
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                //任務(wù)邏輯
                System.out.println("---------------start-------------------");
                System.out.println("動(dòng)態(tài)修改定時(shí)任務(wù)參數(shù),時(shí)間表達(dá)式cron為:" + cron);
                System.out.println("當(dāng)前時(shí)間為:" + sdf.format(new Date()));
                System.out.println("----------------end--------------------");
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                CronTrigger cronTrigger = new CronTrigger(cron);
                Date nextExecDate = cronTrigger.nextExecutionTime(triggerContext);
                return nextExecDate;
            }
        });
    }
 
    public void setCron(String cron) {
        this.cron = cron;
    }
}

這個(gè)是定時(shí)任務(wù)調(diào)度執(zhí)行器,采用的是注解的方式。首先要?jiǎng)討B(tài)配置,要設(shè)置為@EnableScheduling,這是確保能夠動(dòng)態(tài),然后實(shí)現(xiàn)SchedulingConfigurer,重寫configureTasks方法,接下來就是這個(gè)的相關(guān)spring配置文件,要引入下面這個(gè)task,不然識(shí)別不了啊,配置文件就是這么簡(jiǎn)單

http://www.springframework.org/schema/task

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:component-scan base-package="com.seckill.quartz"/> 
    <task:annotation-driven /> 
</beans>

接下來就是寫測(cè)試類,測(cè)試可不可行啊

package com.seckill.quartz;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.IOException;
 
/**
 * Created by loup on 2017/11/11.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:/conf/spring-quartz.xml"})
public class QuartzTest {
 
    @Autowired
    private DynamicScheduledTask dynamicScheduledTask;
 
    @Test
    public void test1(){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        dynamicScheduledTask.setCron("0/10 * * * * ?");
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

運(yùn)行測(cè)試類,查看結(jié)果,達(dá)到效果,親測(cè)可用

spring schedule 動(dòng)態(tài)配置執(zhí)行時(shí)間

之前saas平臺(tái)實(shí)現(xiàn)動(dòng)態(tài)修改定時(shí)任務(wù)的時(shí)間,都是通過xx-job這樣的框架來實(shí)現(xiàn),這樣我們可以單獨(dú)一個(gè)服務(wù)來管理我們整個(gè)saas平臺(tái)的定時(shí)任務(wù),但是最近給銀行做的一個(gè)小項(xiàng)目,需要本地化部署,所以我不想弄很多的服務(wù),并且他們并沒有要求修改以后即時(shí)生效,所以我直接采用了 spring schedule結(jié)合mysql動(dòng)態(tài)配置執(zhí)行時(shí)間。

之前我們用的schedule通過注解的方式,只能用靜態(tài)的corn表達(dá)式,如果想實(shí)現(xiàn)動(dòng)態(tài)的需要實(shí)現(xiàn)SchedulingConfigurer,并且通過注解@EnableScheduling。如下:

package com.zqf.marketing.task;  
import com.zqf.db.marketingrobot.sys.model.RobotSysSwitch;
import com.zqf.marketing.sys.service.SwitchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service; 
import java.util.Date;
 
/**
 * @author zhenghao
 * @description
 * @date 2019/1/22 21:50
 */
 
@Lazy(false)
@Service
@EnableScheduling
public class TestTaskService implements SchedulingConfigurer { 
    private static Logger log = LoggerFactory.getLogger(TestTaskService.class);
    @Autowired
    private SwitchService switchService;  
    private String SpringDynamicCronTask() {
        String cron = "0/5 * * * * ?";
        //從數(shù)據(jù)庫獲得配置的corn表達(dá)式
        RobotSysSwitch switchById = switchService.getSwitchById(5L);
        cron = switchById.getSwitchFlag();
        log.info(cron);
        return cron;
    } 
 
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                // 任務(wù)邏輯
                log.info("task_task_tak");
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                String s = SpringDynamicCronTask();
                // 任務(wù)觸發(fā),可修改任務(wù)的執(zhí)行周期
                CronTrigger trigger = new CronTrigger(s);
                Date nextExec = trigger.nextExecutionTime(triggerContext);
                return nextExec;
            }
        });  
    }
}

這樣我們就可以動(dòng)態(tài)的修改task的執(zhí)行時(shí)間,生效時(shí)間為,上一個(gè)任務(wù)的執(zhí)行周期,也可以滿足我們現(xiàn)在需求,這樣就可以實(shí)習(xí)項(xiàng)目更加的靈活!

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解@Autowired是如何注入變量的

    詳解@Autowired是如何注入變量的

    在?Spring?容器中,當(dāng)我們想給某一個(gè)屬性注入值的時(shí)候,有多種不同的方式,例如使用?@Autowired、@Inject等注解,下面小編就來和小伙伴們聊一聊,@Autowired?到底是如何把數(shù)據(jù)注入進(jìn)來的
    2023-07-07
  • mybatis接收以逗號(hào)分隔的字符串批量查詢方式

    mybatis接收以逗號(hào)分隔的字符串批量查詢方式

    這篇文章主要介紹了mybatis接收以逗號(hào)分隔的字符串批量查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 使用maven命令安裝jar包到本地倉庫的方法步驟

    使用maven命令安裝jar包到本地倉庫的方法步驟

    這篇文章主要介紹了使用maven命令安裝jar包到本地倉庫的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Java經(jīng)典設(shè)計(jì)模式之觀察者模式原理與用法詳解

    Java經(jīng)典設(shè)計(jì)模式之觀察者模式原理與用法詳解

    這篇文章主要介紹了Java經(jīng)典設(shè)計(jì)模式之觀察者模式,簡(jiǎn)單分析了觀察者模式的概念、原理并結(jié)合實(shí)例形式給出了java觀察者模式的具體用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-08-08
  • Spring Security整合KeyCloak保護(hù)Rest API實(shí)現(xiàn)詳解

    Spring Security整合KeyCloak保護(hù)Rest API實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了Spring Security整合KeyCloak保護(hù)Rest API實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • 對(duì)比Java設(shè)計(jì)模式編程中的狀態(tài)模式和策略模式

    對(duì)比Java設(shè)計(jì)模式編程中的狀態(tài)模式和策略模式

    這篇文章主要介紹了Java設(shè)計(jì)模式編程中的狀態(tài)模式和策略模式對(duì)比,文中列舉了兩種模式的相似點(diǎn)和不同點(diǎn),并都舉了代碼的實(shí)例作為參照,需要的朋友可以參考下
    2016-04-04
  • Java解決浮點(diǎn)數(shù)計(jì)算不精確問題的方法詳解

    Java解決浮點(diǎn)數(shù)計(jì)算不精確問題的方法詳解

    在 Java 中,浮點(diǎn)數(shù)計(jì)算不精確問題指的是使用浮點(diǎn)數(shù)進(jìn)行運(yùn)算時(shí),由于浮點(diǎn)數(shù)的內(nèi)部表示方式和十進(jìn)制數(shù)的表示方式存在差異,導(dǎo)致計(jì)算結(jié)果可能出現(xiàn)誤差,本文就給大家介紹一下Java如何解決浮點(diǎn)數(shù)計(jì)算不精確問題,需要的朋友可以參考下
    2023-09-09
  • Java線程池隊(duì)列LinkedBlockingDeque

    Java線程池隊(duì)列LinkedBlockingDeque

    這篇文章主要為大家介紹了Java線程池隊(duì)列LinkedBlockingDeque示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • MyBatis?@Select注解介紹:基本用法與動(dòng)態(tài)SQL拼寫方式

    MyBatis?@Select注解介紹:基本用法與動(dòng)態(tài)SQL拼寫方式

    這篇文章主要介紹了MyBatis?@Select注解介紹:基本用法與動(dòng)態(tài)SQL拼寫方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • SpringBoot整合spring-retry實(shí)現(xiàn)接口請(qǐng)求重試機(jī)制及注意事項(xiàng)

    SpringBoot整合spring-retry實(shí)現(xiàn)接口請(qǐng)求重試機(jī)制及注意事項(xiàng)

    今天通過本文給大家介紹我們應(yīng)該如何使用SpringBoot來整合spring-retry組件實(shí)現(xiàn)重試機(jī)制及注意事項(xiàng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-08-08

最新評(píng)論