消息过期

允许消息过期 。默认情况下,消息永不会过期。如果消息在特定周期内失去意义,那么可以设置过期时间。 有两种方法设置消息的过期时间,时间单位为毫秒:

  1. 使用消息生产者的setTimeToLive 方法为所有的消息设置过期时间。
  2. 使用消息生产者的send 方法为每一条消息设置过期时间。 消息过期时间,send 方法中的 timeToLive 值加上发送时刻的 GMT 时间值。如果 timeToLive 值等于零,则 JMSExpiration 被设为零, 表示该消息永不过期。
  3. 消息服务器接收到消息后,在指定的时间后,会从队列中移除指定的消息,超时被移除的消息不会发送给消费者。
  4. 使用消息生产者的setTimeToLive(long time ) 方法来给所有的消息设置过期时间:
// 消息生产者  
MessageProducer producer = null;  
producer = session.createProducer(queue);  
// 消息是否为持久性的,如果不设置默认是持久化的。  
producer.setDeliveryMode(DeliveryMode.PERSISTENT);  
 //消息过期设置  
producer.setTimeToLive(1000);
  1. 使用消息生产者的send()方法来设置消息的过期时间:
//message发送的消息,deliveryMode是否持久化,priority优先级,timeToLive 消息过期时间  
//producer.send(message, deliveryMode, priority, timeToLive);  
producer.send(message, DeliveryMode.PERSISTENT, 4, 1000);

results matching ""

    No results matching ""