送信者プログラムと受信者プログラムを使用して、RabbitMQ サーバーを理解しようとしています。これで、送信者が単一のメッセージを送信し、受信者が同じメッセージを受信する場合、セットアップ全体がうまく機能します。
ただし、(送信者を 2 回実行して) 2 つのメッセージを送信し、受信者プログラムを 2 回実行すると、最初のメッセージのみが取得されます。
送信者
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
String message = "He12!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println("Sent "+message);
channel.close();
connection.close();
レシーバー
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, consumer);
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
/*channel.basicCancel(consumer.getConsumerTag()); */
String message;
if (delivery != null) {
message = new String(delivery.getBody());
System.out.println("Reciever .."+message);
}
channel.close();
connection.close();