Reputation: 31
When my request comes on my controller, executorService create a threadpool and create some threads to perform some logic, my request goes back to browser. My problem start from here, threads which are running on server side needs session object, threads gets session as null. what approach i use to set session value after response goes back to clientside.Below is the code, please give some suitable solution, so that I can get session value after request is completed.
@RequestMapping(value = "/expandAllController", method = RequestMethod.POST) public @ResponseBody void expandAllController( @RequestParam final String[] controllerList,final HttpServletRequest request) {
int listSize=controllerList.length;
BlockingQueue controllerDataQueue=cacheUtility.initBlockingQueue(listSize);
ExecutorService executor=Executors.newCachedThreadPool();
for(String controllerID:controllerList){
executor.submit(new producer(controllerID,request,controllerDataQueue));
}
executor.shutdown();
try {
executor.awaitTermination(10, TimeUnit.MINUTES);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// data fetching Thread
}
private class producer implements Runnable{
Object responseObj=null;
String controllerID;
HttpServletRequest request;
BlockingQueue controllerDataQueue;
producer(String controllerID ,HttpServletRequest request, BlockingQueue controllerDataQueue ){
this.controllerDataQueue=controllerDataQueue;
this.controllerID=controllerID;
this.request=request;
}
@Override
public void run() {
int ThreadCount=0;
final HttpSession session = request.getSession();
final CommonSettings commonSettings = CommonSettings
.getInstance(session);
// for(String controllerID:controllerList){
controllerData.setControllerId(controllerID);
responseObj=expandController(controllerData,session);
if(responseObj!=null){
try {
controllerDataQueue.put(responseObj);
ThreadCount++;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Upvotes: 0
Views: 377
Reputation: 691765
Store an empty container (a synchronized set or list, for example) in the session directly from the controller method, then pass this container to each of the tasks, and make the tasks add their response to the container.
Also, consider reusing always the same thread pool. It wastes resources to always start a new one. And if too many requests come in, you'll have a much too large number of thread running in parallel, which is precisely what a thread pool is used to avoid.
Upvotes: 0