【发布时间】:2019-07-20 14:09:38
【问题描述】:
这是一个非常人为的 rabbitmq 应用程序示例,它在一个主要方法中既是消息的生产者又是消息的消费者。问题是覆盖的 handleDelivery 方法中的代码永远不会被执行。我使用 Rabbitmq 仪表板,看到队列已满并消耗。并且 handleConsumeOk 中的行被打印出来。 由于我是 rabbitmq 的新手,我想知道我是否做错了一些根本性的错误,或者我只是认为“当收到此消费者的 basic.deliver 时调用”的想法是错误的。
public class RabbitMain {
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
Connection connection = Utils.getConnection("test");
String payload = "hello world!";
try (Channel channel = connection.createChannel()){
channel.exchangeDeclare("sampleExchange", BuiltinExchangeType.TOPIC, true);
channel.basicPublish("sampleExchange", "testKey", null, payload.getBytes());
}
System.out.println("Consume...");
try (Channel channel = connection.createChannel()){
channel.exchangeDeclare("sampleExchange", BuiltinExchangeType.TOPIC, true);
channel.queueDeclare("testQueue", true, false, false, null);
channel.queueBind("testQueue", "sampleExchange", "testKey");
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);
System.out.println("Received: " + message);
}
@Override
public void handleConsumeOk(String consumerTag) {
System.out.println("handled consume ok");
}
};
Thread.sleep(2000);
channel.basicConsume("testQueue", true, consumer);
}
}
}
【问题讨论】:
-
消费发生在另一个线程上。当我使用 IntelliJ idea 调试器运行代码时,代码可以正常工作。通过向主线程添加一些空闲时间(例如 Thread.sleep(1000) 消费线程有时间完成它的工作。我不确定,但似乎消费者线程是一个守护线程并在主线程终止后被杀死。