|
@@ -0,0 +1,57 @@
|
|
|
|
+package com.sf.helloworld;
|
|
|
|
+
|
|
|
|
+import com.rabbitmq.client.*;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+
|
|
|
|
+public class Consumer {
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
|
+ ConnectionFactory factory = new ConnectionFactory();
|
|
|
|
+ factory.setHost("127.0.0.1");
|
|
|
|
+ factory.setPort(5672);
|
|
|
|
+ factory.setUsername("guest");
|
|
|
|
+ factory.setPassword("guest");
|
|
|
|
+ Connection connection = factory.newConnection();
|
|
|
|
+
|
|
|
|
+ Channel channel = connection.createChannel();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ channel.queueDeclare("hello1", false, false,
|
|
|
|
+ false, null);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ DeliverCallback deliverCallback = (String consumerTag, Delivery message) -> {
|
|
|
|
+
|
|
|
|
+ String body = new String(message.getBody());
|
|
|
|
+
|
|
|
|
+ long deliveryTag = message.getEnvelope().getDeliveryTag();
|
|
|
|
+ System.out.println(deliveryTag);
|
|
|
|
+ System.out.println(body);
|
|
|
|
+
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ CancelCallback cancelCallback = consumerTag -> {
|
|
|
|
+ System.out.println(consumerTag);
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ channel.basicConsume("hello1", true, deliverCallback, cancelCallback);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|