java - Spring boot integration url pagination solution -
i using spring (boot) integration in order connect restful webservice , retrieve data that, here part of codes:
@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; }
as can see, have url accept variable pagenum name, want increase page number while response body empty, don't know how can implement spring integration.but let me explain need:
1 - poller calling inbound method once peer hour 2 - inbound method start calling webservice page 1 page n. 3 - inbound method should pass data next channel before fetching next page
i need below:
@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 = 0; { i++; responseentity = template.exchange(url, httpmethod.get, requestentity, appsitem[].class, 10, i); scraper.parse(responseentity); log.info("entry={}", ""); //send response next channel in loop } while (responseentity.hasbody()); return responseentity; }
remove setmaxmessagesperpoll(1)
downloadtrigger
, poller (on each poll) keep calling download()
until returns null
.
increment page count on each call , reset when no more pages exist; poller next run after interval.
Comments
Post a Comment