Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringRESTTemplate:如何反复检查RESTfulAPI服务?_Java_Spring_Rest_Spring Boot_Resttemplate - Fatal编程技术网

Java SpringRESTTemplate:如何反复检查RESTfulAPI服务?

Java SpringRESTTemplate:如何反复检查RESTfulAPI服务?,java,spring,rest,spring-boot,resttemplate,Java,Spring,Rest,Spring Boot,Resttemplate,我正在尝试创建一个SpringBoot应用程序,该应用程序将使用来自第三方REST API的数据,并根据数据的事件/更改将Websocket通知推送到我自己的客户端。我使用的数据经常变化,有时一秒钟几十次(加密货币价格波动的行为与此数据类似)。我希望以固定的间隔(例如,每1-10秒)重复调用API,观察某些事件/更改,并在这些事件发生时触发Websocket推送 我已经能够构建一个简单的Spring Boot应用程序,它可以通过以下指南推送Websocket通知并使用API: 问题:我只

我正在尝试创建一个SpringBoot应用程序,该应用程序将使用来自第三方REST API的数据,并根据数据的事件/更改将Websocket通知推送到我自己的客户端。我使用的数据经常变化,有时一秒钟几十次(加密货币价格波动的行为与此数据类似)。我希望以固定的间隔(例如,每1-10秒)重复调用API,观察某些事件/更改,并在这些事件发生时触发Websocket推送

我已经能够构建一个简单的Spring Boot应用程序,它可以通过以下指南推送Websocket通知并使用API:

问题:我只能让应用程序从API请求数据一次。我花了数小时搜索我能想到的“SpringRestTemplateMultiple/repeated/persistent calls”的每一个变体,但我找不到一个解决方案来满足我的特定用途。我发现的最接近的例子使用重试,但即使是那些最终也会放弃。我希望我的应用程序不断请求这些数据,直到我关闭应用程序。我知道我可以用
while(true)
语句或类似的语句来包装它,但对于SpringBoot这样的框架来说,这似乎并不合适,在尝试实例化RestTemplate时仍然存在问题

如何实现RESTful API资源的持久查询

下面是我在申请课上学到的东西

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableScheduling
public class Application {

    private static final String API_URL = "http://hostname.com/api/v1/endpoint";

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

    @Bean
    RestTemplate restTemplate(RestTemplateBuilder builder){
        return builder.build();
    }

    @Bean
    public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
        return args -> {
            Response response= restTemplate.getForObject(API_URL, Response.class);
            System.out.println(response.toString());
        };
    }
}

CommandLineRunner
仅在每次应用程序启动时运行一次。相反,您希望使用
@Scheduled
注释以固定的间隔执行重复的操作,如

    @Scheduled(fixedDelay = 1000L)
    public void checkApi() {
        Response response = restTemplate.getForObject(API_URL, Response.class);
        System.out.println(response.toString())
    }

它不需要是
Bean
,它可以只是一个简单的方法。有关SpringConfig类或主类的更多信息,请参阅Spring指南

添加
@EnableScheduling
注释

您可以使用计划的fixedDelay或fixedRate

@Scheduled(fixedDelay = 10000)
    public void test() {
        System.out.println("Scheduler called.");
    }

fixedDelay和fixedRate之间的差异:

fixedDelay-确保任务执行的完成时间与下一次任务执行的开始时间之间存在n毫秒的延迟

fixedRate-每n毫秒运行一次计划任务

理想情况下,还应在application.properties文件中外部化fixedDelay或fixedRate值:

@Scheduled(fixedDelayString  = "${scheduler.fixed.delay}")
    public void test() {
        System.out.println("Scheduler called.");
    }
在application.properties文件中,添加以下配置:

scheduler.fixed.delay = 10000

希望这能有所帮助。

您需要了解
@Scheduled
注释的正确用法。此外,在应用程序启动期间,
CommandLineRunner
仅运行一次。您需要阅读更多关于spring及其用途的内容。
scheduler.fixed.delay = 10000