I am trying to migrate a legacy application to current jee 7 standards.
During the application deployment/bootstrap, we initialize some caches through a startup servlet. Few caches are inturn used by other application components during the deployment process. These are initialized sequentially in the legacy application. I am trying to introduce multithreading to initialize all caches in parallel. So I am using ManagedExecutorService and submitting the tasks. After the tasks are submitted, the deployment is continuing for a while and stopping indefinitely. Below is my code
for(Cacheable cacheable: cacheableApps.values()) {
mes.submit(new Runnable(){
public void run() {
cacheable.initialize();
}
});
}
I also tried using ManagedExecutorService.execute(Runnable) and also ManagedExecutorService.submit(Callable) with no luck.
Additionally, I tried calling ManagedExecutorService.invokeAll(Collection) and Future.get() but the server hung indefinitely at this point.
Then I replaced ManagedExecutorService with ManagedThreadFactory and the threads started initializing and the deployment completed with NPEs in other application components which uses the caches. This is because the caches are initializing at the end of the deployment.
The idea is to replace startup servlet with a startup singleton ejb, initialize my caches in multiple threads and halt the deployment process untill all threads return so that no NPEs are thrown and the deployment is finished smoothly.
While using ManagedThreadFactory, I tried halting the deployment like below
for(Cacheable cacheable: cacheableApps.values()) {
RunnableTask task =new RunnableTask(cacheable);
mtf.newThread(task).start();
tasks.add(task);
}
int count = 0;
while(count!=tasks.size()) {
count = 0;
for(Task task: tasks) {
if(task.isDone()) {
count++;
}
}
}
public class RunnableTask implements Runnable {
private boolean done;
public(Cacheable cacheable) {
this.cacheable = cacheable;
}
public void run() {
cacheable.initialize();
done = true;
}
public boolean isDone() {
return done;
}
}
But this code also hung up the server indefinitely. Because the threads are not started yet while the execution is in the while loop. The threads are only starting at the end of the deployment. So the above code is invalid.
To debug the problem, I created a dummy restful web service and added the below code
Future<String> future = mes.submit(new Callable<String>() {
public String call() throws Exception {
Cacheable.STUDENT.getApp().initialize();
return "successful";
}
});
return future.get();
This initialized the cache and the response of the web service is "successful".
Then I copied the exact code to my startup servlet and the deployment again hung while executing future.get()
I was wondering if there is some problem with the cache code and removed the call and simply returned "successful". Again the deployment hanged while calling future.get()