So, I have this old legacy JAX-WS service, that does a lot of IO per request, so each request takes quite a bit of time, but doesn't consume much CPU/RAM.
With amount of clients increasing lately, there's a huge problem of thread starvation.
At first, I thought about implementing JAX-WS builtin async support, but it requires clients to be aware of it, and thats a no in my situation. I cannot possibly force them all to update their code.
My current idea is to create special async servlet and manually parse SOAP request and encode the response, but I can't seem to find some good way to do that.
Is there any way to utilize Servlet 3.1 async support when handling JAX-WS requests?
I should clarify, that I cannot use some kind of queue, or otherwise "just return early", because request processing can fail, and client must get that failure.
I've found the solution that works perfectly for me, CXF Continuations API.
http://cxf.apache.org/docs/continuations.html
https://github.com/search?l=java&q=ContinuationProvider&type=Code&utf8=%E2%9C%93
I had to enable async for CXF Servlet, and add jboss module dependency to bundled CXF.
While the whole things feels somewhat like a hack, it allowed me to do proper asynchronous processing without changing service external API at all.
As a bonus, I can even decide whether to suspend request, or process it normally, which helps a lot in certain cases.
You could make use of workflow here where say you have webserver's workers accept the request, you handle it and places it in a queue which is shared by thread which will process this event asynchronously.
This would just mean that you have low wait time on client side and you process the client request asynchronously. In that way, you build your system scalable and can increase number of worker threads on either of the side i.e. webserver so you could accept multiple requests from client concurrently and at the same time multiple threads may be concurrently processing the events from client.
Related
I've been told during stress testing that I need to reduce my app's Thread usage.
All of our other apps follow a 1 request per page model while I've create a single page app that fires 8 AJAX requests on load.
I'm running on Websphere 8.5.5.1 (Servlet 3.0).
I am making 8 AJAX requests from the Browser one straight after the other to the same Servlet, so they are in Parallel.
The Servlet basically checks the request parameters and calls the corresponding ESB service over http and then returns the response.
Please correct me if anything I say here is rubbish as I'm not 100% on any of it...
My understanding is that when the request hits Websphere it gets a thread from the thread pool and uses that thread to execute the Servlet and holds on to the same thread until the response is returned. I'm also thinking that the thread will block while waiting for a response from the ESB. Is that right?
I'm looking at the following 3 options:
1) The best we could do with Async servlets in Servlet 3.0 is put the request on a queue and return the thread back to the pool. Then one or more threads would go through the queue servicing the requests. So we are just swapping one thread pool for another, but we can limit/control the size of the second one.
If we had Servlet 3.1 (JSR 340) we could use a ReadListener and I'm guessing we could avoid the thread blocking while waiting for the ESB response?
2) Queue the AJAX requets on the Frontend so instead of firing off 8 AJAX requests, I fire 3 and when they finish fire the next one and so on, so you never have more that 3 requests in parallel.
3) Make 1 AJAX Request to the Servlet and let it call all 8 ESB Services and combine the results.
Can you advice me if any of the above solutions are valid and if I even understand them fully and suggest other possible solutions?
Thanks.
Trying to answer point by point:
"Websphere it gets a thread from the thread pool and uses that thread to execute the Servlet and holds on to the same thread until the response is returned." Your understanding is incorrect. This would have been correct if WS was using old IO, but WS version 6.1 onwards use Native IO (NIO) and Asynchronous IO (AIO), which allows a handful of threads to cater thousands of concurrent connections. See this reference. Thus, you should not worry about making concurrent connections from AJAX client, and parallelism is fine.
Having said this, the application must try and avoid doing any blocking operations, that block any worker threads which otherwise are capable of handling multiple concurrent connections. Just think on how you can execute each task async, which would mean that you should be able to do things without blocking yourself. In your case, you are waiting (and blocked) on the responses from ESB. There will be ~8 blocking connections to ESB service. Please review how you are making calls to ESB, and if you can use NIO for these calls. Understand that WS is a client for calls to ESB, and it has to use non-blocking IO for the operations it does on ESB.
Alternatively: Is there a way, that ESB can also behave async, and release the WS request thread? ESB can callback a WS service URL, when its done with the request, and you can then create the AJAX response. That way you would not hold up WS threads.
In your options above, clearly #1, and #3 will not help you. #2 would of course limit the parallel operations to ESB to three. But, I would recommend to see if the calls to ESB can be done in a async manner, and non-blocking manner. That would be ideal.
Update
To make an async and non-blocking calls to an external service from your app server, consider using a library like async-http-client. This uses Netty by default, but other NIO frameworks can be plugged in as well. The README.md of this project has good examples on how to make an HTTP call, and write response on the completion of HTTP request. You can use the onCompletion to write response back to your client.
The concluding statement:
Be a non-blocking server when listening to your clients. So far as possible, make sure that the request threads of the container/server do not block.
Be a non-blocking client when making calls to an external service. This will allow you to make a large number of requests with a handful of threads.
I've been reading about asynchronous servlets in an attempt to see if it fits my use case.
The server will receive long-running requests that take time to read from the db, and process each message.
Here is the basic flow:
Client makes a request to validate some data.
Server receives a request for the long-running process (~1-2 minutes)
Request needs to talk to a db (redis, in my case) to grab 100,000 docs in batches.
Processing occurs for each doc, and if it is valid, it is written to the client. Similarly, upto 25 sample docs are written to the client.
When the full data set is processed (but no more than 25 docs are written), a result is calculated and sent.
Connection is successfully closed.
So there is a db wait, which all similar requests will have, and then the processing for each doc, which happens in the same thread.
Is there any benefit to switching to async servlets versus a regular synchronous servlet in this case?
Additionally, if I do use an async servlet, would there be any benefit in also trying to parallelize the processing of the docs with thread pools? It seems like I won't be getting any additional benefit, but want to clarify this as well.
Thanks for your help!
Is there any benefit to switching to async servlets versus a regular synchronous servlet in this case?
Yes. In fact it is the use case that all manuals tells you "async servlets are useful for this"
Additionally, if I do use an async servlet, would there be any benefit in also trying to parallelize the processing of the docs with thread pools? It seems like I won't be getting any additional benefit, but want to clarify this as well.
Unrelated. Async servlets just releases resources so your server keeps answering petitions even if lots of servlets are waiting for results. It is not related to the speed of answering the asynchronized request.
Any benefit that you get from optimizing your logic will be good for the server and the user, but it would be good either if you are using async servlets or sync servlets.
I need to be able to achieve two threading tasks in a container and need to understand the best practices for doing this. Here are the two classes of task I need to accomplish:
During a web services call, I need to start a thread that continues processing after the response has been sent. No message is required back to the original sender when processing is complete.
A web services call may need to spawn multiple threads that need to run in parallel to each other. The response to the original request should be blocked until all the workers have completed. Elements of the response will be drawn from pieces of each of the thread's results.
Of course, I could create my own instance of a java.util.concurrent.Executor and use it, but I suspect containers might be smart enough to provide one that they manage.
FWIW - I'm using WebSphere 6.1 on JDK 1.5.0 (I know, ancient...but it is what it is). I am running web services developed using Apache CXF, so I'm in the servlet container, but configured with Spring.
For 1) you might want to look at Asynchronous Beans. Alternatively use a Message Driven Bean which picks up and actions a message you send to a Queue. There's the Quartz stuff from Spring you might want to look at too. I think with Servlet 3 (no chance on WAS 6.1!) you might get the async support without the Async Work Manager or JMS approach, but until then I don't know a better way than these patterns.
For 2) generally blocking the request is a risky business (what if you hit the timeout). However, you're in the servlet container so you're ok using something from java.util.concurrent e.g. ExecutorService as you mentioned. Alternatively use messaging to send the work off somewhere else and block until it completes.
Generally, I wouldn't start threads from inside a container because there is the chance that j2ee full compliance is turned on and your app would die. Under full compliance threads are not allowed to be created. What you want to do is set up a JMS queue that you submit your "work to be done" to. You can then have an MDB listening to the queue which performs that actual operation that your thread would have done.
I am in the process of writing a web-app that uses multiple web APIs.
For a single request of a single user, my app may need to perform up to 30 HTTP requests to other sites. The site housing the web-app can have hundreds of concurrent users.
I've been looking around trying to figure out which library should I use. I'm looking for a mature project that has detailed documentation and tested code, one that will still be around in years to come. Not sure if something like that exists (!)
Couple of questions :
In a case such as described above, should I be using an asynchronous HTTP client (without threading), or a regular (possibly pooled) HTTP client (with threading)? Asynchronicity relieves my app from using threads, but makes the code more scattered - will the above mentioned number of requests burden my server too much? (it says here that asynchronous is more scalable)
Which library is the common one to use? Is it Apache HttpComponenets HttpClient or its asynch couterpart HttpAsynchClient - which is in Alpha...)? How about jfarcand's AsyncHttpClient?
Okay, let's say I will use threads.
After digging around I realize that spawning threads from within a servlet (in my case - a Struts action), may be a big No No :
related questions:
What is recommended way for spawning threads from a servlet in Tomcat
Need help with java web app design to perform background tasks
Can i spawn a thread from a servlet ?
The way I see it, these are my options:
use my own thread pool (container doesn't manage my threads)
use a WorkManager such as CommonJ (seems like an inactive product)
use a 3rd party scheduler such as Quartz (may be an overkill ... ?)
I would appreciate any recommendations for this specific use case - aggregating lotsa data from different web services (this aggregation is invoked by a single user's single request).
Good question. I would try an asynchronous solution first to see how everything works. The asynchronous solution would be the simplest to implement.
If that doesn't work, try a more threaded model.
I would use HttpClient for making your requests. I've worked with it a lot and use it for any http work that I have to do.
A single thread for each remote http connection, and using a synchronous http client will probably be easier. I would try this approach first, and see if it is fast/scalable enough. For the synchronous approach, apache http client is a good choice.
If a synchronous solution is not good enough, something like netty may be a good fit. It uses NIO so you won't get thousands of threads.
I do not know of any existing software to do this for you that will not be overkill. But you might try splitting things up. That is, separate the fetching of the data of the showing of the result. Since you do not provide no further details on the problem at hand I cannot say for you whether that would be feasible or not.
Basically the idea is to create a service that will fetch those 30 subsequent requests for you and if possible process it into a request. The client of this service is the service that is running on the web. It will receive a request from a user and subsequently put it's own request trough to your data service. When the data service is ready it will return it's response. Either synchronously or asynchronously.
You could program your data service in any language you want, even Java, without being bound to servlets ensuring that fetching the subsequent 30 request and combining them in a response isn't being done by the webserver. This could also enhance the responsiveness of your web server itself.
Nutshell: branch of the ``difficult" tasks to a specialised service where you can transparently handle parallelism.
I would use Jetty and in the servlets I'd use the continuation mechanism to free up the thread while waiting for the third party web request to complete. This will allow maximum concurrency on your server, as you can have a lot more suspended requests than threads.
You can use either continuations or the servlet 3.0 asynchronous API, the end result is the same.
I am writing a web service in Java which needs to handle a large number of requests / second. The general flow will be:
Web service receives a request from client
Returns a 'keep polling me' response to client
Calls another web service (or
services), and waits for them to
respond (with a timeout)
Client polls our web service, until
it receives a response (with a
timeout)
Researching on the Internet, I have found two general approaches to writing web services:
Spawn a thread for each request
Use the Reactor pattern (central dispatcher thread responds to IO events)
Do you have a recommendation for which approach is generally better, and what are the pros/cons of each approach? I would also appreciate pointers to examples.
Don't think multi-threading. Think asynchronously. I happened to have just coded an async handler that ran 2,000 RPS with <10 threads in IIS. Not sure how java works since I'm a .net guy but I gotta believe they have similar BeginXXX/EndXXX methods. If you ever spawn a thread then you're not considering all the places your code can block: data base IO, File I/O, web services, etc. These are the places your performance will cause your site to be slow.
Async, Async, Async.
Chant and repeat.
In addition to "No Refunds No Returns" response, I'd say yeah "Think Asynchronously" as you should be allowing your container to manage the multi-threading/scalability and high-availability issues of the web services it has deployed, this allows you to set-up things like clustering and so forth using your application container.
EDIT: So in conclusion, there isn't a pattern as such, maybe you should explore the scalability/availability features of your application container...
Asynchronism is indeed the right approach but don't manage this yourself, use something that supports asynchronous web service invocation like JAX-WS 2.0 (which uses the Future interface and/or the Executor framework from java.util.concurrent). See Asynchronous Web Service Invocation with JAX-WS 2.0.