在ActiveMQ生产者端设置消息的时间戳。
示例代码:
// 创建ActiveMQ连接工厂 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 创建连接并启动 Connection connection = connectionFactory.createConnection(); connection.start();
// 创建Session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建Queue Queue queue = session.createQueue("myQueue");
// 创建消息Producer MessageProducer messageProducer = session.createProducer(queue);
// 设置消息的时间戳 Long currentTime = System.currentTimeMillis(); messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT); messageProducer.setTimeToLive(86400000); // 设置消息的有效期为24小时 messageProducer.setPriority(5); TextMessage message = session.createTextMessage("Hello, ActiveMQ!"); message.setJMSDeliveryMode(DeliveryMode.PERSISTENT); message.setJMSTimestamp(currentTime); messageProducer.send(message);
// 关闭连接 session.close(); connection.close();