How to run a Timer class (always) on deployment?

Asked

Viewed 452 times

1

I have a non-web project and I will turn it into a web project using Spring MVC.

Today in my method static main of my main class, I create it here:

    Exec.principalTimer = new Timer();
    Exec.principalTimer.scheduleAtFixedRate(new PrincipalTimerTask(), Exec.delay, Exec.interval);

The class PrincipalTimerTask is the type TimerTask.

How I create this Timer in Spring already in the implementation of the project, without having to call any url and let it run there, as if it were a service if I don’t have one public static main in the Web container?


I can implement the Webapplicationinitializer interface and create the Timer already within the onStartup method.

Here’s where I got the idea http://joshlong.com/jl/blogPost/simplified_web_configuration_with_spring.html

  • Web applications have a different status handling of console applications, with windows or services. I think you will need, in addition to the web application, a service that keeps calling something by URL or other means at fixed intervals. The technical name for this is Cron Job.

  • You may not use Quartz (http://www.quartz-scheduler.org/)?

  • I’ll note here, the boring is like "one more framework"... ahhah

2 answers

1


Use Eventlistener may solve your problem, but it is not ideal as it was done to perform certain action after an event in the system.

When it comes to Spring you can use the annotation @Scheduled. To enable the use of Schedule you need to use the annotation @EnableScheduling in its Spring configuration class.

@Configuration
@EnableScheduling
public class SpringConfig {
}

Then you should write down a class with @Component and in the method that will perform the task you use the annotation @Scheduled.

Using fixedDelay the execution between the last run and the next one is fixed and one execution only begins when the other ends.

@Scheduled(fixedDelay = 1000)
public void scheduleFixedDelayTask() {
    System.out.println("Fixed delay task - " + System.currentTimeMillis()/1000);
}

Using fixedRate, the next run starts without taking into account whether the last one has finished

@Scheduled(fixedRate = 1000)
public void scheduleFixedRateTask() {
    System.out.println("Fixed rate task - " + System.currentTimeMillis()/1000);
}

initialDelay, the task will be executed after the value of the intialDelay and then follow the time of the fixedDelay.

@Scheduled(fixedDelay = 1000, initialDelay = 1000)
public void scheduleFixedRateWithInitialDelayTask() {
    long now = System.currentTimeMillis() / 1000;
    System.out.println("Fixed rate task with one second initial delay - " + now);
}

Remembering that all values are in milliseconds and you can also use cron expressions

To know more look at documentation. Source: http://www.baeldung.com/spring-scheduled-tasks

  • Thanks, I’ll take a closer look!

  • Adelmo is possible to extend Scheduled to read from a property file instead of having a fixed time?

  • I don’t know, but I’ll search on. Why do you want to read a property file instead of setting in the annotation itself?

  • Then it is not to have q compile every time you change the program interval... Even cool would make it to update the interval without even having to stop the application....

  • See if it helps you: http://stackoverflow.com/questions/15250928/how-to-change-springs-scheduled-fixeddelay-at-runtime

0

The output was to create some other java class annotated with @Component and in it create a method with that signature..

 @EventListener
 public void onCreate(ContextRefreshedEvent e) {}

This method will run as soon as spring reads all classes... At least that’s what I understood

Browser other questions tagged

You are not signed in. Login or sign up in order to post.