Friday, May 1, 2009

Java Multithreaded Web Service Performance Tips

Right after Java 5.0 Concurrency Model was released, MultiThreaded Java Web Service application is always an interesting subject. It's easy to tweak codes that are slowing down the application. But it was always a challenge to tweak it at the container level.

A former colleague of mine and me worked to figure out why our Web Service Application was slowing down and always leaves lots of run-away processes. Here are some of the findings we had implemented to keep our application rolling! The sample codes below reflects a reporting process where requests to generate a report is made and how the threads handle the status of each requests.

Use Java Concurrency API
There are only three classes that I've used to implement a MultiThreaded WS Application.
Runnable, ScheduledExecutorService, and ConcurrentLinkedQueue. 

The ScheduledExecutorService implements ExecutorService and is of type Executor.  Executor is responsible for abstracting the gory details of implementing threads into simpler methods. And it works all the time.

ScheduledExecutorService allows you to provide what the initial time is to run the thread associated with it, at what frequency, the thread to run and the unit of time. Was very easy to use and you can configure the thread pool size as well. The application I wrote had 5 threads running concurrently and it never had any problems at all. They all shared the ConcurrentLinkedQueue object resource and I didn't use the synchronized keyword to make this object thread safe since by default it already is.


Using the Runnable interface is better than just extending the Thread class. It promotes less coupling. Here's a sample of how this thread is plugged-in to the ScheduledExecutorService.



Implement the ServiceLifeCycle
Implementing ServiceLifeCycle will allow you to put shutdown hooks and implement its destroy() method where you can manipulate how the spawned threads are to be shutdown/killed. The init(Object context) method must also be implemented.



Provide Shutdown Hooks
The shutDownMonitor() method is called and the ExecutorService's awaitTermination() method is invoked which basically gracefully shuts down the spawned thread in the WS Container according to the specified time (might be in seconds, minutes, hours). You can also force to shutdown all threads by calling the method shutdownNow() if it takes longer than the specified time.


Use "application" Scoping
When you use "request" scope as configured in your wsdd file, you are creating one instane of the service per request. If several requests were triggered, your application is also spawning threads per request. The previous request that was processed left a trail of run-away processes. If the scope was "application" it will create only one instance of the service that will serve multiple requests. Hence, when the application runs it does not create run away processes since we already have a shutdown hook to kill all of the spawned threads before the application completely shutdown.


I think it all depends on what an application is trying to achieve, it might have more complex business process than this sample code we have and it requires more classes to use from the concurrent api. But in a multithreaded web service application, you definitely want to use the application scope and implement a shutdown hook.

20 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. desert fox,
    Can you please provide me source code for the multithreaded webservice.
    please send me source at paulkatich@gmail.com

    ReplyDelete
  4. can i have the source code plz...

    ReplyDelete
  5. Hi Paul/Edwi,

    The code I have is proprietary :). I'll have to change it for your reference - the framework of my implementation should help you, but I'll try to post it here this weekend or email it to you.

    Rommel

    ReplyDelete
  6. The code is ready. What's your email address Edwi? There's no easy way for me to post the code here.

    Paul, I've emailed the code to you already. Let me know if you have any questions.

    ReplyDelete
  7. desert fox,

    Good article . please send me source code at shyamu.in@gmail.com

    ReplyDelete
  8. Good article, appreciate your code at aadvani@dmlt.com

    ReplyDelete
  9. desert fox,
    Can you please provide me source code for the multithreaded webservice.At satyadhanbad@gmail.com

    ReplyDelete
  10. desert fox,
    Would appreciate the code at bergerm@isp.state.il.us.
    Thanks

    ReplyDelete
  11. Desert Fox,
    Would appreciate the code at bergerm@isp.state.il.us
    Thanks

    ReplyDelete
  12. When I originally commented I seem to have clicked the -Notify me when new comments are added- checkbox and now every time a comment
    is added I recieve 4 emails with the exact same comment.

    Perhaps there is a way you can remove me from that service?

    Thanks!

    Also visit my webpage ... microfinance.ca

    ReplyDelete
  13. Hi ,
    Can you please provide me the soucecode at ankurmahajan8@gmail.com

    ReplyDelete
  14. Hi Desert Fox,

    It would be very noce if you share me the code at Kunwarjee.pathak@gmail.com

    I am developing 1 project using JAX-WS and implementing threading. So it ll be a lot help to me.

    ReplyDelete
  15. Can you send to me??? Email: tohanh82@gmail.com

    Thank you so much

    ReplyDelete
  16. desert fox,
    Can you please provide me source code for the multithreaded webservice.At tohanh82@gmail.com

    Thank you so much!!!

    ReplyDelete
  17. This blog was... how do you say it? Relevant!! Finally
    I have found something which helped me. Many thanks!


    Here is my blog post; Evgeni Malkin Jersey

    ReplyDelete
  18. could u please help me src code in my account.Its really nice article/...dibyadyuti@ghmail.com

    ReplyDelete