android - Asynchronously processing the attached files in an email (Spring Mail Integration) -
if have spring app mail server inbound channel, best way process every file in every email (i poll approx. every 1 min, , fetch 1 email multiple attachments).
although can apply multithreading @ receiving channel (simpleasynctaskexecutor or threadpooltaskexecutor) doesn't because if have 10 files attached in email, processing pretty bound 1 thread.
i've been keeping pretty synchronous until now, because wanted aggregate data every email, , send response after files have been processed. believe done in better way.
in general how can asynchronously process every file in every email, , again asynchronously build email reply?
looks asking java.util.concurrent.future
. java core concept block until (method) result calculated. (see javadoc example)
the spring @async
support future
concept too.
so think need having method uses @async
takes 1 attachment of mail argument , returns ever calculated in future. need invoke methods attachments (asynchronous) , store returned future in list. after methods invoked. try feature results in new loop. after loop finish attachments proceed asynchronous.
processonemail(list<attachement> attachments) { list<future<attachmentresult>> futures = new arraylist... for(attachment attachment : attachments) { futures.add(processoneattachment(attachment)); //async } list<attachmentresult> attachmentresults = new arraylist... for(future<attachmentresult>> future : futures) { attachmentresults.add(future.get()); //eventually blocks } //now attachments calculated , stored in list. ... } @async future<attachmentresult> processoneattachment(attachment attachment) { ... }
see also: http://blog.espenberntsen.net/2010/03/08/spring-asynchronous-support/
Comments
Post a Comment