简体   繁体   English

在Spring Boot中,有什么方法可以在主方法中调用@Scheduled批注

[英]Is there any way to call @Scheduled annotation in main method in spring boot

I just want to run my spring boot main method periodically by using @scheduler annotation. 我只想通过使用@scheduler注释定期运行我的spring boot main方法。 I have specified some additional code which will perform some pre-action before it enables REST services. 我指定了一些其他代码,这些代码将在启用REST服务之前执行一些预操作。

@EnableScheduling
@SpringBootApplication
public class SpringBootJDBCApp {

    @Autowired
    ITest testService;

    public static void main(String args[]) throws Exception  {
        PersistenceValidation.cloneGit();
        PersistenceValidation.dataPersistance();
        PersistenceValidation.cleanUp();
        ApplicationContext context = SpringApplication
                .run(SpringBootJDBCApp.class);
        ITest testService = context.getBean(ITestService.class);
        testService.getAllData();
    }
}

I want to run the above main method every 10seconds once. 我想每10秒运行一次以上main方法。 and Added @Schedule annotation at main method. 并在主要方法中添加了@Schedule注释。 But It throws an exception: 但是它抛出一个异常:

Expected behavior as per doc @Scheduler should be called a method which doesn't have args[] 根据doc @Scheduler的预期行为应称为没有args []的方法

I wanna use @Scheduler annotation in main method as below: 我想在主要方法中使用@Scheduler注释,如下所示:

@Scheduled(initialDelay = 1000, fixedRate = 10000)
public static void main(String args[]) throws Exception {
    PersistenceValidation.cloneGit();
    PersistenceValidation.dataPersistance();
    PersistenceValidation.cleanUp();
    ApplicationContext context = SpringApplication.run(SpringBootJDBCApp.class);
    ITest testService = context.getBean(ITestService.class);
    testService.getAllData();
}

Error : 错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springBootJDBCApp': Initialization of bean failed; org.springframework.beans.factory.BeanCreationException:创建名称为“ springBootJDBCApp”的bean时出错:初始化bean失败; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method 'main': Only no-arg methods may be annotated with @Scheduled 嵌套的异常为java.lang.IllegalStateException:遇到无效的@Scheduled方法'main':@Scheduled只能注释无参数的方法

Is there any other way to achieve this task? 还有其他方法可以完成此任务吗? I wanna run the entire things which are mentioned in the main method periodically. 我想定期运行main方法中提到的全部内容。

Any leads? 有线索吗?

The scheduled method annotated with @Scheduled annotation must have no arguments because the annotation doesn't provide any input. @Scheduled注释注释的计划方法必须没有参数,因为注释不提供任何输入。 The Spring-docs of @Scheduled sais: @Scheduled的Spring文档说:

The annotated method must expect no arguments. 带注释的方法不得包含任何参数。 It will typically have a void return type; 它通常具有void返回类型; if not, the returned value will be ignored when called through the scheduler. 如果不是,则通过调度程序调用时,返回值将被忽略。

You annotated the method public static void main(String args[]) which has an array as an argument. 您注释了方法public static void main(String args[]) ,该方法具有一个数组作为参数。 You have to just wrap the content in the main(String args[]) into a different method. 您只需要将main(String args[])的内容包装到其他方法中即可。 Note you don't use args[] at all. 请注意,您根本不使用args[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM