定時(shí)任務(wù)的五種創(chuàng)建方式,你都會(huì)么?

Quartz表達(dá)式生成地址:http://cron.qqe2.com/

支持生成定時(shí)任務(wù)表達(dá)式和反解析,使用Quartz表達(dá)式的定時(shí)任務(wù)如下

  • xxl-job

  • springboot 的 @Scheduled

  • Quartz 框架

一、job 定時(shí)任務(wù)的五種創(chuàng)建方式

1、使用線程創(chuàng)建 job 定時(shí)任務(wù)

/**

  • TODO 使用線程創(chuàng)建 job 定時(shí)任務(wù)

  • @author 王松
    */
    public class JobThread {

    public static class Demo01 {
    static long count = 0;

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                        count++;
                        System.out.println(count);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }
    

    }
    }

2、使用 TimerTask 創(chuàng)建job定時(shí)任務(wù)

/**

  • TODO 使用 TimerTask 創(chuàng)建job定時(shí)任務(wù)

  • @author 王松
    */
    public class JobTimerTask {

    static long count = 0;
    public static void main(String[] args) {
    TimerTask timerTask = new TimerTask() {
    @Override
    public void run() {
    count++;
    System.out.println(count);
    }
    };
    //創(chuàng)建timer對(duì)象設(shè)置間隔時(shí)間
    Timer timer = new Timer();
    // 間隔天數(shù)
    long delay = 0;
    // 間隔毫秒數(shù)
    long period = 1000;
    timer.scheduleAtFixedRate(timerTask, delay, period);
    }
    }

3、使用線程池創(chuàng)建 job定時(shí)任務(wù)

/**

  • TODO 使用線程池創(chuàng)建 job定時(shí)任務(wù)
  • @author 王松
    */
    public class JobScheduledExecutorService {
    public static void main(String[] args) {
    Runnable runnable = new Runnable() {
    @Override
    public void run() {
    // task to run goes here
    System.out.println("Hello !!");
    }
    };
    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    // 第二個(gè)參數(shù)為首次執(zhí)行的延時(shí)時(shí)間,第三個(gè)參數(shù)為定時(shí)執(zhí)行的間隔時(shí)間
    service.scheduleAtFixedRate(runnable, 1, 1, TimeUnit.SECONDS);
    }
    }

4.Quartz 框架

1.引入maven依賴

<dependencies>

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>

2.任務(wù)調(diào)度類

public class MyJob implements Job {

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    System.out.println("quartz MyJob date:" + System.currentTimeMillis());
}

}

3.啟動(dòng)類

public class JobQuartz {

public static void main(String[] args) throws SchedulerException {
    //1.創(chuàng)建Scheduler的工廠
    SchedulerFactory sf = new StdSchedulerFactory();
    //2.從工廠中獲取調(diào)度器實(shí)例
    Scheduler scheduler = sf.getScheduler();
    //3.創(chuàng)建JobDetail,
    JobDetail jb = JobBuilder.newJob(MyJob.class)
            //job的描述
            .withDescription("this is a ram job")
            //job 的name和group
            .withIdentity("ramJob", "ramGroup")
            .build();
    //任務(wù)運(yùn)行的時(shí)間,SimpleSchedle類型觸發(fā)器有效,3秒后啟動(dòng)任務(wù)
    long time=  System.currentTimeMillis() + 3*1000L;
    Date statTime = new Date(time);
    //4.創(chuàng)建Trigger
    //使用SimpleScheduleBuilder或者CronScheduleBuilder
    Trigger t = TriggerBuilder.newTrigger()
            .withDescription("")
            .withIdentity("ramTrigger", "ramTriggerGroup")
            //.withSchedule(SimpleScheduleBuilder.simpleSchedule())
            //默認(rèn)當(dāng)前時(shí)間啟動(dòng)
            .startAt(statTime)
            //兩秒執(zhí)行一次,Quartz表達(dá)式,支持各種牛逼表達(dá)式
            .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?"))
            .build();
    //5.注冊(cè)任務(wù)和定時(shí)器
    scheduler.scheduleJob(jb, t);
    //6.啟動(dòng) 調(diào)度器
    scheduler.start();
}

5. springboot 的 @Scheduled 注解

@Component
@Configuration //1.主要用于標(biāo)記配置類,兼?zhèn)銫omponent的效果。
@EnableScheduling // 2.開(kāi)啟定時(shí)任務(wù)
public class SaticScheduleTask {

@Scheduled(cron = "0/5 * * * * ?")  //3.添加定時(shí)任務(wù)
//@Scheduled(fixedRate=5000)        //或直接指定時(shí)間間隔,例如:5秒
private void configureTasks() {
    System.err.println("執(zhí)行靜態(tài)定時(shí)任務(wù)時(shí)間: " + LocalDateTime.now());
}

}

二、xxl-job 任務(wù)調(diào)度后臺(tái) Admin

xxl-job 有什么用?

  • 分布式集群的情況下,保證定時(shí)任務(wù)不被重復(fù)執(zhí)行。

