38 lines
1.2 KiB
Java
38 lines
1.2 KiB
Java
package me.ehlxr.rabbitmq.simple;
|
|
|
|
import com.rabbitmq.client.Channel;
|
|
import com.rabbitmq.client.Connection;
|
|
import com.rabbitmq.client.QueueingConsumer;
|
|
import me.ehlxr.rabbitmq.ConnectionUtil;
|
|
|
|
import java.io.IOException;
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
/**
|
|
* 简单模式:一个生产者,一个消费者
|
|
*
|
|
* @author lixiangrong
|
|
* @since 2019-01-22.
|
|
*/
|
|
public class Receiver {
|
|
private final static String QUEUE_NAME = "simple_queue";
|
|
|
|
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
|
|
//获取连接
|
|
Connection connection = ConnectionUtil.getConnection();
|
|
//获取通道
|
|
Channel channel = connection.createChannel();
|
|
|
|
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
|
QueueingConsumer consumer = new QueueingConsumer(channel);
|
|
channel.basicConsume(QUEUE_NAME, true, consumer);
|
|
|
|
while (true) {
|
|
//该方法会阻塞
|
|
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
|
|
String message = new String(delivery.getBody());
|
|
System.out.println("[x] Received '" + message + "'");
|
|
|
|
}
|
|
}
|
|
} |