Java slack sdk client how to make the call async - java

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());

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());

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.

Related

How is Apache NIO HttpAsyncClient performing non-blocking HTTP Client

How is Apache NIO HttpAsyncClient able to wait for a remote response without blocking any thread? Does it have a way to setup a callback with the OS (I doubt so?). Otherwise does it perform some sort of polling?
EDIT - THIS ANSWER IS WRONG. PLEASE IGNORE AS IT IS INCORRECT.
You did not specify a version, so I can not point you to source code. But to answer your question, the way that Apache does it is by returning a Future<T>.
Take a look at this link -- https://hc.apache.org/httpcomponents-asyncclient-4.1.x/current/httpasyncclient/apidocs/org/apache/http/nio/client/HttpAsyncClient.html
Notice how the link says nio in the package. That stands for "non-blocking IO". And 9 times out of 10, that is done by doing some work with a new thread.
This operates almost exactly like a CompletableFuture<T> from your first question. Long story short, the library kicks off the process in a new thread (just like CompletableFuture<T>), stores that thread into the Future<T>, then allows you to use that Future<T> to manage that newly created thread containing your non-blocking task. By doing this, you get to decide exactly when and where the code blocks, potentially giving you the chance to make some significant performance optimizations.
To be more explicit, let's give a pseudocode example. Let's say I have a method attached to an endpoint. Whenever the endpoint is hit, the method is executed. The method takes in a single parameter --- userID. I then use that userID to perform 2 operations --- fetch the user's personal info, and fetch the user's suggested content. I need both pieces, and neither request needs to wait for the other to finish before starting. So, what I do is something like the following.
public StoreFrontPage visitStorePage(int userID)
{
final Future<UserInfo> userInfoFuture = this.fetchUserInfo(userID);
final Future<PageSuggestion> recommendedContentFuture = this.fetchRecommendedContent(userId);
final UserInfo userInfo = userInfoFuture.get();
final PageSuggestion recommendedContent = recommendedContentFuture.get();
return new StoreFrontPage(userInfo, recommendedContent);
}
When I call this.fetchUserInfo(userID), my code creates a new thread, starts fetching user info on that new thread, but let's my main thread continue and kick off this.fetchRecommendedContent(userID) in the meantime. The 2 fetches are occurring in parallel.
However, I need both results in order to create my StoreFrontPage. So, when I decided that I cannot continue any further until I have the results from both fetches, I call Future::get on each of my fetches. What this method does is merge the new thread back into my original one. In short, it says "wait for that one thread you created to finish doing what it was doing, then output the result as a return value".
And to more explicitly answer your question, no, this tool does not require you to do anything involving callbacks or polling. All it does is give you a Future<T> and lets you decide when you need to block the thread to wait on that Future<T> to finish.
EDIT - THIS ANSWER IS WRONG. PLEASE IGNORE AS IT IS INCORRECT.

How to implement CompletableFuture with a priority execution?

