I'm struggling with asynchronous processing in Spring. I've decided to use #Acync together with #EnableAsync and ThreadPoolTaskExecutor.
Is there any mechanism responsible for thread's hanging coming together with Spring's #Async? The Async method calls few external resources with RestTemplate and I should probably somehow protect against hanging a thread.
The only possibility of hanging thread with #Async is if request to any external resource goes to hang state due to some network or resource issue.
The solution would be, provide connection timeout for each external resource call.
Related
As stated in the documentation passing from quarkus-resteasy to reactive is as simple as changing only maven dependency and it should works.
In our quarkus project, we create a session in #PreMatching filter and save it in thread local. In other filters we update session with other information. Session information are then used in all services and resources.
After somme performance tests, even we have more RAM and CPU we see only 200 threads used in prometheus. We have the possibility to change max-thread in properties file or moving to reactive programming.
At start point, we changed to quarkus reactive and we experienced the following problems:
When adding reactive to quarkus-resteasy we experienced the following:
The first thread that intercept request is an event loop thread. Then it delegate execution to worker thread if method annotated with #Blocking, else it continue execution with event loop thread.
Problem 1) Blocking method:
All information added to session in #PreMatching filter are lost. But information added in the session from other filters with priority for example #Priority(Priorities.AUTHENTICATION - 10) are present in the worker thread.
Problem 2) NoBlocking method:
With a few requests all thing works fine, but with 100 parallel requests, we experience a random losing session information. After googling, event loop thread use a different context (Vertx thread context) and I haven't find any documentation that explain how to move from ThreadLocal to Vertx Thread context while moving from quarkus to quarkus reactive.
Problem 3) Transform method to return Uni, ThreadLocal information are propagated but I haven't yet test it under load. [UPDATE] same as Problem 2)
Problem 4) Cant find how to run integration tests in Vertx Thread
Any help on how to move projects using ThreadLocal to anything else when moving to quarkus reactive will be appreciated
I'm going through this tutorial which explains the difference between using a Callable and a DeferredResult with the servlet 3.0 spec and Spring. For Callables spring manages the thread, so I presume then that we need to configure a thread pool? How is this configured for Spring Boot?
WebMvcConfigurationSupport.configureAsyncSupport() is used to set up async request processing. AsyncSupportConfigurer provides configuration for all controller methods returning Callable and DeferredResult. The underlying thread pool can be configured through AsyncSupportConfigurer.setTaskExecutor() using appropriate AsyncTaskExecutor implementation (ThreadPoolTaskExecutor for example). It's also possible to update this configuration on per-request basis by returning WebAsyncTask instead for Callable. Linked javadocs describe all of it in more detail.
I am in the process of developing an EJB that makes 10+ calls to other components (EJBs, Web services, etc.) as part of it's business logic. In my case, performance is a huge concern. This EJB will be servicing a few million requests a day.
My question is: For each of those 10+ calls, how can I enforce a timeout?
I cannot wait more than 'n' seconds for any one of the calls to return. If a call takes longer than 'n' seconds, I will use a default response for processing.
I would normally use a Executor to solve this problem but, from what I understand, one shouldn't spawn threads from within an EJB as it may potentially interfere with the EJB's lifecycle.
how can I enforce a timeout?
The ejb3.1 specification provides the possibility to set a timeout using #AccessTimeout annotation that applies for serialized client calls that have to wait when an Session Bean instance
is busy executing a previous request.
Clearly (and explicity described in the specification) this applies to StateFul and Singleton session bean, although it could be implemented for Stateless in the case the bean pool run out of available instances.
Notice, once the client-invoked business method is in progress this timeout no applies.
Other possibility that is not part of the specification but, is supported by several servers (see JBoss example) is to define a timeout at the remote client side. If the client invocation
takes longer than the configured timeout, the client will be informed, however, the server execution will not be interrupted which it is not good enough.
Set a transaction timeout neither is a good option because there is no guarantee the thread that executes the business logic will be interrupted when the transaction timeout expires.
I would normally use a Executor to solve this problem but, from what I understand, one shouldn't spawn threads from within an EJB..
Instead you could use ManagedExecutorService class that is an Executor extension suitable to use within a EJB Container.
Aditionally, to implement asynchronous call within an EJB, take a look at #Asynchronous annotation, which provides a high level abstraction to solve the multithreding issue you are facing.
Cancel() method from Future class, allows you to interrup a thread's execution if you consider that the process has taken too long.
since you are not providing much detail of your environment:
use bean managed transactions and set the transaction timeout
EE7: provides an managed executor service
EE6: custom executor service as a JCA connector
We are using JTA to manage global transactions in servlet context. Additionally, some of the servlet threads are invoking asynchronous beans. Currently, I have designed it in a way so that the asynchronous bean (Work Manager) gets its own transaction.
It's my understanding that two threads can participate in the same XA transaction. But if I start using the transaction created by the servlet, for the asynchronous bean, will that block the servlet thread until all participant have committed or rolled back? Both the servlet and the async bean can commit and rollback.
The app server is WebSphere, and we are NOT using Spring.
Your response is appreciated.
Using the same transaction both for the servlet thread and an asynchronous worker conflicts somehow with the general idea of messaging. Messaging is a means to decouple, whereas a transaction keeps things tightly together.
In other words, if you want a worker to use the same transaction, I would not implement the worker in an asynchronous way.
As for two threads participating in the same XA transaction, this might not be supported, look here for details. Even if it worked, it could be troublesome to share resources over a thread context generally speaking (file handles, connections, transactions whatever).
As for the servlet thread, whether it blocks: I have never tried it (for the above reasons), but I assume that it does not block: You would have to wait/poll for your async workers, until they have finished (or failed). Otherwise the servlet just reaches your commit statement.
I have a Web application using spring and hibernate and struts (it runs on Tomcat)
The call sequence is something like this...
Struts action calls spring service bean which in turn calls Spring DAO bean. The DAO implementation is a Hibernate implementation.
The question is
Would all my spring beans be running in the same thread ?
Can I store something in the ThreadLocal and get it in another bean?
I am quite sure this would not work in Stateless Session Bean.
The EJB container can (or will) spawn a new thread for every call to the session bean
Will the spring container do the same? i.e. run all beans in the same thread ?
When I tried a JUnit test - I got the same id via Thread.currentThread().getId() in the Test Case and the two beans- which leads me to believe there was only one thread in action
Or is the behavior unpredictable?
Or will it change when running on Tomcat server ?
Clarification
I do not wish to exchange data between two threads. I want to put data in the ThreadLocal and be able to retrieve it from all beans in the call stack. This will work only if all beans are in the same thread
Spring doesn't spawn the threads. Tomcat does. Spring is just creating and wiring up the objects for you.
Each request from the browser is processed in one request. It is Tomcat that handles the request. It is Tomcat that creates the thread to process the request.
Assuming you have just created a singleton bean in Spring called "X". Then the same instance of X is used by all requests.
The Spring beans don't live in a thread. They are just allocated on the heap.
Would all my spring beans be running
in the same thread ? Can I store
something in the ThreadLocal and get
it in another bean?
AFAIK for the components you mentioned (service bean, DAO bean - i guess they are plain spring beans), Spring does not spawn a new thread. I do not understand your use case (ie, exchanging data between two threads).
For most webapps, a new thread is spawned for each new request, and if you want to share data between two requests you normally:
- use the get/post parameters to pass the data
- use the session to share data
To answer your question, I'm pretty sure the spring container does not spawn threads for most components.
Yes, you can do this. The same thread will be used to execute your action so the ThreadLocal will work. Typically, the same thread is used for the stateless session bean as well, assuming it is running in the same app server instance. I would not depend on this though, as it is probably vendor dependent.
We use this technique to access the callers identity anywhere in the code. We use session beans and jms as well, but explicitly pass the information between containers and set the ThreadLocal at each entry point. This way it doesn't matter if the bean (session or mdb) are local or not.
In addition to all the other answers, I will just add the following:
Normally the only reason to switch threads is because of some requirement for parallellity. Since this normally does not come for free in terms of complexity, you will usually be clearly informed when this happens.
Switching threads within what appears to be a single-threaded processing of a request is actually extremely complex. This will normally only happen at one place in a container, and this is usually handled by tcp/ip socket readers that receive the request from the external clients. These reader threads usually determine which thread(pool) should process the request and forward the request to that thread. After that the request stays with that thread.
So normally the only thing that will/can happen is that additional threads get created for parallelity or asynchronous processing (like JMS).