|
@@ -0,0 +1,44 @@
|
|
|
+package com.sf.message.publish;
|
|
|
+
|
|
|
+import com.rabbitmq.client.AMQP;
|
|
|
+import com.rabbitmq.client.Channel;
|
|
|
+import com.sf.util.RabbitMqUtils;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Scanner;
|
|
|
+
|
|
|
+public class Producer {
|
|
|
+
|
|
|
+ private static String queueName = "hello1";
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception{
|
|
|
+ Channel channel = RabbitMqUtils.getChannel();
|
|
|
+ channel.queueDeclare(queueName, false, false, false, null);
|
|
|
+
|
|
|
+ System.out.println("输入消息:");
|
|
|
+ Scanner scanner = new Scanner(System.in);
|
|
|
+ while (scanner.hasNext()){
|
|
|
+ String message = scanner.next();
|
|
|
+ // 发送一个持久化的消息
|
|
|
+// AMQP.BasicProperties basicProperties = new AMQP.BasicProperties.Builder()
|
|
|
+// .contentType("text/plain").deliveryMode(2).priority(1).build();
|
|
|
+// // void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body)
|
|
|
+// channel.basicPublish("", queueName, basicProperties, message.getBytes());
|
|
|
+
|
|
|
+ // 发送一个带有header的消息
|
|
|
+// Map<String,Object> headers = new HashMap<>();
|
|
|
+// headers.put("location","here");
|
|
|
+// headers.put("time",System.currentTimeMillis());
|
|
|
+// AMQP.BasicProperties basicProperties = new AMQP.BasicProperties.Builder().headers(headers).build();
|
|
|
+// channel.basicPublish("",queueName,basicProperties,message.getBytes());
|
|
|
+
|
|
|
+ // 发送一个带有过期时间的消息
|
|
|
+ AMQP.BasicProperties basicProperties = new AMQP.BasicProperties.Builder()
|
|
|
+ .expiration("10000").build();
|
|
|
+ channel.basicPublish("", queueName, basicProperties, message.getBytes());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|