Could this RabbitMQ code cause a memory leak
I have the code segment below. If the RabbitMQ server drops off the connection seems to return null and a new connection is established. I am suspicious that repeated attempts to reconnect to the server is eating up memory, but I am not sure why. How should a lost connection (server unavailable) be handled?
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;
}
The code is part of a messaging system that redirects in bound messages to an outgoing AMQP queue. Each time an in bound message is received (at a rate of 20 to 30 per second) the getConnection method is called. If the AMQP server is down, the method is still called and an attempt is made to build a queue. It fails throwing an exception seemingly eating more memory with each failure.
链接地址: http://www.djcxy.com/p/34174.html