Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring调度程序:在使用cron和启动时运行一次之间切换_Java_Spring_Cron_Spring Scheduled - Fatal编程技术网

Java Spring调度程序:在使用cron和启动时运行一次之间切换

Java Spring调度程序:在使用cron和启动时运行一次之间切换,java,spring,cron,spring-scheduled,Java,Spring,Cron,Spring Scheduled,我设法让一个简单的应用程序在spring应用程序上下文中运行,并通过@Scheduled: @计划(cron=“0 30 9***,zone=“澳大利亚/悉尼”) 有没有办法让@Scheduled在启动时运行 我的想法是在application.properties中设置一个开关,例如: scheduler.triggernow=true 其中,如果设置为true,应用程序将忽略spring cron计划并立即运行(启动时运行一次),如果设置为false,则将使用上面的spring cron计划

我设法让一个简单的应用程序在spring应用程序上下文中运行,并通过@Scheduled:

@计划(cron=“0 30 9***,zone=“澳大利亚/悉尼”)

有没有办法让@Scheduled在启动时运行

我的想法是在application.properties中设置一个开关,例如:

scheduler.triggernow=true

其中,如果设置为true,应用程序将忽略spring cron计划并立即运行(启动时运行一次),如果设置为false,则将使用上面的spring cron计划并在每天上午9:30运行

  • 我去获取一个基本的应用程序(maven项目、java、spring boot 2.3.4)

  • 我看了一段YouTube视频来帮助设置调度程序:

  • 在我的主课上

    @SpringBoot应用程序

    @EnableConfigurationProperties

    公开课演示{

    public static void main(String[] args){ 
    
      ConfigurationApplicationContext ctx = new SpringApplicationBuilder(Demo.class)
    
      .web(WebApplicationType.None)
    
      .run(args)
    
    }
    
    }

    在一个新类中,我添加了:

    @配置

    @使能调度 @ConditionalOnProperty(name=“scheduling.enabled”,matchIfMissing=true)

    }

    cron调度器本身就可以工作。我有一个属性可以禁用它。如果我想在启动时运行它一次,我只想禁用它(当前禁用它意味着它什么也不做)


    顺便说一句,这是一个非Prod应用程序。计划是在调度程序运行的情况下部署它。如果我想在本地运行它,那么只需禁用调度程序并在启动时运行它即可

    使用类组件(或服务…)中的
    @PostConstruct
    在启动时执行代码

    下面的示例显示了如何继续:

    @Component
    public class MyClass {
     
        
        @PostConstruct
        private void methodToExecute() {
            doScheduledWOrk(); // call here for startup execution
        }
    
        @Scheduled(cron= "0 30 9 * * *, zone="Australia/Sydney")
        public void scheduleMethod() {
             doScheduledWOrk(); // sample function
        }
    
        public void doScheduledWOrk() {
             //doing some scheduled stuff here... 
        }
    }
    

    你能给我们看一下你创建的一些组件吗?我刚刚添加到文章中。本质上,我使用了一个基本的spring应用程序,并制作了一个简单的cron调度程序,带有一个属性来禁用它。但我不打算对此进行扩展,禁用cron调度程序将意味着在Startupbook上运行一次抱歉,我不擅长在堆栈上格式化代码低。谢谢。这当然是在启动时运行的(我在我的主类中添加了此post contract)。虽然我看到它在启动时和计划时都运行,但我的想法是拥有一个在启动时运行和计划时运行之间切换的属性
    @Component
    public class MyClass {
     
        
        @PostConstruct
        private void methodToExecute() {
            doScheduledWOrk(); // call here for startup execution
        }
    
        @Scheduled(cron= "0 30 9 * * *, zone="Australia/Sydney")
        public void scheduleMethod() {
             doScheduledWOrk(); // sample function
        }
    
        public void doScheduledWOrk() {
             //doing some scheduled stuff here... 
        }
    }