目录
概述
- 队列声明 ->生产者中 -> queueDeclare
- 消息持久化 : 在发布时候声明 -> 生产者 -> basicPublish
代码实现
import com.aa.Utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class Task03 {
public static final String TASK_QUEUE_NAME = "queueName";
public static void main(String[] args) throws Exception {
Channel channel = RabbitMqUtils.getChannel();
boolean durable = true;
channel.queueDeclare(TASK_QUEUE_NAME, durable, false, false, null);
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String message = scanner.next();
channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes(StandardCharsets.UTF_8));
System.out.println("生产者发出信息 " + message);
}
}
}