使用RabbitMQ进行消息循环发送,此处发送了0 1 2三条消息,但是却给出的是1 2 2的确认,也并没有打印0号消息投递失败,在接收方接收到的顺序是0 2 1消息。却出现了0号消息,并且顺序不一致。这是怎么回事呀?有哪位好兄弟知道吗?麻烦各位了。
这样子消费消息试一下:
ConnectionFactory factory = new ConnectionFactory();
//cpolar公网地址
factory.setHost("1.tcp.cpolar.cn");
//公网地址对于的端口号
factory.setPort(24889);
//用户名和密码
factory.setUsername("admin");
factory.setPassword("123456");
Connection connection = null;
Channel channel = null;
try {
// 1.创建连接和通道
connection = factory.newConnection();
channel = connection.createChannel();
// 2.为通道声明exchange以及exchange类型
channel.exchangeDeclare("exchange", BuiltinExchangeType.FANOUT);
// 3.创建随机名字的队列
String queueName = channel.queueDeclare().getQueue();
// 4.建立exchange和队列的绑定关系
channel.queueBind(queueName, "exchange", "");
System.out.println(" **** Consumer1 keep alive ,waiting for messages, and then deal them");
// 5.通过回调生成消费者并进行监听
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
com.rabbitmq.client.AMQP.BasicProperties properties, byte[] body) throws IOException {
// 获取消息内容然后处理
String msg = new String(body, "UTF-8");
System.out.println("*********** Consumer1" + " get message :[" + msg + "]");
}
};
// 6.消费消息
channel.basicConsume(queueName, true, consumer);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}