1

I'm trying to make a call by using slack's sdk client in java in order to get user's id by using the email name. The slack client returns CompletableFuture object. I can get the user_name if i use get() method but as far as i understand, it's a synchronous function and it will make the application slower. Is there another way to make this call asynchronous?

public static String lookUpUserId(String email) throws ExecutionException, InterruptedException {

       CompletableFuture<UsersLookupByEmailResponse> response = slackClient.usersLookupByEmail(r -> r
        .email(email));

       UsersLookupByEmailResponse data = response.get();

       return data.getUser().getId();
}

I tried using supplyFunc and thenApply like this but it gives an error saying 'missing return statement' although i return a value. I'm just concerned about the performance and curious if there's a a better way to handle this. Thanks

response.thenApply(r->r.getUser().getId());
curiosityrock
  • 213
  • 4
  • 20

2 Answers2

2

Since the call returns CompletableFuture, it is alredy asynchronous. Yes, get() method is a synchronous function. Using it will NOT make the application slower, it will make application consume more memory. Asynchronous access like

   response.thenApply(r->r.getUser().getId());

looks correct.

The reason of error message is that the method lookUpUserId is declared as returning String and so must have a return staement. If you want to make it asynchronous, then declare it as returning CompletableFuture<String> and add return statement

 return response.thenApply(r->r.getUser().getId());
Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • hmm doesn't the sync mean it will lock the main thread from running? Thus, it'll make it slower because things won't be able to run in parallel. That was my understanding? – curiosityrock Nov 09 '20 at 23:33
  • Things always can be run in parallel - just create more threads. Async programming may use less number of threads than ordinary multithreading, but the level of parallelization depends on how algorithm is desgned, and the same algorithm can be represented both with synchronous and asynchronous means. – Alexei Kaigorodov Nov 10 '20 at 10:11
  • if i actually use thenApply, the CompletableFuture is not completed on time so i couldn't grab the value from the returned value. @Alexei – curiosityrock Nov 10 '20 at 14:37
  • I think i still need to use get() after thenApply? that way works... but not sure if this is async – curiosityrock Nov 10 '20 at 15:40
0

I don't know much specifically about the slack api but some information can be found in this answer with regards to housing your function in a class that implements Runnable.

Make your `CompletableFuture` in the constructor and run your gets in `run()`. 

In my opinion it is a best practice to process all of your api requests off of the main thread but you should know that running some single thing in a separate thread on one line and joining that thread back on the next is only going to add a little overhead without any performance advantages. If you are processing a batch of requests you should start each on independent threads with a for loop and join them all together after.


I'm also noticing that my referenced answer doesn't really cover thread joins for retrieving your results sooo you will probably also find this informative. And if you havn't learned about object oriented programming yet that's ok! you'll be writing your own classes in no time.

Edit: Ask for code if you need it, It's better if you write it yourself.
kpie
  • 9,588
  • 5
  • 28
  • 50