使用ThreadPoolTaskScheduler而不使用Spring框架

huangapple go评论72阅读模式
英文:

Use ThreadPoolTaskScheduler without using Spring framework

问题

我正在尝试使用ThreadPoolTaskScheduler来使用CRON调度任务,但不使用Spring框架。我创建了两个任务调度器,分别用于每天和每周。

到目前为止,这是我尝试过的内容:

public class MyClass {

    public static void main(String args[]) {
        MyClass mc = new MyClass();
        mc.runTaskScheduler();
    }

    private void runTaskScheduler() {
        Runnable dailyScheduler = this::processDaily;
        Runnable weeklyScheduler = this::processWeekly;

        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.initialize();
        threadPoolTaskScheduler.schedule(dailyScheduler, new CronTrigger("0 * * * * *")); //0 * * * * *
        threadPoolTaskScheduler.schedule(weeklyScheduler, new CronTrigger("0 1 * * * *")); //0 1 * * * *
    }

    private void processDaily() {
        // some code
    }

    private void processWeekly() {
        // some code
    }

}

我不确定是否需要将ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); 声明为静态变量?我尝试创建了一个主类,但不确定是否做得正确。

英文:

I am trying to use ThreadPoolTaskScheduler to schedule task using CRON but without using Spring framework. I created 2 task scheduler for daily and weekly.

So, far here is what i tried:

public class MyClass {

        public static void main(String args[]) {
           MyClass mc = new MyClass();
           mc.runTaskScheduler();
        }

        private void runTaskScheduler() {
            Runnable dailyScheduler = this::processDaily;
            Runnable weeklyScheduler = this::processWeekly;

            ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
            threadPoolTaskScheduler.initialize();
            threadPoolTaskScheduler.schedule(dailyScheduler, new CronTrigger("0 * * * * *")); //0 * * * * *
        threadPoolTaskScheduler.schedule(weeklyScheduler, new CronTrigger("0 1 * * * *")); //0 1 * * * *
        
        }

        private void processDaily() {
           // some code
        }

        private void processWeekly() {
          // some code
        }

    }

I am not sure if should i need to make ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); static variable?

I tried to make a main class but not sure if I did it correct.

答案1

得分: 2

以下是翻译好的部分:

首先,如果你使用Spring Scheduling库,你正在使用Spring框架。Spring旨在让你的生活更轻松,因此它使用了许多预配置的内容,以防你没有提供任何特定的配置。如果你不想使用Spring但仍需要类似cron的调度,请查看Quartz Job Scheduler

其次,你的Cron作业不是每天和每周,而是每分钟和每小时。根据文档,每天是0 0 0 * * *,每周是0 0 0 * * 0

最后,你的代码是有效的。没有必要将ThreadPoolTaskScheduler标记为静态的。当然,如果你只想使用一个实例,你可以将ThreadPoolTaskScheduler转换为单例,但要注意,许多人认为单例是一种代码异味。

或者你可以将调度器作为类变量使用...

这里提供了两个示例,以防你想要避免多个调度器实例或稍后访问它。

英文:

There are couple of things to mention...

First of all, if you use the Spring Scheduling library you are using the Spring framework. Spring is intended to make your live easier, therefore it uses a lot of preconfigured stuff, in case you do not provide any particular configuration.
If you do not want to use Spring and still need a cron like scheduling, take a look at the Quartz Job Scheduler.

Second, your Cron job is not daily and weekly, but minutely and hourly.
According to the docu, daily is 0 0 0 * * * and weekly 0 0 0 * * 0

Finally, your code works. There is no need to flag the ThreadPoolTaskScheduler as static. Of course you could turn your ThreadPoolTaskScheduler into a Singleton, if you want to use just a single instance of it. But be aware, that in the opinion of many, Singletons are a code smell.

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;

import java.util.Date;
    
public class MyClass {

    private static ThreadPoolTaskScheduler THREAD_POOL_TASK_SCHEDULER;

    public static ThreadPoolTaskScheduler getInstance () {
        if (THREAD_POOL_TASK_SCHEDULER == null) {
            THREAD_POOL_TASK_SCHEDULER = new ThreadPoolTaskScheduler();
            THREAD_POOL_TASK_SCHEDULER.initialize();
        }
        return THREAD_POOL_TASK_SCHEDULER;
    }

    public static void main(String args[]) {
        MyClass mc = new MyClass();
        mc.runTaskScheduler();
    }

    private void runTaskScheduler() {
        Runnable minutelyScheduler = this::processMinutely;
        getInstance().schedule(minutelyScheduler, new CronTrigger("0 * * * * *")); //0 1 * * * *
    }

    private void processMinutely() {
        System.out.println("Time " + new Date());
    }

}

Or you could just use the scheduler as a class variable...

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;

import java.util.Date;

public class MyClass {

    public static void main(String args[]) {
        MyClass mc = new MyClass();
        mc.runTaskScheduler();
    }

    private final ThreadPoolTaskScheduler threadPoolTaskScheduler;

    public MyClass() {
        threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.initialize();
    }

    private void runTaskScheduler() {
        Runnable minutelyScheduler = this::processMinutely;
        threadPoolTaskScheduler.schedule(minutelyScheduler, new CronTrigger("0 * * * * *")); //0 1 * * * *
    }

    private void processMinutely() {
        System.out.println("Time " + new Date());
    }

}

Both examples make sense, in case you want to avoid multiple instances of the scheduler or if you want to access it later on.

答案2

得分: 0

如果您只使用Java而不是Spring/SpringBoot,您可以选择使用一些第三方库,例如Quartz Job Scheduler,或者使用Java提供的类。以下是您可能想要使用的类:
ScheduledExecutorService 接口及其实现:ScheduledThreadPoolExecutor。还可以查看类Executors以及其方法public static ScheduledExecutorService newSingleThreadScheduledExecutor()

另外,就第三方库而言,我编写了自己的调度器。请查看Javadoc。这个后台运行程序是Open Source MgntUtils库的一部分,由我编写和维护。该库的源代码包含了如何使用后台运行程序的示例。您可以从Github 这里 下载该库和源代码,并在这里 查看示例。此外,该库还可以从Maven中央仓库获取Maven构件,链接在这里

英文:

If you work with just Java and not Spring/SpringBoot your options are to use some 3d party library such as Quartz Job Scheduler or use Java provided classes for that. Here are the classes you might want to use:
ScheduledExecutorService interface and its implementation: ScheduledThreadPoolExecutor. Also look at class Executors and its method public static ScheduledExecutorService newSingleThreadScheduledExecutor(). <br><br>
Also, as far as 3d parties go I wrote my own scheduler. Look at Javadoc. This Background runner comes as part of Open Source MgntUtils library written and maintained by me. Also the source code of the library contains working example on how to use the Background Runner. The library including the source code can be downloaded from Github here and an example can be seen here. Also, the library can be obtained as Maven artifacts from Maven central here

huangapple
  • 本文由 发表于 2023年1月9日 12:47:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定