0
votes

In my web app, I submit a runable instance to the ScheduledExecutorService. In the runable instance, I receive message from IBM MQ by use receiveNoWait mehod and the session is in CLIENT_ACKNOWLEDGE mode. When I invoke the shutdownNow method of ScheduledExecutorService, the receive method sometimes loses a message without invoking acknowledge method. Is there some suggestion for me?
Here is the code to receive a message.

try {
when the ScheduledExecutorService shutdownNow ,I can only see the first loginfo without any exception.

logger.info("获取消息");

Message msg = consumer.receiveNoWait();

logger.info("获取消息成功");

return msg;

} catch (Throwable e) {

logger.error("处理数据的时候出现异常", e);

return null;

}

1

1 Answers

0
votes

shutdownNow() method tries to interrupt running tasks. If receiveNoWait method had time to finish you get message, otherwise not. If you need running tasks to be finished you can use awaitTermination method of ExecutorService interface or methods of Future object(isDone(), get() ...) that is returned by submit method.