RabbitMQ的失败重连

2022-07-21 12:57:51

异常重连

    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);
    }
  • 作者:weixin_34268843
  • 原文链接:https://blog.csdn.net/weixin_34268843/article/details/89169917
    更新时间:2022-07-21 12:57:51