gpt4 book ai didi

spring - 计划任务在 tomcat 生产中运行两次

转载 作者:行者123 更新时间:2023-11-28 23:14:36 25 4
gpt4 key购买 nike

我有 onde 应用程序 spring-boot v2 和一个 Scheduled 类,在开发人员环境中我的方法 run 在类 Scheduled 中运行一次。但我为生产构建(使用 mvn clean package)并在 tomcat 8 中发布我的任务执行了两次

这是我的课@Scheduled

@Service
@EnableScheduling
public class SchedulerEmailService {

@Autowired
private SenderEmailService senderEmailService;

private static final Logger LOG = LoggerFactory.getLogger(SchedulerEmailService.class);

@Autowired
private TaskService taskService;

@Scheduled(fixedDelay = 10000)
public void run() {

LOG.info("Status do Servico: " + taskService.isEnabled());
if(taskService.isEnabled()) {
LOG.info("Executando... {}", LocalDateTime.now());
senderEmailService.enviarEmail();
}else {
LOG.info("Falsa execução do servico... {}", LocalDateTime.now());
}

}


}

在生产中这是日志

O servico foi parado
Status do Servico: false
Falsa execução do servico... 2018-08-21T15:26:59.663
Status do Servico: true
Executando... 2018-08-21T15:27:01.183
Status do Servico: false
Falsa execução do servico... 2018-08-21T15:27:09.664
Status do Servico: true
Executando... 2018-08-21T15:27:11.368

在日志中看到她跑了一次为 false,另一次为 true。Obs:我为 false 定义了变量 taskService.isRunning但在执行值默认 true

时存在其他任务

编辑我在日志中打印哈希码类,结果如下:

2018-08-22 07:49:45.996  INFO 8168 --- [pool-19-thread-1] c.c.s.services.SchedulerEmailService     : Status do Servico: true
2018-08-22 07:49:45.996 INFO 8168 --- [pool-19-thread-1] c.c.s.services.SchedulerEmailService : HashCode Classe: 19875385
2018-08-22 07:49:45.996 INFO 8168 --- [pool-19-thread-1] c.c.s.services.SchedulerEmailService : Executando... 2018-08-22T07:49:45.996
2018-08-22 07:49:50.730 INFO 8168 --- [pool-20-thread-1] c.c.s.services.SchedulerEmailService : Status do Servico: true
2018-08-22 07:49:50.730 INFO 8168 --- [pool-20-thread-1] c.c.s.services.SchedulerEmailService : HashCode Classe: 11898713
2018-08-22 07:49:50.731 INFO 8168 --- [pool-20-thread-1] c.c.s.services.SchedulerEmailService : Executando... 2018-08-22T07:49:50.731
2018-08-22 07:49:56.121 INFO 8168 --- [pool-19-thread-1] c.c.s.services.SchedulerEmailService : Status do Servico: true
2018-08-22 07:49:56.121 INFO 8168 --- [pool-19-thread-1] c.c.s.services.SchedulerEmailService : HashCode Classe: 19875385
2018-08-22 07:49:56.121 INFO 8168 --- [pool-19-thread-1] c.c.s.services.SchedulerEmailService : Executando... 2018-08-22T07:49:56.121

在执行中存在两个不同的类。什么解决这个问题?

最佳答案

从日志中的 hashCode 可以看出 SchedulerEmailService 被 Spring 实例化了两次。我不是 100% 确定原因,但是 @EnableScheduling 注释不是要用在 bean 类上,而是要用在 @Configuration 类上

Enables Spring's scheduled task execution capability, similar to functionality found in Spring's XML namespace. To be used on @Configuration classes as follows:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableScheduling.html

我建议从 SchedulerEmailService 中删除 @EnableScheduling 注释,并添加一个类似 ComponentScan 的配置,包括 SchedulerEmailService 的包

@Service
public class SchedulerEmailService {

// ...

@Scheduled(fixedDelay = 10000)
public void run() {
// ...
}
}

@Configuration
@EnableScheduling
@ComponentScan(basePackages="your.package")
public class AppConfig {
}

关于spring - 计划任务在 tomcat 生产中运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51956525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com