0

I have a problem with my java service , The problem arises when our database cpu utilization goes up. Now I wonder why wouldn't my java-service return to normal after the database(mysql) has recovered. Im using database pooling using bonecp 0.7.1 RELEASE

This is my CachedThreadPool i used:

final int MAX_CORES = Runtime.getRuntime().availableProcessors();
protected ExecutorService threadPool = new ThreadPoolExecutor(MAX_CORES,MAX_CORES ,
        60L, TimeUnit.SECONDS,
        new SynchronousQueue<Runnable>(),
        new DiscardPolicy());

I have a discard policy that when something happened like the threadpool wont accommodate new task. Ill put it in my pending concurrent hashmap for later work.

public class DiscardPolicy extends ThreadPoolExecutor.DiscardPolicy{

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        // TODO Auto-generated method stub
        super.rejectedExecution(r, e);
        Constants.pendingRunnables.put(e, r);

    }

}

And this is my MqttServer to acquire task

@Override
    public void messageArrived(final String topic, final MqttMessage message) throws Exception {

        try{
            if(Constants.pendingRunnables.size() > 0){
                for(java.util.Map.Entry<ThreadPoolExecutor, Runnable> runner : Constants.pendingRunnables.entrySet()){
                    Constants.pendingRunnables.remove(runner.getKey());
                    runner.getKey().execute(runner.getValue());

                }
            }
        }catch(Exception e){

        }
    threadPool.execute(new Runnable(){
        // any kind of task to run depending on what topic
    });
}

Is there something I'm missing here?

david
  • 2,900
  • 5
  • 28
  • 48
  • Generally I will set time outs. e.g. http://stackoverflow.com/questions/33348869/java-process-did-not-quit-after-future-task-completion – Ravindra babu Nov 03 '15 at 15:32
  • The problem we had recently was that when `Callable` submitted to executor service throws any exception/error, you will not know about it unless you resolve the `Future` on the main thread (and we were not doing this because our tasks were writes to the database). If this is your case I would recommend catching `Throwable` and logging it. In case of any errors (e.g. out of memory error), you will at least have a chance of seeing them in the logs, rather then application behaving in an undefined way. – Jaroslaw Pawlak Nov 03 '15 at 15:34
  • @JaroslawPawlak threadpool executes writes and reads from the database, and some runnables have published too. but I still dont get why my service or this threadpool doesnt return to normal after our database recovered. I did read something about runnables catching RuntimeException but is there any easy way? like all of the pending task will be executed when there is enough threads? and it should come back alive when mysql is recovered? – david Nov 03 '15 at 15:44
  • @david I don't know what is causing the problems and this is what you need to find out. Try logging I mentioned above to make sure there are no weird errors that could affect entire JVM. If it doesn't help, deploy a server running with profiler and kill the database - see what is being executed in your java server. You can also do the same trick with deploying a server locally in debug mode - send a new request to the server and walk through the code, see where it blocks or throws exception. – Jaroslaw Pawlak Nov 03 '15 at 16:13
  • @JaroslawPawlak thats what in my mind now, doing it locally just to be safe. but anyway thanks – david Nov 03 '15 at 16:20

0 Answers0