预定任务在tomcat生产中运行两次

时间:2018-08-21 20:50:22

标签: spring spring-boot tomcat

我有一个类为Scheduled的onde app spring-boot v2,在开发人员环境中,类Scheduled中的方法run运行了一次。 但是我为生产而构建(使用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:我为taskService.isRunning定义了变量false 但存在执行默认值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

在执行中存在两个不同的类。 有什么解决办法?

1 个答案:

答案 0 :(得分:0)

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

  

启用Spring的计划任务执行功能,类似于Spring的XML名称空间中的功能。要在@Configuration类上使用,如下所示:

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

我建议从@EnableScheduling中删除SchedulerEmailService批注,并添加一个与ComponentScan类似的配置,其中包括SchedulerEmailService的软件包。

@Service
public class SchedulerEmailService {

    // ...

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

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