  • 執(zhí)行原理同Nginx 類型,所有定時(shí)任務(wù)通過(guò)任務(wù)調(diào)度平臺(tái)分發(fā),也可配置負(fù)載均衡等等

  • 首先讓我們能夠使用起來(lái),搭建一個(gè)自己的任務(wù)

第一步: github下載源碼導(dǎo)入

下載地址:https://github.com/xuxueli/xxl-job/

當(dāng)前版本目錄結(jié)構(gòu) 2.1.1

第二步: 執(zhí)行sql

文件地址:xxl-job/doc/db/tables_xxl_job.sql

當(dāng)前2.1.1版本sql

第三步: 修改xxl-job-admin項(xiàng)目配置

配置文件:application.properties

修改數(shù)據(jù)庫(kù)連接

第四步: 啟動(dòng)admin項(xiàng)目

springboot 方式啟動(dòng)項(xiàng)目,

訪問(wèn) http://localhost:8080/xxl-job-admin/

賬號(hào)密碼:admin / 123456

任務(wù)調(diào)度中心就搭建好了

接下來(lái)需要?jiǎng)?chuàng)建一個(gè)服務(wù)器連接任務(wù)調(diào)度中心

三、自創(chuàng)建boot項(xiàng)目的任務(wù)xxl-job 示例demo

創(chuàng)建一個(gè) boot 項(xiàng)目

我的目錄結(jié)構(gòu)

pom.xml

web核心及 xxl-job-core


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.1.1-SNAPSHOT</version>
</dependency>

logback.xml

日志配置直接拷貝

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="1 seconds">

<contextName>logback</contextName>
<property name="log.path" value="/data/applogs/xxl-job/xxl-job-executor-sample-springboot.log"/>

<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${log.path}</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern>
    </rollingPolicy>
    <encoder>
        <pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n
        </pattern>
    </encoder>
</appender>

<root level="info">
    <appender-ref ref="console"/>
    <appender-ref ref="file"/>
</root>

</configuration>

application.properties 加入配置

需修改或自定義

  • xxl-job admin 地址

  • xxl.job.executor.appname 自定義名稱,后臺(tái)配置必須對(duì)應(yīng)

  • xxl.job.executor.ip 當(dāng)前電腦Ip,或部署項(xiàng)目的電腦Ip

  • xxl.job.executor.port 端口

端口號(hào)

server.port=8081

日志

logging.config=classpath:logback.xml

xxl-job admin 地址,多個(gè)逗號(hào)分隔"

xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin

xxl-job名稱 || socket ip 當(dāng)前項(xiàng)目部署的ip地址/本機(jī)ip || socket 端口號(hào)

xxl.job.executor.appname=xxl-job-executor-sample
xxl.job.executor.ip=192.168.43.153
xxl.job.executor.port=9999

xxl-job, access token

xxl.job.accessToken=

xxl-job log path

xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler

xxl-job log retention days

xxl.job.executor.logretentiondays=-1

添加boot配置類 XxlJobConfig

package xxljob.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**

  • xxl-job xxljob.config
    */
    @SuppressWarnings("ALL")
    @Configuration
    public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.executor.appname}")
    private String appName;

    @Value("${xxl.job.executor.ip}")
    private String ip;

    @Value("${xxl.job.executor.port}")
    private int port;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;

    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobSpringExecutor xxlJobExecutor() {
    logger.info(">>>>>>>>>>> xxl-job xxljob.config init.");
    XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
    xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
    xxlJobSpringExecutor.setAppName(appName);
    xxlJobSpringExecutor.setIp(ip);
    xxlJobSpringExecutor.setPort(port);
    xxlJobSpringExecutor.setAccessToken(accessToken);
    xxlJobSpringExecutor.setLogPath(logPath);
    xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
    System.err.println(ip+":"+port);
    return xxlJobSpringExecutor;
    }

    /**

    • 針對(duì)多網(wǎng)卡、容器內(nèi)部署等情況,可借助 "spring-cloud-commons" 提供的 "InetUtils" 組件靈活定制注冊(cè)IP;
    •  1、引入依賴:
      
    •      <dependency>
      
    •         <groupId>org.springframework.cloud</groupId>
      
    •         <artifactId>spring-cloud-commons</artifactId>
      
    •         <version>${version}</version>
      
    •     </dependency>
      
    •  2、配置文件,或者容器啟動(dòng)變量
      
    •      spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
      
    •  3、獲取IP
      
    •      String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
      

    */
    }

任務(wù)job

@JobHandler(value="demoJobHandler")
@Component
public class DemoJobHandler extends IJobHandler {

 static int count;
@Override
public ReturnT<String> execute(String param) throws Exception {
    System.out.println("執(zhí)行job任務(wù)"+count++);
    return SUCCESS;
}

}

admin 后臺(tái)配置

執(zhí)行管理器下

任務(wù)管理下編輯任務(wù)

定時(shí)規(guī)則生成:http://cron.qqe2.com/

job任務(wù)名:@JobHandler注解值 >> 如:@JobHandler(value=“demoJobHandler”)

啟動(dòng)

這樣就配置完成了

完成

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容