温馨提示:本文翻译自stackoverflow.com,查看原文请点击:java - Run a task every minute in spring jpa but HikariPool shuts down

java - 在 Spring JPA中每分钟运行一次任务,但HikariPool关闭

发布于 2020-03-27 16:14:30

我想每分钟在spring data 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

有人可以帮忙吗?

查看更多

查看更多

提问者
user182410
被浏览
156
Patrick 2020-01-31 17:15

您的主要方法无法正常工作。您应该将main方法更改为以下内容:

@SpringBootApplication
@EnableScheduling
public class JpademoApplication {

    public static void main(String[] args) {
        SpringApplication.run(JpademoApplication.class, args);
    }

}

Spring上下文将自动查找所有带@Component注释的类,并运行带注释的方法@Scheduled不需要在主类中进行任何其他配置。

顺便说一句:所有@Scheduled方法都在自己的线程中运行。对于数据库操作,应@Transactional为自己的会话注释方法