【发布时间】:2020-02-22 01:54:25
【问题描述】:
我有一个应用程序,它有一个主线程和一个 JMS 线程,它们通过 ActiveMQ 5.15.11 相互通信。我可以很好地发送消息,但是我想要一种发回状态或错误的方法。我注意到MessageListener 允许onSuccess() 和onException(ex) 作为两个要监听的事件,但是我发现只有onSuccess() 被调用。
这是我的代码的 sn-ps。
JMS 线程:
ConnectionFactory factory = super.getConnectionFactory();
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(super.getQueue());
MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(m -> {
try {
super.processRmbnConfigMsg(m);
} catch (JMSException | IOException e) {
LOG.error(e.getMessage(), e);
// I can only use RuntimeException.
// Also this exception is what I am expecting to get passed to the onException(..)
// call in the main thread.
throw new RuntimeException(e);
}
});
connection.start();
主线程(向 JMS 发送消息):
sendMessage(xml, new AsyncCallback() {
@Override
public void onException(JMSException e) {
// I am expecting this to be that RuntimeException from the JMS thread.
LOG.error("Error", e);
doSomethingWithException(e);
}
@Override
public void onSuccess() {
LOG.info("Success");
}
});
我期待的是,new RuntimeException(e) 中抛出的异常将在某种程度上被 onException(JMSException e) 事件侦听器拾取,即使 RuntimeException 已包装。
相反,我总是收到onSuccess() 事件。我想onException(..) 事件发生在通信问题期间,但我想要一种方法将异常发送回调用者。
如何实现在 JMS 线程中收集错误并将其发送回调用线程的目标?
【问题讨论】: