user2467545
user2467545

Reputation:

Efficient way of having synchronous and asynchronous behavior in an application

I am working on a project in which I am supposed to make synchronous and asynchronous behavior of my client. In general, how our client will work as below -

Customer will call our client with a userId and we will construct a URL from that userId and make a HTTP call to that URL and we will get a JSON String back after hitting the URL. And after we get the response back as a JSON String, then we will send that JSON String back to our customer.

So in this case, as I mentioned above, I need to have synchronous and asynchronous methods, some customer will call executeSynchronous method to get the same feature and some customer will call our executeAsynchronous method to get the data back.

Below is my Interface -

public interface Client {

    // for synchronous
    public String executeSynchronous(final String userId);

    // for asynchronous
    public Future<String> executeAsynchronous(final String userId);
}

And then I have our SmartClient which implements the above Client interface.

public class SmartClient implements Client {

    ExecutorService executor = Executors.newFixedThreadPool(5);

    // This is for synchronous call
    @Override
    public String execute(String userId) {

        String response = null;
        Future<String> handle = getHandle(userId);

        try {
            response = handle.get(500, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {

        }

        return response;
    }

    // This is for asynchronous call
    @Override
    public Future<String> executeAsync(String userId) {

        return getHandle(userId);

    }

    private Future<String> getHandle(String userId) {
        Future<String> future = null;

        Task task = new Task(userId);
        future = executor.submit(task);

        return future;

    }
}

Below is my simple class which will perform the actual task -

class Task implements Callable<String> {

    private final String userId;

    public Task(String userId) {
        this.userId = userId;   
    }

    public String call() throws Exception {

        String url = createURL(userId);

        // make a HTTP call to the URL
        RestTemplate restTemplate = new RestTemplate();
        String jsonResponse = restTemplate.getForObject(url, String.class);

        return jsonResponse;
    }

    // create a URL
    private String createURL(String userId) {
        String generateURL = somecode;

        return generateURL;
    }
}

Is this the correct and efficient way of doing this problem? And how about exception handling? Do I need any other catch blocks at any places? If any, then just a high level code example will help me understand to better.

If there is any better way, then please let me know... I am still learning..

Upvotes: 0

Views: 373

Answers (1)

Serdar
Serdar

Reputation: 383

I would implement executeSynchronous this way:

 public String execute(String userId){
    Task task = new Task(userID);
    return task.call();      //Task executes in the caller thread
 }

I would not use execution framework since call shall execute in the caller thread. Bu think you meant to provide a timeout mechanism in the synchronous method so that caller does not get blocked. If not I think there is no need to use execution framework in the synchronous method.

Upvotes: 1

Related Questions