简体   繁体   English

Spring MVC运行Cron作业

[英]spring MVC running cron job

How can i run cron job in spring webmvc framework with hibernate..? 如何使用Hibernate在Spring WebMVC框架中运行Cron作业?

Currently I have a application with spring-mvc and hibernate.i need to update the db every day night by calculating data from other tables in database.I have tried by write a java class with main method and run it with linux crontab.but in main method the sessionfactory gives a null instance.i need to done this with in the spring application because all my pojo's and methods are defined in that.i need to use it to calculation. 当前我有一个带有spring-mvc和hibernate的应用程序。我需要每天晚上通过计算数据库中其他表中的数据来更新数据库。我尝试通过使用main方法编写一个Java类并使用linux crontab来运行它。 sessionfactory的main方法提供了一个null实例。我需要在spring应用程序中执行此操作,因为我的所有pojo和方法都在其中定义了.i需要使用它进行计算。

You can use @Scheduled annotation, here is an example : 您可以使用@Scheduled注释,这是一个示例:

To enable the @Scheduled annotation in java style 以Java样式启用@Scheduled批注

@Configuration
@EnableScheduling
public class SpringConfig {
   ...
}

Then annotate the desired method with @Scheduled like this using Cron expresion 然后使用Cron expresion使用@Scheduled注释所需的方法

@Scheduled(cron = "0 15 10 15 * ?")
public void scheduleTaskUsingCronExpression() {

    long now = System.currentTimeMillis() / 1000;
    System.out.println(
    "schedule tasks using cron jobs - " + now);
}

http://www.baeldung.com/spring-scheduled-tasks http://www.baeldung.com/spring-scheduled-tasks

Enable scheduling on your application : 在您的应用程序上启用调度:

@EnableScheduling
public class App {
}

And use @Scheduled annotation on desired method : 并在所需方法上使用@Scheduled批注:

@Component
public class MyScheduler {

    @Scheduled(cron = "0 * * * * *")
    public void myJob() {
    }
}

You can implement ContextListener in your application. 您可以在应用程序中实现ContextListener。 The listener will automatically call at the time of deployment. 侦听器将在部署时自动调用。

You can below in your web.xml: 您可以在下面的web.xml中:

<listener>
    <listener-class>com.listener.CustomListener</listener-class>
</listener>

Create a class named CustomListener. 创建一个名为CustomListener的类。

public class CustomListener extends ContextLoaderListener { 公共类CustomListener扩展了ContextLoaderListener {

@Override
public void contextInitialized(ServletContextEvent event) {

    if (this.contextLoader == null) {
        this.contextLoader = this;
    }

    this.contextLoader.initWebApplicationContext(event.getServletContext());



}
   @Override
   public void contextDestroyed(ServletContextEvent event){
 }
}

ContextInitialized() will get call at time of deployment and contextDestroyed() will get call at undeployment. ContextInitialized()将在部署时被调用,contextDestroyed()将在取消部署时被调用。

You can add your scheduling code inside ContextInitialized() method. 您可以在ContextInitialized()方法内添加计划代码。

You can use Spring + Quartz for scheduling or Java's Timer class 您可以使用Spring + Quartz进行调度或使用Java的Timer类

Timer class implementation will be easy to implement and debug. 计时器类的实现将易于实现和调试。

  • For spring-boot example: 对于春季启动示例:
    • Annotate your SpringBoot initialzer class with @EnableScheduling to enable scheduling: @SpringBootApplication @EnableScheduling public class HealthVendingMachineApplication { ... } 使用@EnableScheduling注释SpringBoot初始类,以启用调度: @SpringBootApplication @EnableScheduling public class HealthVendingMachineApplication { ... }
    • Create a method annotated with @Scheduled to perform a scheduling task: @Scheduled(fixedRate = 2000)//Or Use @Scheduled(cron='* * * *') public void scheduleTaskWithFixedRate() { ... System.out.println("King in the north... "); } 创建一个用@Scheduled注释的方法来执行调度任务: @Scheduled(fixedRate = 2000)//Or Use @Scheduled(cron='* * * *') public void scheduleTaskWithFixedRate() { ... System.out.println("King in the north... "); } @Scheduled(fixedRate = 2000)//Or Use @Scheduled(cron='* * * *') public void scheduleTaskWithFixedRate() { ... System.out.println("King in the north... "); }

ref-link 引用链接

  • For Spring-Mvc : 对于Spring-Mvc:

    • Create a configuration class to enable scheduling: @Configuration @EnableScheduling public class SpringConfig { ... } 创建一个配置类以启用调度: @Configuration @EnableScheduling public class SpringConfig { ... }
    • Create a component class with methods performing scheduling operations: @Component public class MyScheduler { ... @Scheduled(cron = "0 * * * * *") public void myJob() { System.out.println("King in the north... "); } } 使用执行调度操作的方法创建一个组件类: @Component public class MyScheduler { ... @Scheduled(cron = "0 * * * * *") public void myJob() { System.out.println("King in the north... "); } } @Component public class MyScheduler { ... @Scheduled(cron = "0 * * * * *") public void myJob() { System.out.println("King in the north... "); } }

    • You can use different fields provided by @Scheduled annotation. 您可以使用@Scheduled批注提供的不同字段。 For eg, to schedule job at exactly x=2 seconds use @Scheduled(fixedRate = 2000) . 例如,要将作业计划为恰好x = 2秒,请使用@Scheduled(fixedRate = 2000)

Edit : I am a beginner in Spring Web Apps development and even though I have tested these examples, kindly correct me if you find anything wrong with this comment. 编辑:我是Spring Web Apps开发的初学者,尽管我已经测试了这些示例,但是如果您发现此注释有任何错误,请对我进行纠正。

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

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