|
@@ -0,0 +1,35 @@
|
|
|
|
|
+package space.anyi.rabbitMQ_learn.ttl;
|
|
|
|
|
+
|
|
|
|
|
+import com.rabbitmq.client.Channel;
|
|
|
|
|
+import com.rabbitmq.client.Connection;
|
|
|
|
|
+import com.rabbitmq.client.ConnectionFactory;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @ProjectName: RabbitMQ-learn
|
|
|
|
|
+ * @FileName: Producer
|
|
|
|
|
+ * @Author: 杨逸
|
|
|
|
|
+ * @Data:2025/10/9 16:40
|
|
|
|
|
+ * @Description:
|
|
|
|
|
+ */
|
|
|
|
|
+public class Producer {
|
|
|
|
|
+ private final static String QUEUE_NAME = "ttl_queue";
|
|
|
|
|
+ public static void main(String[] argv) throws Exception {
|
|
|
|
|
+ ConnectionFactory factory = new ConnectionFactory();
|
|
|
|
|
+ factory.setHost("localhost");
|
|
|
|
|
+ try (Connection connection = factory.newConnection();
|
|
|
|
|
+ Channel channel = connection.createChannel()) {
|
|
|
|
|
+ //信息队列的额外配置参数
|
|
|
|
|
+ Map<String, Object> arguments = new HashMap<String, Object>();
|
|
|
|
|
+ //设置队列中的消息的过期时间为五秒钟
|
|
|
|
|
+ arguments.put("x-message-ttl",5*1000);
|
|
|
|
|
+ //创建信息队列时,通过arguments设置队列的过期时间
|
|
|
|
|
+ channel.queueDeclare(QUEUE_NAME, false, false, false, arguments);
|
|
|
|
|
+ String message = "Hello World!";
|
|
|
|
|
+ channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
|
|
|
|
|
+ System.out.println(" [x] Sent '" + message + "'");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|