停止ActiveMQ后如何捕获java.net.SocketException?

时间:2019-03-05 14:51:42

标签: java apache queue activemq

我有一个消费者Java应用程序,该应用程序连接到Apache ActiveMQ服务器。工作正常但是,如果我停止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);

1 个答案:

答案 0 :(得分:0)

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

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