@EnableScheduling无法识别并创建执行器服务并运行计划的作业

时间:2019-06-21 21:57:36

标签: spring spring-boot

尝试将应用程序修改为具有@Component,该@Component的计划任务每​​n秒触发一次。似乎执行程序服务永远不会开始被认可,@ Components带有@Scheduled注释。有任何想法吗?

确保包装正确无误,并且应位于componentscan基本包装中。


package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * This is the Spring Boot class for the Data Consistency Model (DCM)
 */
@EnableScheduling
@SpringBootApplication
@EnableConfigurationProperties({KafkaConfig.class, SparkConfig.class, JedisConfig.class, PrometheusConfig.class})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


Component Class:
package com.test;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.PushGateway;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@EnableScheduling
@EnableAsync
public class PrometheusGatewayMetricsPusher {
    private static final Logger LOGGER = LogManager.getLogger(PrometheusGatewayMetricsPusher.class);
    @Value("${spring.application.name}")
    private String appName;

    @Autowired
    PushGateway pushGateway;

    @Scheduled(fixedDelay = 1000, initialDelay = 1000)
    public void push() {
        try {
            pushGateway.push(CollectorRegistry.defaultRegistry, appName);
        } catch (IOException e) {
            LOGGER.log(Level.ERROR, "Error pushing to gateway. " + e.getMessage());
        }
    }
}

Config Class:
package com.test;
import io.prometheus.client.exporter.PushGateway;
import io.prometheus.client.hotspot.DefaultExports;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Getter
@Setter
@Configuration
public class MetricsConfig {
    @Value("prometheus.config.gateway")
    private String pushGatewayURL;


    @PostConstruct
    public void defaultExports() {
        DefaultExports.initialize();
    }

    @Bean
    public PushGateway pushGateway() {
        return new PushGateway("localhost:9091");
    }
}

我希望执行程序服务在线程池为1的应用程序上下文中启动和初始化。然后,在初始延迟1秒之后,它将每秒执行@Scheduled方法。如果我在@Component类中断点,则根本不会初始化。

0 个答案:

没有答案