i'm trying to run a task every minute in spring data jpa. I want to do it with annotations:
JpademoApplication.java
@SpringBootApplication
@EnableScheduling
public class JpademoApplication {
public static void main(String[] args) {
try (var context = SpringApplication.run(JpademoApplication.class, args)) {
var app = context.getBean(RunEveryMinute.class);
app.run();
}
}
}
And my class with the method which i want to run every minute:
@Component
public class RunEveryMinute {
private static final Logger log = LoggerFactory.getLogger(RunEveryMinute.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 60000)
public void run() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
}
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
It is working but the problem is, after that my HikariPool shutting down so it do it only one time instead every minute:
2020-01-31 09:34:47.479 INFO 27966 --- [ main] com.example.jpademo.JpademoApplication : Starting JpademoApplication on edv-vm with PID 27966 (/home/.../Downloads/jpademo/target/classes started by ...in /home/.../Downloads/jpademo)
2020-01-31 09:34:47.483 INFO 27966 --- [ main] com.example.jpademo.JpademoApplication : No active profile set, falling back to default profiles: default
2020-01-31 09:34:48.127 INFO 27966 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-01-31 09:34:48.153 INFO 27966 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 16ms. Found 0 JPA repository interfaces.
2020-01-31 09:34:48.679 INFO 27966 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-01-31 09:34:49.144 INFO 27966 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-01-31 09:34:49.280 INFO 27966 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-01-31 09:34:49.374 INFO 27966 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.4.10.Final}
2020-01-31 09:34:49.604 INFO 27966 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-01-31 09:34:49.742 INFO 27966 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2020-01-31 09:34:49.975 INFO 27966 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-01-31 09:34:49.984 INFO 27966 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-01-31 09:34:50.083 INFO 27966 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2020-01-31 09:34:50.107 INFO 27966 --- [ scheduling-1] com.example.jpademo.ScheduledTasks : The time is now 09:34:50
2020-01-31 09:34:50.112 INFO 27966 --- [ main] com.example.jpademo.JpademoApplication : Started JpademoApplication in 3.487 seconds (JVM running for 5.109)
2020-01-31 09:34:50.113 INFO 27966 --- [ main] com.example.jpademo.ScheduledTasks : The time is now 09:34:50
2020-01-31 09:34:50.115 INFO 27966 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Shutting down ExecutorService 'taskScheduler'
2020-01-31 09:34:50.116 INFO 27966 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-01-31 09:34:50.116 INFO 27966 --- [ main] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
2020-01-31 09:34:50.120 INFO 27966 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-01-31 09:34:50.136 INFO 27966 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
Process finished with exit code 0
Can anybody helped out with that ?
Your main method is not working as expected. You should change the main method to the following:
@SpringBootApplication
@EnableScheduling
public class JpademoApplication {
public static void main(String[] args) {
SpringApplication.run(JpademoApplication.class, args);
}
}
The spring context will automatically look up all @Component
annotated classes and runs the methods which are annotated with @Scheduled
. No need for any ohter configuration in your main class.
BTW: All @Scheduled
methods run in an own thread. For Database actions you should annotate the methods with @Transactional
for an own session.