|
@@ -0,0 +1,52 @@
|
|
|
+package com.sf.handle;
|
|
|
+
|
|
|
+import com.rabbitmq.client.AMQP;
|
|
|
+import com.rabbitmq.client.BuiltinExchangeType;
|
|
|
+import com.rabbitmq.client.Channel;
|
|
|
+import com.rabbitmq.client.ReturnListener;
|
|
|
+import com.sf.util.MqUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Scanner;
|
|
|
+
|
|
|
+
|
|
|
+ * 对于消息的持久化而言
|
|
|
+ * 需要队列时持久化的 同时消息时持久化的
|
|
|
+ */
|
|
|
+public class ProducerAE {
|
|
|
+
|
|
|
+ private final static String QUEUE_NAME = "queue_demo4";
|
|
|
+ private final static String EXCHANGE_NAME = "exchange_demo4";
|
|
|
+ private final static String ROUNTING_KEY = "routing_key_demo";
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ Channel channel = MqUtils.getChannel();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ channel.exchangeDeclare("myAe", BuiltinExchangeType.FANOUT,
|
|
|
+ true, false, null);
|
|
|
+ channel.queueDeclare("myQueue", true, false, false, null);
|
|
|
+ channel.queueBind("myQueue", "myAe", "");
|
|
|
+
|
|
|
+ HashMap<String, Object> arguments = new HashMap<>();
|
|
|
+ arguments.put("alternate-exchange", "myAe");
|
|
|
+
|
|
|
+ channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT,
|
|
|
+ true, false, arguments);
|
|
|
+
|
|
|
+
|
|
|
+ channel.queueDeclare(QUEUE_NAME, true, false, false, null);
|
|
|
+ channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUNTING_KEY);
|
|
|
+
|
|
|
+ Scanner scanner = new Scanner(System.in);
|
|
|
+ System.out.println("请输入消息:");
|
|
|
+ while (scanner.hasNext()) {
|
|
|
+ String message = scanner.next();
|
|
|
+ System.out.println(message);
|
|
|
+
|
|
|
+ channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|