2

Setup: Apache Tomcat 8 Web Server/Java Spring Framework/MVC

Background: I have multiple servers attached a load balancer. Every time the load balancer forwards a request to a web server, the server pushes the request onto a remote queue.

Now, I'm trying to set up multiple threads on my web application, so that these threads can be used to process the requests on the remote queue (retrieve them from the queue and then start working on them).

In the past, I'm only working on a single server instance. The server would receive requests directly from the front end in the Controller, and then it would generate multiple threads to handle theses requests in parallel. All of the calls (thread pool creation and others) are triggered once the first request is received (Requests coming in --> Create threads --> Process the requests).

Problem: Now, instead of creating threads after the request comes in, I want to create the threads once the web application starts running, so that it can start pulling requests from the remote queue right away. Simply put, I would like to decouple thread creation from request handling. Ideally, I would like to have these threads (a fixed number of them) running all the time, pulling request from the queue whenever they are idle. And in the Controller, when ever the server receives a request, I'm just going to send it to the queue.

This is my first time working with the Spring framework, and so I'm not sure what would be the best approach for doing what I need. The Controller (the method that handles the incoming request) was the only program entry point that I know of. Therefore, I don't know how and where I can create these threads on application startup. I want the threads to be very robust, because they are going to do some heavy processing on the requests.

The following are some of the ideas I came up with: - I'm thinking about using TaskExecutor to create the threads that I need when the context is loaded (not sure if this is a good and robust approach). If I were to use TaskExecutor, how should I manage the life cycle of these threads and other resources? - During my research, I also came cross WorkManager. I'm not sure if it does what I need.

Any suggestions and pointers are appreciated!

4

1 回答 1

0

I don't think you need to worry about the threads yourself. Spring has lots of support for producing and consuming message queues. Just wire up a message listener, and I think it should start consuming those messages.

于 2016-03-05T05:12:48.837 回答