【问题标题】:How can I catch java.net.SocketException when ActiveMQ is stopped?ActiveMQ 停止时如何捕获 java.net.SocketException?
【发布时间】:2019-07-27 01:56:41
【问题描述】:

我有一个连接到 Apache ActiveMQ 服务器的消费者 Java 应用程序。它正在工作。但是,如果我停止 Apache ActiveMQ 服务器,消费者应用程序会出现异常:

java.net.SocketException: Connection reset

我该如何处理这个异常,例如我想发送一封警报电子邮件给支持。如何检测离线 ActiveMQ 服务器。

Here is the exception details

// Getting JMS connection from the server
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(this.activeMqClient.getUsername(), this.activeMqClient.getPassword(), this.activeMqClient.getProducerUri());
activeMQConnectionFactory.setUseAsyncSend(false);
activeMQConnectionFactory.setMaxThreadPoolSize(1);

this.activeMqClient.setClientId(activeMqClient.getClientType() + "_" + Thread.currentThread().getName());

if (this.activeMqClient.getClientId() != null) {
    activeMQConnectionFactory.setClientID(this.activeMqClient.getClientId());
}

Connection connection = activeMQConnectionFactory.createConnection();

if (this.activeMqClient.getClientId() != null) {
    connection.setClientID(this.activeMqClient.getClientId());
}

connection.start();

connection.setExceptionListener(this);

// Creating session for receiving messages
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

// Getting the queue
Destination destination = session.createQueue(this.activeMqClient.getDestinationQueueName() + "?consumer.exclusive=true");

// MessageConsumer is used for receiving (consuming) messages          
MessageConsumer consumer = session.createConsumer(destination);

//Setting message listener
consumer.setMessageListener(this);

【问题讨论】:

  • 你能给我们一个你的代码示例吗
  • 是的,我用连接构建代码编辑了这个问题
  • 这个异常是否被抛出到您的客户端代码中?如果是这样,为什么你不能抓住它然后做任何你需要做的事情(例如发送警报电子邮件给支持)?

标签: java apache queue activemq


【解决方案1】:

您可以像捕获任何其他异常一样捕获它:

try {
  //... client code ...//
}
catch(SocketException e) {
  //... send email ...//
}

【讨论】:

    猜你喜欢
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    相关资源
    最近更新 更多