java - Spring RestTemplate - passing in batches of GET requests -
i need query server links can obtained giving server reference.
lets have 10 references , want 10 links in 1 go in arraylist.
is below efficient way it? looks pretty resource intensive , takes approximately 4672ms generate
i looked @ docs resttemplate: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/resttemplate.html#getforentity-java.lang.string-java.lang.class-java.util.map- there doesn't seem easier way want do.
arraylist<string> references = new arraylist<>(); arraylist<string> links = new arraylist<>(); resttemplate resttemplate = new resttemplate(); resttemplate.getmessageconverters().add(new stringhttpmessageconverter()); (int = 0; < 10; i++) { responseentity<string> resource = resttemplate.getforentity(references.get(i), string.class); links.add(resource.getbody().tostring()); }
edit:
based on suggestions, have changed code i'm getting error: "asynchronous execution requires asynctaskexecutor set":
arraylist<string> references = new arraylist<>(); arraylist<string> links = new arraylist<>(); asyncresttemplate asyncresttemplate = new asyncresttemplate(new customclienthttprequestfactory()); resttemplate.getmessageconverters().add(new stringhttpmessageconverter()); (int = 0; < 10; i++) { future<responseentity<string>> resource = asyncresttemplate.getforentity(references.get(i), string.class); responseentity<string> entity = resource.get(); //this should start 10 threads links asynchronously links.add(entity.getbody().tostring()); }
i looked @ reference docs none of constructors allow me set both asynclistenabletaskexecutor , clienthttprequestfactory (the clienthttprequestfactory used - customclienthttprequestfactory extends simpleclienthttprequestfactory can redirect links successfully: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/asyncresttemplate.html#asyncresttemplate--
here you're making rest calls sequentially - i.e. nothing done in parallel.
you use the asynchronous variant of resttemplate , make calls in parallel.
Comments
Post a Comment