开发者

How to speed up servlet that calls 2 web-services?

开发者 https://www.devze.com 2023-03-13 11:53 出处:网络
I have a servlet that calls 2 web-services. Those 2 web services are called in separate threads. In my servlet thered (class that extends HttpServlet), i start those 2 threads and then wait in a while

I have a servlet that calls 2 web-services. Those 2 web services are called in separate threads. In my servlet thered (class that extends HttpServlet), i start those 2 threads and then wait in a while loop, until those threads finish. This consumes quite a lot CPU (almost 100%) on a JBOSS and Tomcat server util that while loop finishes. I created 开发者_运维知识库code for WS clients with wsiimport tool. It takes quite some time to create WSClient objects for every HTTP request (calling new WSClient (wsdlLocation, serviceName)).

Can you give me any advice on how to further speed things up? How can i make creation of WS client objects faster?


Don't busy wait.

Either sleep the current thread for a short delay until checking again to see if the other threads have finished, or make use of wait() / notify() to alert the current thread when the other actions are complete (this might be very messy inside a servlet though).

As for the process of importing new web-service clients into your application, if you need to process different WSDL's to generate client-side proxy code I am not sure of a better way. The only option may be to find a design where you don't need to keep generating new code at all.


You can create a thread pool and assign calls to threads in that pool. Check out ThreadPoolExecutor

As far as faster creation of proxies, I suggest caching proxy objects in either cache or bind it to JNDI tree. Handle exceptions properly for scenarios where cached reference goes stale so you can re-initialize them.


You probably want to look at using the Tomcat Comet extensions so you don't have to sit in a while loop.

If you MUST use the standard servlet structure, then put a LARGE sleep (something like try{ Thread.sleep(1000); }catch(InterruptedException ie){}) so you spend most of the time in the while loop sleeping. It is only marginally better than a simple while loop, so you REALLY want to look at the non-blocking options as linked above.


Use Servlet 3.0's supported containers which has the support of asynchronous request. Which will allow to suspend the request and resume after some long processing has happened. Asynchronous support is to make the server-side processing much more efficient. It overcomes connection and thread-consumpation issues.

Few of these These support servlet 3.0:

  • Tomcat
  • Jetty
  • Glassfish
0

精彩评论

暂无评论...
验证码 换一张
取 消