In Java is a callback synchronous? - java

When you use a callback in Java (you can't do that right?), does it block execution until everything is finished? Am I using the wrong terminology. I am familiar on the purpose of a Callback as it relates to Inverion Of Control, Interface, etc., just not the sequence of events.

Converting to an answer from a comment:
All method calls are synchronous, in that they return a result to the caller. However, a callback implies potential asynchronous behavior wherein you define a method to execute when some action is complete. For that matter, the state of the application may determine whether the call is synchronous (i.e. caching of resources). In order to know whether the underlying action is synchronous or asynchronous you need to read the documentation for whatever library you are using. In summary: read the docs

A callback is just a piece of code to execute. It can be invoked synchronously or asynchronously.

Yes.
That's not to say that invoking the callback won't start a new thread internally to do something asynchronously, but the callback method itself is synchronous.

Related

Java: Best way to delegate to method w/o waiting for it to complete

I have a AWS SQS listener that, when it receives a message, invokes the proper method to handle it. The problem is that it also waits for that method to return, and if it takes longer than the visibility timeout, we get the message again. Take this for instance:
if (FULL_LOAD.equalsIgnoreCase(msg.getLoadType())) {
//Create the controller and process the message
log.info("Process valid Full Load message from "+msg.getDataSourceName());
productRefService.fullRefresh(ctx);
jobCompletion(msg, jobStart, start);
}
// continue processing
So right now, before the listener can continue, it must wait for productRefService.fullRefresh to complete, which can be up to 15 minutes. It seems to me there should be a way to invoke the service method and continue, but I'm not sure how that would look. Make productRefService async?? Use the Callable interface? I'm not interested in the return value - I just want the listener freed up.
Per your request:
Good question! If you're on Java 8 or higher, consider
CompletableFuture:
Asynchronous Programming in Java
Guide To
CompletableFuture
A Java "CompletableFuture" is analogous to Javascript "Promises" or C#
"async/await". It's return value gives the caller something to "wait
on" (if it needs to).

What is correct JavaDoc to mark method as blocking?

I'm making Minecraft Plugin which aside from it's normal behaviour provides API for developers.
In Minecraft it's important to run blocking operations Async - so main thread will not be blocked and thus not causing lags.
In my API I have simple method which loads data from Database. This method doesn't care about what thread its executed on - I want to inform user about blocking behavior of this method and that he should run it Async (preferably via BukkitScheduler). I don't want to return CompletableFuture.
Is there any special JavaDoc item or method annotation for this purpose?

What when #Asynchronous method calls another #Asynchronous method?

So the title says it all, what happens when during asynchronous invocation of a method performed by Java EE Container, there is a call to another method that is also annotated with #Asynchronous. I would expect that there will be one more asynchronous invocation. However the specification does not say anything about this, so could this also be Application sever vendor specific?
Currently I am analyzing the performance of a Java EE application that runs in Websphere. And I clearly see within the method tree that the second asynchronous method will actually be synchronously called. This actually makes sense for me, because we are already in some kind of asynchronous context, so instead of submitting new task we can just execute it right away..
Any idea about this?
This works. The second call will just post a request to execute that method to some internal queue that the AS maintains.
BUT... be very careful with waiting on the results from any Future that the second method might return. If the second method is a void method it's always safe.

Can I check if an async request completed?

Is there any way to check if an async ServletRequest is completed from an AsyncContext? I saw that spring has some kind of wrapper that supports this but googling around I couldn't find anything in the standard library. That is what I was hoping for.
I am using Tomcat 7.
Sounds like one of the two - you either need a listener that will be called upon a asynchronous request completion or you don't need to use an asynchronous call.
Your question is a bit too general.
Talking generally - asynchronous calls are used when the caller is not interested in immediate result of the call.
If the caller is interested to know the result of the call immediately then synchronous calls should be used.
If the caller is not interested to know the result immediately (for example it has secondary priority, like logging in some business applications), but some action should be performed upon the end of execution of asynchronous calls you should use some sort of a listener.
What you need for asynchronous call is some listener (of class javax.servlet.AsyncListener).
In the listener you will know for sure that the asynchronous call is over (onComplete method) and may perform some action to finalize/complement the asynchronous call.
Again, if you see that the caller of the request needs to know the result upon completion immediately, there probably is a mistake in your architecture. You should use a synchronous call - just wait until the call is done and you will have the result of it. Using an asynchronous call is wrong in this situation.
I saw how people use some sort of a loop to check from time to time the result of a asynchronous call, but it looks like in 99.99% of cases such approach is the result of some architectural mistake.
You can register AsyncListener which can implement onComplete() method.
The AsyncListener needs to be added to the AsyncContext.

Wrapping a callback function in a single threaded fashion

In my program, I am essentially trying to connect to a publisher and get data. The basic functionality is there in these steps
I make the connection to the publisher with username and password etc
I make the request for data. Method exits
The publisher's API gives me a callback to a method onDataUpdate(Object theUpdate)
From there, I can print the data, or write it to a database or anything I need to do. That all works.
My problem is, I would now like to wrap the functionality in such a way that a calling program can say request the data and receive it as soon as I have it. Meaning, I want my exposed method to look like
public Object getData() {
subscribeForData();
// somehow wait
return theUpdate;
}
How can I make this happen? Is there some way I can use threads to wait/notify when I've received the update? I'm a newb to stackoverflow and also multithreaded programming, so any help and sample code would be much appreciated!! Thanks in advance.
In this case I would prefer to use CountDownLatch, where i'll initialize my lathch with count 1 as soon i subscribe for publisher i will call await() on latch and when i get the callback i'll countdown the latch.
Use a SynchronousQueue. Create it in getData, call put() in the callback method, then call take() in the original thread at the end of getData().
Check out CompletionService, especially ExecutorCompletionService. There is a nice example of a web page loader/renderer in the book Java Concurrency in Practice.
I'm not entirely certain about your question but I'll give it a shot - hope it helps :)
You could use a blockingqueue in java for this purpose (producer consumer message) - if you write to the queue when the callback gets invoked - from another thread, you could read from the queue. Blocking queues are thread safe (but may not fit your requirements).
You could also look into readwrite locks if you only have one thread writing to a collection and perhaps multiple readers (or even just on reader).
You could also look into the observer pattern - for reference: http://www.vogella.com/articles/DesignPatternObserver/article.html
If neither of those work, one could look into using a queue/topic from an in-VM messaging server such as ZeroMQ/ActiveMQ or perhaps something like Redis/HazelCast.
Hope it helps and good luck
Converting a asynchronous call to a synchronous one is an interesting exercise, I use it often in interviews (and the reverse, wrapping a synchronous call in asynchronous).
So there is a requestData method that is going to return immediately and it (or something else) will later call onDataUpdate in a different thread. You want to create a new method, say requestDataSynchronous that does not require the caller to use a callback but instead blocks till data is available and returns it to the caller.
So what you need for requestDataSynchronous to do is:
call requestData
wait till onDataUpdate is called (in a different thread)
get the data onDataUpdate received
return it to the caller
Of the above, #2 and #3 have to be done by some mode of inter-thread-communication. You can use wait/notifiy but it might be much simpler to use a BlockingQueue. onDataUpdate writes to it once data is available, and requestDataSynchronous reads from it, blocking on the read until onDataUpdate writes into it.
Using ExecutorService might make this even easier, but it will be useful to know what's going on.

Categories