HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Having sync and async methods by implementing future interface

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
asynchavingmethodsfutureinterfaceandimplementingsync

Problem

I need to make a library in which I will have synchronous and asynchronous methods in it and this library will be used by our customer in our company. Some customer will call the executeSynchronous method to get the same feature and some customer will call our executeAsynchronous method and with the latter method they will do future.get in their code base.

  • executeSynchronous() - waits until I have a result, returns the result.



  • executeAsynchronous() - returns a Future immediately which can be processed after other things are done, if needed.



In case of any exception, I need to log it into our company storage system with the logging I have both from synchronous and asynchronous method. This will ensure that I can log the way I want into our storage system, instead of asking each customer to do that.

This logging will log into our storage system.

PotoLogging.logErrors(ex, DataErrorEnum.TIMEOUT_ON_CLIENT, dataKey);


Core Logic of my Library

The customer will use our library and they will call it by passing DataKey builder object. We will then construct a URL by using that DataKey object and make a HTTP client call to that URL by executing it and after we get the response back as a JSON String, we will send that JSON String back to our customer as it is by creating DataResponse object.

Interface:

public interface Client {

    // for synchronous
    public DataResponse executeSynchronous(DataKey dataKey);

    // for asynchronous
    public Future executeAsynchronous(DataKey dataKey);
}


And then I have my DataClient which implements the above Client interface:

```
public class DataClient implements Client {

private RestTemplate restTemplate = new RestTemplate();
private ExecutorService service = Executors.newFixedThreadPool(10);

// for synchronous call
@Override
public DataResponse executeSynchronous(DataKey dataKey) {
DataResponse dataResponse = null;

try {
Future future

Solution

The comments on the interface should be JavaDoc so your IDE picks them up:

public interface Client {

    // for synchronous
    public DataResponse executeSynchronous(DataKey dataKey);

    // for asynchronous
    public Future executeAsynchronous(DataKey dataKey);
}


That said, such comments don't add much value, and the naming of the methods is actually hindering readability IMO. Blame it on my C# background, but I'd rather see this:

public interface Client {    
    public DataResponse execute(DataKey dataKey);
    public Future executeAsync(DataKey dataKey);
}


That way it's clear that the xxxAsync method is for async execution, and if you make that a convention throughout your code base, then you don't need a Synchronous suffix anywhere.

I'll leave the rest to Java reviewers :)

Code Snippets

public interface Client {

    // for synchronous
    public DataResponse executeSynchronous(DataKey dataKey);

    // for asynchronous
    public Future<DataResponse> executeAsynchronous(DataKey dataKey);
}
public interface Client {    
    public DataResponse execute(DataKey dataKey);
    public Future<DataResponse> executeAsync(DataKey dataKey);
}

Context

StackExchange Code Review Q#84038, answer score: 5

Revisions (0)

No revisions yet.