spring - Get to know that the async receiving data is finished in Java -


i have 2 microservices in spring. first one, lets call him datadeliverer collecting bunch of data internet , sending data json second microservices called, example, receiver. sending divided on parts. number of parts random. receiver , datadeliver never knows how parts there be. receiver receiving parts , saving them asynchronously using spring's annotation @async each part separately. creates problem me, becouse have know sending completed , receiver finished receiving.

for moment figured out sth below: add java.util.map object key , flag isfinished value. know if saving single object(part) finished. when datadeliverer end sendig data, send information it. when receiver information wait when flags in map show objects saved. collecting data done. think solution? appreciate tip. thanks

below simple outline of current situation. dont mind java correctness. it's pseudo java easier display problem.

public class datadeliver {    public void senddata(list<data> listwithrandomsize) {      list<data> listtosend = new arraylist<>();     int = 0;       listwithrandomsize.foreach(data -> {         listtosend.add(data);         i++;         if (i%10 == 0) {             resttemplate.postforobject("http://receiver:8080/save/", listtosend);             listtosend = new arraylist<>();             = 0;         }     }              } }  public class somereceiverservice {      @async     public void save(list<data> partofdata) {         database.save(partofdata);       } } 

in asynchronous method can return object of type org.springframework.util.concurrent.listenablefuture<t> or java8 java.util.concurrent.completablefuture<t>. return value returned async method , can used registers callbacks/handlers failure and/or success.

basically components should this

@component public class someservice {    @async   public completablefuture<string> someservicemethod() {       // async       return completablefuture.completedfuture(someresult);   } } 

and client this:

public class someclient {   @autowired   private someservice msomeservice;    public void someclientmethod() {       //function process result of async call       final function<string, string> resulthandler = null;       msomeservice.someservicemethod().thenapply(resulthandler);   } } 

see async example on spring.io complete reference.


given example code each of calls somereceiverservice.save return new completablefuture (actually of asyncresttemplate seems support listenablefuture - might need convert them completablefuture benefit join functionality) can collect these futures , wait of them finish:

list<completablefuture<void>> allfutures = new arraylist<>(); listwithrandomsize.foreach(data -> {     listtosend.add(data);     i++;     if (i%10 == 0) {         listenablefuture<void> futureresult = resttemplate.postforobject("http://receiver:8080/save/", listtosend);         allfutures.add(converttocompletablefuture(futureresult));         listtosend = new arraylist<>();         = 0;     } }  // sending finished when reaching point completablefuture joinedfuture = completablefuture.allof(allfutures.toarray(new completablefuture[allfutures.size()])); joinedfuture.join();  //at point save have finished chunks (completablefuture future : allfutures) {     furture.get(); } 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -