我使用spring(boot)集成以连接到一个宁静的WebService并从中检索数据,以下是我的代码的一部分:
@Bean
public PollerMetadata downloadTrigger()
{
PeriodicTrigger trigger = new PeriodicTrigger(config.getDownloadInterval());
trigger.setFixedRate(true);
PollerMetadata pollerMetadata = new PollerMetadata();
pollerMetadata.setTrigger(trigger);
pollerMetadata.setMaxMessagesPerPoll(1);
return pollerMetadata;
}
@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata poller()
{
PeriodicTrigger trigger = new PeriodicTrigger(10);
trigger.setFixedRate(true);
PollerMetadata pollerMetadata = new PollerMetadata();
pollerMetadata.setTrigger(trigger);
return pollerMetadata;
}
@InboundChannelAdapter(value = "channel1", poller = @Poller("downloadTrigger"))
public ResponseEntity<AppsItem[]> download()
{
String url = "https://example.com/?page{pageNumber}";
try
{
SSLUtil.turnOffSslChecking();
}
catch (Exception e)
{
}
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<AppsItem[]> responseEntity = template.exchange(url, HttpMethod.GET, requestEntity, AppsItem[].class, 10, 1);
return responseEntity;
}
@Splitter(inputChannel = "channel1", outputChannel = "channel2")
public List<AppsItem> scrape(ResponseEntity<AppsItem[]> payload)
{
//do something;
}
正如您所看到的,我有一个接受带有pageNum名称的变量的网址,我希望在响应主体为空时增加页码,我不知道如何使用spring integration实现它。但是让我解释它需要什么:
1 - Poller calling inbound method once peer hour
2 - Inbound method start calling WebService from page 1 to page n.
3 - Inbound method should pass data to next channel before fetching next page
我需要以下内容:
@InboundChannelAdapter(value = "channel1", poller = @Poller("downloadTrigger"))
public ResponseEntity<AppsItem[]> download()
{
String url = "https://example.com/?page{pageNumber}";
try
{
SSLUtil.turnOffSslChecking();
}
catch (Exception e)
{
}
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<AppsItem[]> responseEntity;
int i = 0;
do
{
i++;
responseEntity = template.exchange(url, HttpMethod.GET, requestEntity, AppsItem[].class, 10, i);
scraper.parse(responseEntity);
LOG.info("entry={}", "");
//Send response to the next channel in this loop
}
while (responseEntity.hasBody());
return responseEntity;
}
答案 0 :(得分:1)
从setMaxMessagesPerPoll(1)
移除downloadTrigger
,并且轮询器(每次轮询)将继续调用download()
,直到它返回null
。
增加每个呼叫的页数,并在不再存在页面时重置它;轮询器将在间隔之后运行。