Reputation: 1
I was tried to implement scheduler with setting for every day. Then I build to a jar file and running with "java -jar". I trying on my computer to still alive until 2 days for test it. Yesterday it's works. But when I look today is not running. Refer from this https://riptutorial.com/spring/example/21209/cron-expression.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class Scheduler {
private static final Logger log = LoggerFactory.getLogger(Scheduler.class);
@Scheduled(cron = "0 0 18 * * ?")
public void currentTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
log.info("Current Time = {}", dateFormat.format(new Date()));
log.info("Excel File has been generated");
}
}
Upvotes: 0
Views: 11423
Reputation: 66
Najib, it seems like this should be straightforward, so I was testing it locally and I was having a difficult time getting an instance time at a specific hour to run, but I figured out why I had a problem. Can I ask, are you calculating your cron time in UTC? I wasn't and it was throwing off my result.
Working example
package com.brand.server.v1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class Scheduler {
private static final Logger LOG = LoggerFactory.getLogger(Scheduler.class);
static {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
LOG.info("The current time is: {}", sdf.format(new Date()));
}
@Scheduled(cron = "0 30 22 * * ?")
public void currentTimeDifferentName() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
LOG.info("Current time = {}", sdf.format(new Date()));
LOG.info("File has been generated");
}
@Scheduled(cron = "0 */15 * * * ?")
public void heartbeat() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
LOG.info("Heartbeat = {}", sdf.format(new Date()));
}
}
Log snippet from 20:00 Mountain Standard Time
server_1 | 2020-05-14 22:24:48.496 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
server_1 | 2020-05-14 22:24:48.499 INFO 1 --- [ main] com.brand.server.v1.ServerApplication : Started ServerApplication in 5.181 seconds (JVM running for 5.827)
server_1 | 2020-05-14 22:30:00.001 INFO 1 --- [ scheduling-1] com.brand.server.v1.Scheduler : Heartbeat = 22:30:00
server_1 | 2020-05-14 22:30:00.004 INFO 1 --- [ scheduling-1] com.brand.server.v1.Scheduler : Current time = 22:30:00
server_1 | 2020-05-14 22:30:00.004 INFO 1 --- [ scheduling-1] com.brand.server.v1.Scheduler : File has been generated
server_1 | 2020-05-14 22:45:00.000 INFO 1 --- [ scheduling-1] com.brand.server.v1.Scheduler : Heartbeat = 22:45:00
server_1 | 2020-05-14 23:00:00.001 INFO 1 --- [ scheduling-1] com.brand.server.v1.Scheduler : Heartbeat = 23:00:00
server_1 | 2020-05-14 23:15:00.000 INFO 1 --- [ scheduling-1] com.brand.server.v1.Scheduler : Heartbeat = 23:15:00
...
server_1 | 2020-05-15 22:15:00.000 INFO 1 --- [ scheduling-1] com.brand.server.v1.Scheduler : Heartbeat = 22:15:00
server_1 | 2020-05-15 22:30:00.000 INFO 1 --- [ scheduling-1] com.brand.server.v1.Scheduler : Heartbeat = 22:30:00
server_1 | 2020-05-15 22:30:00.000 INFO 1 --- [ scheduling-1] com.brand.server.v1.Scheduler : Current time = 22:30:00
server_1 | 2020-05-15 22:30:00.000 INFO 1 --- [ scheduling-1] com.brand.server.v1.Scheduler : File has been generated
Upvotes: 0
Reputation: 453
Support for specifying both a day-of-week AND a day-of-month parameter is not implemented.
0 30 16 * * ?
@Jason's
answer you also have to use @EnableScheduling
to your configuration class
.Reference: Cron Expression
Upvotes: 3