gpt4 book ai didi

java - @Schedule 不在固定时间触发Service

转载 作者:行者123 更新时间:2023-12-02 10:34:36 26 4
gpt4 key购买 nike

我正在尝试以固定速率和初始延迟从 spring-boot 安排服务,
部署后,我预计 read 方法应在 5 秒后和每 10 秒执行一次,但控制台中没有显示任何内容。这是我的主要应用程序类

@SpringBootApplication
@EnableScheduling
public class ArgusAPIApplication {

@Value("${proxy.host}")
private String proxyHost;
@Value(("${proxy.port}"))
private int proxyPort;
@Value(("${readTimeout}"))
private int readTimeout;
@Value(("${connectTimeout}"))
private int connectTimeout;


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

@Bean
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy= new Proxy(Type.HTTP, new InetSocketAddress(proxyHost,proxyPort));
requestFactory.setProxy(proxy);
requestFactory.setReadTimeout(readTimeout);
requestFactory.setConnectTimeout(connectTimeout);
return new RestTemplate(requestFactory);
}


}

我希望以固定的时间间隔安排服务下面给出了带有 @Scheduled 注解的方法的 Service 类

@Service
public class targusTractoscalingScheduler {

private static final Logger logger = LogManager.getLogger(targusTractoscalingScheduler.class);

@Autowired
targusController targusController;

@Autowired RestTemplate restTemplate;

private boolean firstTime = true;

@Value("aaaaaaaaaaaaaaaa")
private String apiKey;

@Value("xxxxxxxxxxxxxxxxxxxxxxx")
private String applicationKey;

@Value("${ddUrl}")
private String ddUrl;

@Value("/filter/instance")
private String ddInstanceApiPath;

@Value("/stop/job/{Tractoscalinggroup}")
private String ddStopJobPath;

/**
* Scheduler method that will run at every predefined interval.
*/

@Scheduled(initialDelayString = "5000", fixedRateString = "10000")
public void targusAsgAndCmdbASgCompare() {
List<targusData> targusDatas = targusController.gettargusData();
if (CollectionUtils.isEmpty(targusDatas)) {
logger.debug("Empty list obtained from targus, so simply exiting from method");
return;
}
processtargusData(targusDatas);
firstTime = false;
}

但是服务没有触发,springboot文档也给出了类似的实现。

最佳答案

这是一个例子:

@EnableScheduling
@SpringBootApplication
public class MyExampleCronApplication {

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

@Scheduled(initialDelay=5000,fixedRate=10000)
public void test() {
System.out.println(Date.from(Instant.now()).getSeconds());
}
}

输出:

enter image description here

注意:我通过将预定的方法放入服务中来分离代码,并且它可以找到。

关于java - @Schedule 不在固定时间触发Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53384434/

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