异常重连
public void start() {
try {
connection = factory.newConnection();
connection.addShutdownListener(this);
LOGGER.info("Connected to " + factory.getHost() + ":" + factory.getPort());
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, "Failed to connect to " + factory.getHost() + ":" + factory.getPort(), e);
asyncWaitAndReconnect();
}
}
shutdown
@Override
public void shutdownCompleted(final ShutdownSignalException cause) {
// reconnect only on unexpected errors
if (!cause.isInitiatedByApplication()) {
LOGGER.log(Level.SEVERE, "Lost connection to " + factory.getHost() + ":" + factory.getPort(),
cause);
connection = null;
asyncWaitAndReconnect();
}
}
定时start
protected void asyncWaitAndReconnect() {
executor.schedule(new Runnable() {
@Override
public void run() {
start();
}
}, 15, TimeUnit.SECONDS);
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。