I have an issue where I am not sure if the completable future api is able to solve my problem, but would like to open it to the stack overflow community.
I have a case, where I want to call 2 methods. One of these methods (method A) will return some data from DynamoDB. Another method (method B) will return some data from a SQL database. I would like to do these methods in parallel. However, the twist is the customer can ask for data in a certain priority.
For example, a user could pass in `["A" , "B"]. In this case, I would like both tasks to start being done in parallel, but if A finishes and returns useful data to stop execution on task B. If A returns null/not found, then return the result of B.
When reading about completable future's I have not found any methods that seem to be able to do this. The methods anyOf(CompletableFuture<?>... cfs) or allOf(CompletableFuture<?>... cfs) seem to apply to these situations. It seems like there is no way to give a preference to a specific CompletableFuture.
I would love to know how to solve this problem in Java!

Restful Class Thread

I am a java beginner, I met a problem in my Restful class, I just wonder when my code is working on the Restful Class, like the thread is still working on one url request, at the same time I send another request to invoke the same Restful Class, this can work properly or not?
Now, I set a client using "GET" method. Do I need to change the method "GET" so the thread does not need to wait to the "Response"?
Any help, thanks a lot~
When you are using a framework, things are very easy. To answer your questions
How does the class behave when two simultaneous requests are made?
Each of your request will run in a separate thread. So its as simple as two different threads accessing a method simultaneously. Care must be taken not to use any shared resources (including instance variables) in the method. In that case, this becomes a bit tricky.
Do I need to change the method "GET" so the thread does not need to wait to the "Response"?
I am not sure what you mean by this. When you use HTTP, a request will always be accompanied by a response. That's how the the protocol works. Even in case of an asynchronous scenario, an immediate notional response is always returned.

Modal-style programming within a Java server

For my game, I have it running on two servers (one for the game, one for the login system). They both need to interact with each other, and sometimes, ask questions about the state of something else in the other server.
For this example, the game server will be asking the login server if a player is trying to log in:
public boolean isLoggingIn(int accountId) {
//Form a packet to send.
int retVal = sendData();
return retVal > 0;
}
Obviously I'd use an int so information other than booleans can be returned.
My question is, how do I get this modal-style programming working? It'd work just like JFileChooser's getOpenDialog() function.
Also, I should mention that more than one thread can call this method at once.
I assume by modal, you mean trying to block all actions except one. I strongly suspect that this style will lead you into trouble. Modal interaction is a form of locking and therefore not very tolerant to hangups and disconnects and such. To make it tolerant, you need timeouts and cleanup code for cases when someone entered a mode and then nothing further happened. (i.e they closed their laptop, or the game crashed, they unplugged the network cable etc).
If I were you I would instead try to think of things in terms of authentication and authorization.
The quick answer - you need to expose methods on both servers as RMI-capable, and simply invoke methods like you described.
You might find it useful to review the official Oracle RMI tutorial: http://docs.oracle.com/javase/tutorial/rmi/index.html
Althought your design might be wrong - it's your design, and why not letting you shoot your head? ;)
Also, it's worth looking at Spring Security: http://static.springsource.org/spring-security/site/
If you use something like this on a thread that is supposed to handle other requests after it, it would hang up all those requests while it is blocking for a return value if the latency between the game and login servers is high. Certainly what you want instead is a callback so that your thread could handle other requests while it waits for a response.
I see no reason to halt execution of a thread until a value is received. If you need the value for an operation after it, then just copy all the code you have after the call you want to be "modal" in the callback. If you expect to send multiple requests while still waiting for a response, then send a unique "responseId" from the requester's side that the responder can include in its response. Use the "responseId" as a key for a Map with Runnables as values. When you receive a response, call remove on the Map with the responseId key and call run() on the Runnable value that is returned. MINA is supposed to asynchronous and should not block for a response packet.
If you have a really good reason for why you want to handle it all on the same thread, you can look into the java.util.concurrent package. I would implement it using a CountDownLatch of count 1, call await() after sending a request message, and call countDown() when you receive a response by MINA. You have to use an AtomicReference or an array of length 1 to hold the value you received in the response that you can read back into the waiting thread.
PS, you still doing MapleStory work?

Syncronized methods In MultiThreadedApplicationAdapter

I am using red5 for mmo , and I am in trouble with syncronized methods , the methods which i used in MultiThreadedApplicationAdapter always blocking next request. Is it normal ? Because i am doing some database operations in these methods and this syncronized block is making my performance very poor. I decided to use quartz jobs to overcome this stuation, how ever this time clustered topolgy is making me confused . Could you please help me , is it a common solution to use quartz for this problem , is there any body to give me a smarter advice
Thank you
I want to make an addition to clear my question
After extending MultiThreadedApplicationAdapter and create my overriden class , I implemented
public boolean connect(IConnection conn, IScope scope, Object[] params) {
function and in this function I want to set user status Online (As you can see there is not any syncronized literal in this function however it is acting as there is.)
And I want to take user entity from database and then set online status and then save it back
In this part even if I dont use syncronized literal , next coming client is waiting previous completed. I feel like I must create another job with Quartz and make database operations in that thread however I am not sure this decrease my performance. Is there any other way to prevent this block , this seems to be a Red5 limitation ??
This is also mentioned in a blog http://ria101.wordpress.com/2010/03/09/red5-cabin-fever-advanced-scope-and-room-management/
Only one thread can invoke synchronized method of an object, an quartz won't change it. And from your post doesn't seem what you want to achieve.

Categories

Resources