Skip to content

Examples demonstrating usage of Deferred #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions examples/ClientLibrary.java
Original file line number Diff line number Diff line change
@@ -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<String> 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");
}


}
33 changes: 33 additions & 0 deletions examples/HighLevelLibrary.java
Original file line number Diff line number Diff line change
@@ -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<String> get() {
RPC rpc = new RPC();
LOGGER.info("Making RPC call");
Deferred<String> 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;
}
}
38 changes: 38 additions & 0 deletions examples/RPC.java
Original file line number Diff line number Diff line change
@@ -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<String> get() {
ExecutorService executorService = Executors.newCachedThreadPool();
Deferred<String> 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;
}
}