【发布时间】:2016-12-13 16:14:09
【问题描述】:
当我通过 rabbitmq 管理界面强制断开客户端连接时,它不会重新连接。我正在使用客户端 4.0.0。请在下面找到我的代码。
public void processQueue(){
// Establishing a Connection with RabbitMQ server,
// running in the local machine, localhost
ConnectionFactory factory = new ConnectionFactory();
Connection connection=null;
try {
factory.setHost("localhost");
factory.setRequestedHeartbeat(5);
factory.setAutomaticRecoveryEnabled(true);
factory.setConnectionTimeout(5000);
factory.setNetworkRecoveryInterval(10000);
connection = factory.newConnection();
// creating a channel with first_queue
Channel channel = connection.createChannel();
channel.queueDeclare("xyz", true, false, false, null);
// creating the Consumer, that will be receive a message and convert to String
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println("Java Queue - Message Received '" + message + "'");
}
};
// loop that waits for message
channel.basicConsume("xyz", true, consumer);
} catch (Exception e) {
System.out.println("server is Down !");
System.out.println(e.getMessage());
try {
if(connection!=null)
connection.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
我有一个无法重新连接的简单消费者程序。我关注了这篇文章,但它也无济于事RabbitMQ Java client auto reconnect。谁能帮我解决这个问题?
【问题讨论】: