2

We have a SOAP webservice deployed on Weblogic server with the flow as follows.

SOAP Client will send message to Webservice which validates the message and sends an Acknowledgement as response of the service. However, it is suppose to do a SocketConnection in the background to further process the message as asynchronously.

The implementation that has been provided is to spawn a ChildThread in the background using ExecutorService.newFixedThreadPool. this is the code being used (not all the code for the webservice implementation file is posted here).

  if ("103".equals(requestType)) {
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    executorService.submit(new MessageProcessing103(coreMsg, otherDetails));
  } else if ("104".requestType) {
         ExecutorService executorService = Executors.newFixedThreadPool(1);
         executorService.submit(new MessageProcessing104(coreMsg, otherDetails));
    }

With the above implementation Scalability is definitely an issue but we can live with it as of now. Can you help with the below queries though

  1. Since background thread is long living does that mean the same spawned thread will be used to execute all the requests?
  2. If two requests come in at an interval of within few seconds, will the second request be in waiting state or new thread will be spawned?
  3. We are not shutting down the executor so what would be the issues encountered in such a case?
Saurav
  • 118
  • 9

2 Answers2

1
  1. For each request, you create a new thread which executes only that request.
  2. Same.
  3. Very soon you encounter an OutOfMemoryError - created and not shutted executors eat all the memory.

My advice is not to create new executor for each request, but reuse the same executor. And let that executor contain several threads. not one.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • Thanks for the inputs. Based on the third point above I am planning to have a final ExecuteService object with a fixed thread pool as follows. 'private static final ExecutorService EXECUTESERVICE = Executors.newFixedThreadPool(3); ' Will this be good enough. Also I read the Executor threads do not shutdown on server stop? How do I terminate those threads programmatically? This code is in my web service implementation file. – Saurav Oct 21 '15 at 16:11
  • to shouldown executor threads on server stop, make them daemon threads, as in http://stackoverflow.com/questions/1516172/executor-and-daemon-in-java/1517264#1517264 – Alexei Kaigorodov Oct 21 '15 at 18:16
0

This is the approach I devised for the above problem.

Can someone please validate and confirm if this can still cause any Out of Memory or threading issues.

Firstly, create a ThreadPool to serve average parallel threads.

public static final ExecutorService executorService = Executors.newFixedThreadPool(3);

Next, invoke the respective MessageProcessor Instances for the message type 103 or 104

  if ("103".equals(requestType)) {
       executorService.submit(new MessageProcessing103(coreMsg, otherDetails));
  } else if ("104".requestType) {
       executorService.submit(new MessageProcessing104(coreMsg, otherDetails));
    }

Finally, do the shutdown of the executorService if the Webservice Context destroys. For this will configure a listener in web.xml.

 <listener>
<listener-class>com.saurav.listener.AppContextListener</listener-class></listener>

Code inside AppContextListener --> contextDestroyed() function

ImplClass.executorService.shutdown();

In case of any exception will handle the same and do a shutdownNow()

Saurav
  • 118
  • 9