2015-01-13 13 views
6

मैं फ़ाइल अपलोड स्वीकार करने के लिए एक छोटी सी सेवा बनाने की कोशिश कर रहा हूं, इसे अनजिप करें और फिर अपलोड की गई फ़ाइल को हटा दें। उन तीन चरणों को वायदा के रूप में जंजीर किया जाना चाहिए। मैं Google Guava लाइब्रेरी का उपयोग कर रहा हूँ।गुवा वायदा श्रृंखला कैसे करें?

कार्यप्रवाह है:

एक भविष्य आपरेशन, पूरा करता है, तो उसके बाद फ़ाइल अनज़िप करने एक भविष्य फ़ाइल डाउनलोड करने के लिए,। यदि अनजिपिंग किया जाता है, तो मूल अपलोड की गई फ़ाइल को हटाने का भविष्य।

लेकिन ईमानदारी से, यह मुझे स्पष्ट नहीं है कि मैं वायदा को कैसे श्रृंखला दूंगा, और यहां तक ​​कि उन्हें कैसे अमरूद में बनाया जाए। दस्तावेज़ीकरण बस terse और अस्पष्ट है। ठीक है, transform विधि है लेकिन बिल्कुल कोई ठोस उदाहरण नहीं है। chain विधि बहिष्कृत है।

मुझे RxJava लाइब्रेरी याद आती है।

+0

वायदा काम नहीं करते हैं। Runnables करते हैं। –

+0

@BrettOkken कैसे Guava के रास्ते में चलाने योग्य श्रृंखला? – Chiron

+0

संभावित डुप्लिकेट [क्या यह गुवा का उपयोग करके एसिंक कॉल को चेन करना संभव है?] (Http://stackoverflow.com/questions/8191891/is-it-possible-to-chain-async-calls-using-guava) – Joe

उत्तर

2

गुवा Future इंटरफ़ेस ListenableFuture के साथ इस उद्देश्य के लिए बढ़ाता है।

कुछ इस तरह काम करना चाहिए:

Runnable downloader, unzipper; 
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); 

service.submit(downloader).addListener(unzipper, service); 

मैं, unzipper में फ़ाइल को हटाने के बाद से यह एक के पास तात्कालिक कार्रवाई है को शामिल किया जाएगा, और यह कोड यह अलग करने के लिए मुश्किल होगा।

2

Futures.transform आरएक्सजेवा की तरह आसानी से चेन करने योग्य नहीं है, लेकिन आप अभी भी Future एस सेट अप करने के लिए इसका उपयोग कर सकते हैं जो एक दूसरे पर निर्भर करता है। यहाँ एक ठोस उदाहरण है:

final ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); 

final ListenableFuture<FileClass> fileFuture = service.submit(() -> fileDownloader.download()) 
final ListenableFuture<UnzippedFileClass> unzippedFileFuture = Futures.transform(fileFuture, 
     //need to cast this lambda 
     (Function<FileClass, UnzippedFileClass>) file -> fileUnzipper.unzip(file)); 
final ListenableFuture<Void> deletedFileFuture = Futures.transform(unzippedFileFuture, 
     (Function<UnzippedFileClass, Void>) unzippedFile -> fileDeleter.delete(unzippedFile)); 
deletedFileFuture.get(); //or however you want to wait for the result 

इस उदाहरण में कल्पना fileDownloader.download()FileClass का एक उदाहरण देता है, fileUpzipper.unzip() रिटर्न एक UnzippedFileClass आदि FileDownloader.download() बजाय एक ListenableFuture<FileClass> देता है, तो AsyncFunction बजाय Function का उपयोग करें।

यह उदाहरण ब्रेवटी के लिए जावा 8 लैम्बडास का भी उपयोग करता है। आप जावा 8 का उपयोग नहीं कर रहे हैं, तो समारोह या AsyncFunction बजाय गुमनाम कार्यान्वयन में पारित:

Futures.transform(fileFuture, new AsyncFunction<FileClass, UpzippedFileClass>() { 
     @Override 
     public ListenableFuture<UnzippedFileClass> apply(final FileClass input) throws Exception { 
      return fileUnzipper.unzip(); 
     } 
    }); 

अधिक जानकारी यहाँ transform पर: http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/Futures.html#transform (स्क्रॉल या "बदलने" के लिए खोज - डिप लिंक वर्तमान में टूटा हुआ प्रतीत होता)

संबंधित मुद्दे