RabbitMQ 消费者没有收到消息
RabbitMQ consumer doesn't receive the message
我正在尝试使用 RabbitMQ 消息传递。消息从生产者发送到队列,但消费者没有收到。我检查了服务器,运行 正常。
ProducerSender
//the messageToSend is set in another class.
private static final String TASK_QUEUE_NAME = "hello";
public void writeMessage(Message messageToSend) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
String message = messageToSend.getTitle()+" "+messageToSend.getYear()+" "+messageToSend.getPrice();
channel.basicPublish("", TASK_QUEUE_NAME, null,
message.getBytes());
channel.close();
connection.close();
}
消费者接收者
public void readMessage() throws IOException, TimeoutException {
Socket clientSocket = new Socket(host, port);
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
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"); //message is null
System.out.println(" [x] Received '" + message + "'");
}
};
channel.basicConsume(TASK_QUEUE_NAME, true, consumer);
}
我做错了什么?
这段代码是基于一些例子?因为与 RabbitMQ Java 指南中显示的形式不同。
我用我用的方式发给你,也许你能猜出它缺少什么。
QueueingConsumer.Delivery queueMessage = consumer.nextDelivery();
String message = new String(queueMessage.getBody());
// if auto-ack is not set
channel.basicAck(queueMessage.getEnvelope().getDeliveryTag(), false);
这是基于 https://www.rabbitmq.com/tutorials/tutorial-two-java.html
中的示例
很确定,因为您没有对队列进行绑定。所以,有一个队列。而且您没有指定交易所,因此您将使用默认交易所。但是当它看到带有您的路由密钥的消息时,您并没有告诉交换器将消息发送到哪个队列。
我正在尝试使用 RabbitMQ 消息传递。消息从生产者发送到队列,但消费者没有收到。我检查了服务器,运行 正常。
ProducerSender
//the messageToSend is set in another class.
private static final String TASK_QUEUE_NAME = "hello";
public void writeMessage(Message messageToSend) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
String message = messageToSend.getTitle()+" "+messageToSend.getYear()+" "+messageToSend.getPrice();
channel.basicPublish("", TASK_QUEUE_NAME, null,
message.getBytes());
channel.close();
connection.close();
}
消费者接收者
public void readMessage() throws IOException, TimeoutException {
Socket clientSocket = new Socket(host, port);
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
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"); //message is null
System.out.println(" [x] Received '" + message + "'");
}
};
channel.basicConsume(TASK_QUEUE_NAME, true, consumer);
}
我做错了什么?
这段代码是基于一些例子?因为与 RabbitMQ Java 指南中显示的形式不同。 我用我用的方式发给你,也许你能猜出它缺少什么。
QueueingConsumer.Delivery queueMessage = consumer.nextDelivery();
String message = new String(queueMessage.getBody());
// if auto-ack is not set
channel.basicAck(queueMessage.getEnvelope().getDeliveryTag(), false);
这是基于 https://www.rabbitmq.com/tutorials/tutorial-two-java.html
中的示例很确定,因为您没有对队列进行绑定。所以,有一个队列。而且您没有指定交易所,因此您将使用默认交易所。但是当它看到带有您的路由密钥的消息时,您并没有告诉交换器将消息发送到哪个队列。