0

Java Service Stops Running when Rejected Execution triggers

I have a running mqtt-client that acts as a bridge/server for Android apps and database pooling for MySQL and when rejected execution triggers my service stops, I'm really wondering why.

This is my threadpool

public static ExecutorService threadPool = new ThreadPoolExecutor(10, // core size
    30, // max size
    10*60, // idle timeout
    TimeUnit.SECONDS,
    new ArrayBlockingQueue<Runnable>(20),
    factory,
    new  DiscardPolicy()); // queue with a size

Ad this is how my message arrived performs

@Override
public void messageArrived(final String topic, final MqttMessage message) throws Exception {
    threadPool.execute(new Runnable(){
        void run(){
            if(topic.equalsIgnoreCase("something")){} // and code goes on like these
        }
    // code
     });



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

//                  runner.getKey().execute(runner.getValue());
                    try{
                        if (!runner.getKey().isShutdown()) {
                            runner.getValue().run();
                            Constants.pendingRunnables.remove(runner.getKey());
                        }
                    }catch(Exception ex){

                    }

                }
            }
        }catch(Exception e){}

}

public class DiscardPolicy extends ThreadPoolExecutor.DiscardPolicy{
    private static final Logger LOGGER = Logger.getLogger(DiscardPolicy.class);
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        // TODO Auto-generated method stub
        super.rejectedExecution(r, e);
        Constants.pendingRunnables.put(e, r);
        LOGGER.info("Rejected Runnable ");

    }

}

Some runnables have mqtt client in them so that they could publish a response. What these service do is run runnables and when exception triggers I append them to pending runnables so that I could run them later.

Do I have something missing here?

david
  • 2,900
  • 5
  • 28
  • 48
  • can you check that ExecutorService is shoutdown? CallerRunsPoilicy is A handler for rejected tasks that runs the rejected task directly in the calling thread of the execute method, unless the executor has been shut down, in which case the task is discarded. – Ravindra babu Nov 08 '15 at 05:48
  • yes I have checked it and catch exception, But the problem still exists, I replaced it with DiscardPolicy and run them later. – david Nov 08 '15 at 07:07
  • My service is running now, but im waiting for rejected exception occurs. Do I have any other way of testing rejected exception? – david Nov 08 '15 at 07:08
  • And I have put Runtime Exception catch in my REHandler for runnables so that i could catch RuntimeException too in Runnables – david Nov 08 '15 at 07:09
  • @ravindra see my edit – david Nov 08 '15 at 07:13
  • I have implemented as per Holger answer and I got RejectionHandler as per : http://stackoverflow.com/questions/30782611/unable-to-get-callablethread-in-rejectionhandler – Ravindra babu Nov 08 '15 at 07:14
  • @ravindra the implementation by holger is about the instance of callables, while my problem is all about when rejection execution triggers my service stops, seems like a deadlock or something i miss. Is there something im probably doing wrong with my code? – david Nov 17 '15 at 05:13

0 Answers0