这个RabbitMQ代码是否会导致内存泄漏

我有下面的代码段。 如果RabbitMQ服务器断开连接似乎返回null并建立新的连接。 我怀疑重复尝试重新连接到服务器正在耗尽内存,但我不知道为什么。 如何处理丢失的连接(服务器不可用)?

public Connection getConnection( ) {
    if (myConnection == null) {
        start();
    }
    return myConnection;
}

private void start() {
    log.debug("Building AMQP Connection");

    ConnectionFactory factory = new ConnectionFactory();
    String ipAddress = applicationConfiguration.getAMQPHost();
    String user = applicationConfiguration.getAMQPUser();
    String password = applicationConfiguration.getAMQPPassword();
    String virtualHost = applicationConfiguration.getAMQPVirtualHost();
    String port = applicationConfiguration.getAMQPPort();

    try {
        factory.setUsername(user);
        factory.setPassword(password);
        factory.setVirtualHost(virtualHost);
        factory.setPort(Integer.parseInt(port));
        factory.setHost(ipAddress);
        myConnection = factory.newConnection();
    }
    catch (Exception e) {
        log.error("Unable to initialise AMQP Connection.");
        e.printStackTrace();
    }

    myConnection.addBlockedListener(new BlockedListener() {
        public void handleBlocked(String reason) throws IOException {
            // Connection is now blocked
            log.warn("Message Server has blocked.  It may be resource limitted.");
            blocked = true;
        }

        public void handleUnblocked() throws IOException {
            // Connection is now unblocked
            log.warn("Message server is unblocked.");
            blocked = false;
        }
    });
}

public Boolean isBlocked() {
    return blocked;
}

该代码是消息系统的一部分,该消息系统将绑定消息重定向到传出的AMQP队列。 每次接收到一个绑定消息时(以每秒20到30的速率),调用getConnection方法。 如果AMQP服务器关闭,则仍然调用该方法,并尝试构建队列。 它无法抛出一个似乎每次失败都会吃掉更多记忆的异常。

链接地址: http://www.djcxy.com/p/34173.html

上一篇: Could this RabbitMQ code cause a memory leak

下一篇: ETIMEDOUT problems with node.js & amqp