Difference between a Vertx Context and a Kotlin Couroutine Context - asynchronous

Can someone please explain the differences and similarities between a Vertx Context and a Kotlin Couroutine Context?

A Vertx context defines the concurrency of code execution in Vertx, it usually is associated to an event-loop (when it's a default context) and any asynchronous operation performed in Vertx when code is running in that context will guarantee that the callbacks will be performed in the same Vertx context. Such context is usually shared by many concurrent executions, for instance an HTTP server will share the same context for the concurrent HTTP requests it serves.
A Kotlin coroutine context is associated with a coroutine and plays a similar role but it is usually associated with a single flow of execution that is determined by the Kotlin coroutine construct such as launch. A Kotlin coroutine is usually in Vertx associated with a Vertx context so when the coroutine resumes, the Vertx context can be used to ensure that the Vertx concurrency model is respect

Related

Does #Asynchronous in EJB 3.1 closes/releses the connection when the Async method is completed

In the EJB 3.1, we have annotated a method with #Asynchronous and using Future.get to fetch the results.
Question is when we do future.get(), does the database connections from the connection pool and resources are released which was being used in that async method?
Database connections are closed according to your configured connection pooling policy of the used application server.
When talking about session beans and container managed transactions, the connection is returned at the end to the transaction scope of the called business method.
Since session beans with #Asynchronous annotations do not support transaction propagation (e.g. see EJB 3.2 spec 4.5.3), a new transaction is created with every call to a business method if the method is configured with REQUIRED or REQUIRES_NEW. Thus, this transaction scope ends with the called business method returning.
There is a chance they are :) Otherwise using #Asynchronous would quickly lead to application crash due to lack of resources...
Anyway as resources are stored using Thread Local storage in Java-ee and as #Asynchronous defer the execution to a dedicated thread, the resources used there (including JCA Connection - and so database one) are also dedicated to the #Asynchronous method execution (not inherited from the caller thread).

#EnableAsync vs AbstractDispatcherServletInitializer#isAsyncSupported()

I am too much confused the difference between #EnableAsync and AbstractDispatcherServletInitializer#isAsyncSupported().
As per this link,
The isAsyncSupported protected method of AbstractDispatcherServletInitializer provides a single place to enable async support on the DispatcherServlet and all filters mapped to it. By default this flag is set to true
Based on my knowledge #EnableAsync is to enable scan #Async annotations and provide multi-threading support. But still I am unable to understand when to use AbstractDispatcherServletInitializer#isAsyncSupported()
There is a big difference between the two, #EnableAsync with #Async are not concerned with spring-web-mvc, they allow the execution of a method asynchronously using a TaskExecutor. For example, if you had a clean up service that deletes all logs in a server, you can allow its doCleanUp method to run asynchronously without having to write the scheduling code, spring will take care of it for you:
public class CleanUpService {
#Async
public void doCleanUp() {
// This code will be executed using a different thread than the calling thread by a TaskExecuter
}
}
As you can see this is not related to web development in particular. Also note that this method returns void, but if you wanted it to return a result, for instance CleanupStatistics, you can't just write the method like this
#Async
public CleanupStatistics doCleanUp() {
// This code will be executed using a different thread than the calling thread by a TaskExecuter
return new CleanupStatistics("someinformation");
}
You are required to return either void or a Future. A Future represents the result of an asynchronous computation that you can use to obtain the result of the computation when it becomes available. We will get back to Future and see how it can be used in controller methods while isAsyncSupported is true.
isAsyncSupported is a totally different story, this flag enables Servlet 3.0 asynchronous request processing, prior to Servlet 3.0 a servlet container (Tomcat for instance) would have a thread pool that handles the processing of Http requests, each thread would handle the request till the processing is complete, even if the thread had to stay idle, for example, to wait for a database query to finish or for an API call result to come back. This affects the scalability of the servlet container. In Servlet 3.0 requests can be handled asynchronously, the container thread will call your handler method, in its turn the handler method can schedule its work to be done asynchronously and return immediately, freeing the container thread to process other requests, after the result is available any container thread can resume the processing of the request. From the prospective of the client, nothing has changed, the client, all that time, is still waiting for the result to come back, but from the perspective of the server, the container threads are better utilised.
In spring-mvc you can trigger asynchronous request processing for a controller method by returning certain java types, for example
DeferredResult
Callable
ListenableFuture
CompletionStage
CompletableFuture
Reactive types
and streaming types
If you wanted to use #Async with isAsyncSupported you could write a controller method that calls a method annotated with #Async and returns ListenableFuture.
You can learn more about task execution and scheduling here. And more about Servlet 3.0 asynch here.
If we want to experience benefits provided by Spring MVC and don't want to manually register DispatcherServlet it'll be better to use: AbstractDispatcherServletInitializer. It adds two abstract methods: createServletApplicationContext() and getServletMappings().
First method returns WebApplicationContext that will be passed to DispatcherServlet, which will be automatically added into container ServletContext. This context will be established as a child of the context returned by createRootApplicationContext() method. Second method -returns mappings that are used during servlet registration.
The #EnableAsync annotation switches on Spring’s ability to run #Async methods in a background thread pool. For Enabling asynchronous processing with Java configuration got by simply adding the #EnableAsync to a configuration class.
#EnableAsync
#Configuration
public class SpringAsyncConfigurer implements AsyncConfigurer {...}
#Async is applicable only to the public methods.
Calling the #Async method within the same class woould not work.

