diff --git a/examples/ClientLibrary.java b/examples/ClientLibrary.java new file mode 100644 index 0000000..603cd5b --- /dev/null +++ b/examples/ClientLibrary.java @@ -0,0 +1,28 @@ +package com.stumbleupon.async; + +import com.stumbleupon.async.Deferred; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Client library which calls get() on High Level Library and expects to be called back with the response for further + * processing. + * + */ +public class ClientLibrary { + + private static final Logger LOGGER = LoggerFactory.getLogger(HighLevelLibrary.class); + + public static void main(String[] args) { + HighLevelLibrary highLevelLibrary = new HighLevelLibrary(); + LOGGER.info("Calling high level library"); + Deferred deferredOutput = highLevelLibrary.get(); + deferredOutput.addCallback(arg -> { + LOGGER.info("Got callback after high level task completion: " + arg); + return arg; + }); + LOGGER.info("Doing other things in client library"); + } + + +} diff --git a/examples/HighLevelLibrary.java b/examples/HighLevelLibrary.java new file mode 100644 index 0000000..47f372a --- /dev/null +++ b/examples/HighLevelLibrary.java @@ -0,0 +1,33 @@ +package com.stumbleupon.async; + + +import com.stumbleupon.async.Deferred; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * High Level library which makes an RPC call and converts the String response from RPC to lower case + * and returns the result to client library in an async manner. + */ +public class HighLevelLibrary { + + private static final Logger LOGGER = LoggerFactory.getLogger(HighLevelLibrary.class); + + /** + * Makes an RPC call. Once RPC completes, expects to be called back with the RPC response. + * The callback converts the response to lower case and returns it to the client library. + * + * @return + */ + public Deferred get() { + RPC rpc = new RPC(); + LOGGER.info("Making RPC call"); + Deferred deferredRPCOutput = rpc.get(); + deferredRPCOutput.addCallback(arg -> { + LOGGER.info("RPC Output: " + arg); + return arg.toLowerCase(); + }); + LOGGER.info("Doing other things in High Level Library"); + return deferredRPCOutput; + } +} diff --git a/examples/RPC.java b/examples/RPC.java new file mode 100644 index 0000000..db8c917 --- /dev/null +++ b/examples/RPC.java @@ -0,0 +1,38 @@ +package com.stumbleupon.async; + +import com.stumbleupon.async.Deferred; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * A class which simulates an RPC call + * + */ +public class RPC { + + private static final Logger LOGGER = LoggerFactory.getLogger(RPC.class); + + /** + * Simulates an RPC call which takes 5 seconds to finish before returning a String response + * + * @return + */ + public Deferred get() { + ExecutorService executorService = Executors.newCachedThreadPool(); + Deferred deferred = new Deferred<>(); + Runnable longRunningTask = () -> { + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + LOGGER.info("RPC call is done"); + deferred.callback("RPC OUTPUT"); + }; + executorService.submit(longRunningTask); + return deferred; + } +}