我有一个生产者如下:
public class MyProducer {
private static final String EXCHANGE_NAME = "messages";
public static void main(String[] argv)
throws java.io.IOException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
String color1 = "red"
String message1 = "message1"
String color2 = "blue"
String message2 = "message2"
channel.basicPublish(EXCHANGE_NAME, color1, null, message1);
channel.basicPublish(EXCHANGE_NAME, color2, null, message2);
channel.close();
connection.close();
}
}
也是一个消费者:
public class MyConsumer {
private static final String EXCHANGE_NAME = "messages";
public static void main(String[] argv)
throws java.io.IOException,
java.lang.InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "color1");
channel.queueBind(queueName, EXCHANGE_NAME, "color2");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, consumer);
}
}
我现在的问题是:
- 我现在只有一个名为“queuName”的队列,还是有两个名为“color1”和“color2”的队列?
- 我不想立即使用这些消息。所以我想要的是为每个队列“color1”和“color2”设置一个延迟。我怎样才能做到这一点?