How to use ManagedExecutorService with #Asynchronous method?

I have a Java EE app that's deployed on WildFly AS.
I have a method annotated with #Asynchronous and I need to set the max number of threads for this method.
I configured a new <managed-executor-service> in server config, but I don't know how to bind it to an async method.
This link: https://developer.jboss.org/message/851027#851027
provides a good answer to how (or when) to use #Asynchronous and when to use JSR-236 ExecutorService and concurrency utilities:
In short, #Asynchronous is a annotation (EE6) to mark an EJB method as async.
You can invoke the method and keep the future object to check whether the method is finished and get the result. The EJB Concurrency Utilities are provided to have a safe way in EE7 to delegate work to a parallel thread. Threads started by this ConcurrentUtilities are managed by the container.
In difference to a direct start of a Thread (which is not allowed for an EE application). There is less overhead than using #Async and you have a bit more control.
See also this link about how to inject a MES:
http://www.adam-bien.com/roller/abien/entry/injecting_an_executorservice_with_java

Singletion EJB an Async method (LockType.Write) block till the method processes or will it release lock as soon as it returns the control to client?

I have a singleton EJB(3.1) which is using default container managed concurrency and default (LockType.Write) for all methods, since its not specified explicitly.
I have an Asynchronous method which looks as below:
#Asynchronous
#TransactionAttribute(value = TransactionAttributeType.NOT_SUPPORTED)
#AccessTimeout(-1)
public AsyncResult<Boolean> myAsyncMethod(....){
}
My question is, when invoked, will this method lock the whole singleton bean till the above method completes processing or will it lock only till the time the method returns the Future object to the client ?
Also what effect will "#AccessTimeout(-1)" the on the concurrency of this method/bean.
My question is, when invoked, will this method lock the whole
singleton bean till the above method completes processing or will it
lock only till the time the method returns the Future object to the
client ?
The singleton bean is never locked at all on the client thread. The client thread schedules the asynchronous method and returns a Future without accessing the bean. The thread that eventually executes the asynchronous method will lock the bean for the duration of the method execution.
Also what effect will "#AccessTimeout(-1)" the on the concurrency of
this method/bean.
The #AccessTimeout(-1) will cause the thread that eventually attempts to execute the asynchronous method to block until the container-managed lock can be acquired.
Answer to Q1: since the method is marked #Asynchronous, it will return immediately. No blocking what so ever
Answer to Q2: Using #AccessTimeout(-1) on a session bean method/class means that once a thread enters that method, it will block for ever until it completes processing. However, in this case its not a concern since the #Asynchronous will kick in, free up the calling thread and start the processing in a different background thread
Coincidentally, I happened to write a blog post on this topic couple of days back. I would also recommend reading up on the EJB 3.2 specification document - its a reliable and thorough reference material

Thread safety in Server side code

I am new to server side coding and JSP/servlets. I have a code which has 3 classes. 1st is a Serv class inherited from java httpservlet. In this i have doPost() method implemented. In doPost() i use object of the 2nd class ResourceClass. ResourceClass is a singleton class. Hence essentially to use any method is do something like ResourceClass.getInstance().readResource();
Now readResource furthur uses Java Native access library to read a resource from disk. Now my question is Since as i understand if 1000 clients connect to my server(Apache Tomcat) for each new request i will have a new servlet serving the request. But all these servlets will essentially use the same singleton object. Hence will this reading be thread safe.
I do not change any internal state. So i think it wont effect my output hence the whole stuff is Idempotent. But will all these requests be queued making the singleton class object a bottleneck. Or will each servlet has its own copy.
Also if i change the Resource state then in that case will it be thread safe.
First of all, you won't have a new servlet for each request. The same, unique instance of servlet will be used to concurrently handle all the requests. The servlet is also a singleton: the web container instantiates only one instance.
You say that the requests to your ResourceClass singleton will be queued. They won't, unless you mark the method as synchronized or use some other locking mechanism. If you don't, then the threads will invoke your singleton method concurrently.
Whether it's thread-safe or not is impossible to say without seeing the code of your singleton and the code of the JNI library. The fact that it's read-only is a sign that it could be thread-safe, but it's not guaranteed.
In a Java EE server, you only have 1 instance of each servlet.
On the other hand, each http request is processed by the server in its own thread.
There is one instance of ResourceClass because it's a singleton so you will have a bottleneck if the readResource() method is synchronized.

Resources