拒绝执行触发时,Java服务停止运行

时间:2015-11-08 04:08:30

标签: java mysql mqtt bonecp

Java服务在拒绝执行触发时停止运行

我有一个正在运行的mqtt-client,它充当Android应用程序的桥接器/服务器和MySQL的数据库池,当被拒绝的执行触发我的服务停止时,我真的想知道为什么。

这是我的线程池

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

广告这是我的消息到达的方式

@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 ");

    }

}

有些runnables中有mqtt客户端,因此他们可以发布响应。 这些服务的作用是运行runnables,当异常触发时,我将它们附加到挂起的runnables,以便我以后可以运行它们。

我在这里缺少什么吗?

0 个答案:

没有答案