я пытаюсь запустить задачу каждую минуту в данных jpa. Я хочу сделать это с аннотациями:
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();
}
}
}
И мой класс с методом, который я хочу запускать каждую минуту:
@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>
Работает, но проблема в том, что после этого мой HikariPool закрывается, поэтому он делает это только один раз вместо каждого минута:
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
Кто-нибудь может помочь с этим?