import com.rabbitmq.client.*;
/**
* 为消息设置 TTL
* @author hxstrive.com 2022/2/24
*/
public class TtlDemo1 {
/** 交换器名称 */
private final String EXCHANGE_NAME = "exchange-" + TtlDemo1.class.getSimpleName();
/** 队列名称 */
private final String QUEUE_NAME = "queue-" + TtlDemo1.class.getSimpleName();
public static void main(String[] args) throws Exception {
TtlDemo1 demo = new TtlDemo1();
demo.sender();
}
/**
* 生产者
* @throws Exception
*/
private void sender() throws Exception {
// 创建连接
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
factory.setPort(5672);
Connection connection = factory.newConnection();
// 创建信道
Channel channel = connection.createChannel();
// 声明交换器
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
// 声明队列
channel.queueDeclare(QUEUE_NAME, true, false, true, null) ;
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "*.hxstrive.com");
// 发送消息
System.out.println("[Sender] Send Message...");
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
.deliveryMode(2) // 持久化消息
.expiration("6000") // 设置 TTL=6秒
.build();
channel.basicPublish(EXCHANGE_NAME, "www.hxstrive.com",
properties, "ttl message".getBytes());
System.out.println("[Sender] message = “ttl message”");
// 关闭连接
channel.close();
connection.close();
}
}