2

I am trying o schedule a method call. I want to schedule this method call as soon as server starts and then after every 30 seconds.

Below code:

@Configuration
@EnableScheduling
@EnableTransactionManagement
public class Schedular implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

        taskRegistrar.setScheduler(poolScheduler());

        taskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                testScheduling();
            }
        }, new Trigger() {

            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                Calendar nextExecutionTime = Calendar.getInstance();
                nextExecutionTime.add(Calendar.SECOND, <some value from database>);
                return nextExecutionTime.getTime();
            }
        });
    }

    @Bean
    public TaskScheduler poolScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setThreadNamePrefix("poolScheduler");
        scheduler.setPoolSize(10);
        return scheduler;
    }

    public void testScheduling(){
        System.out.println("Scheduling Testing");
    }
}

The code below schedule the method code after 30 seconds after the server started BUT NOT just after server started. I know I need to do some other config to schedule the method call just after server start and then after every 30 seconds (or whatever time I want to).

I am using spring boot. Could anyone please suggest.

Also, is it possible to get both initial and fixeddelay/fixedrate value from database. I want to set the initial value as well from database

Thanks in advance.

Manglesh
  • 520
  • 1
  • 13
  • 29
  • 2
    Why not just use `@Scheduled(initialDelay=30000, fixedRate=30000)`?. – M. Deinum Jul 04 '17 at 13:43
  • Apologies my mistake, I should have mentioned, the delay is value fetched from database so its not constant value. I have updated the question. – Manglesh Jul 04 '17 at 16:00
  • 1
    Then just make a `PropertySource` instance that reads from a database and use the `initialDelayString` and `fixedRateString` to use an expression. – M. Deinum Jul 05 '17 at 05:59
  • @M.Deinum can you please explain this little bit more. what do you mean by "make a PropertySource instance that reads from a database". the values from property file will be read only once (server startup) but what if values changes in database then I need to get that dynamically. could you please explain – Manglesh Jul 05 '17 at 08:10

2 Answers2

0

let me know if this worked for you

@PostConstruct
@Scheduled(fixedDelay=30000)
public void testScheduling(){
     System.out.println("Scheduling Testing");
}

Use the @PostConstuct annotation to start the method after the application starts.

Periklis Douvitsas
  • 2,431
  • 1
  • 13
  • 14
  • Apologies my mistake, I should have mentioned, the delay is value fetched from database so its not constant value. I have updated the question. Also if I dont put fixed delay and put only initialDelay, then it complain about having either cron or fixeddelay value – Manglesh Jul 04 '17 at 16:01
0

You can use like below. I had used Spring Boot version v2.2.7

@Scheduled(fixedRateString = "${echo.interval(milliseconds)}", initialDelayString = "${echo.initialDelay(milliseconds)}")

The properties should be mentioned in "application.properties" file for the Spring Boot to detect and inject the values of the fixed rate and initial delay into the Scheduler.