2017-12-27 322 views
-1

我想在'x'秒後在任何時間發送一個't'消息,接收器會收到這個消息。這樣做在spring啓動時在activeMQ中延遲傳送消息

,我已經寫了接收器瞬間。沒有任何延遲接收的發送者代碼

@Autowired 
private JmsTemplate jmsTemplate; 
private Queue queue = new ActiveMQQueue("topicName"); 

public void show(String message) { 
    try { 
     System.out.println("Sending message " + message); 
     jmsTemplate.convertAndSend(queue, message, new MessagePostProcessor() { 
      @Override 
      public Message postProcessMessage(Message message) throws JMSException { 
       System.out.println("postProcessMessage executed "); 
       message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 3 * 60 * 1000); 
       System.out.println("long time " + message 
         .getLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY)); 
       return message; 
      } 
     }); 
     System.out.println("Sending done " + message + " at " + System.currentTimeMillis()); 
    } catch (Exception er) { 
     er.printStackTrace(); 
    } 
} 

和Reciever代碼

@JmsListener(destination = "topicName") 
    public void reciever(String message) { 
     System.out.println("receiving message " + message + " at " + System.currentTimeMillis()); 
    } 

但是消息。

輸出是

發送消息,這是一個消息
postProcessMessage執行長 時間180000
接收到消息,這是一個消息在1514391984964
發送已完成,這是一個消息在1514391984970

配置文件是

@Bean 
    JmsTemplate createJMSTemplate(ConnectionFactory connectionFactory) { 
     JmsTemplate jmsTemplate = new JmsTemplate(); 
     jmsTemplate.setConnectionFactory(connectionFactory); 
     return jmsTemplate; 
    } 

    @Bean 
    ConnectionFactory myActiveMQConnectionFactory() { 
     RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy(); 
     redeliveryPolicy.setBackOffMultiplier(1); 
     redeliveryPolicy.setUseExponentialBackOff(false); 
     ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); 
     connectionFactory.setRedeliveryPolicy(redeliveryPolicy); 
     NetworkConnector networkConnector = new DiscoveryNetworkConnector(); 
     networkConnector.setConsumerTTL(2); 

     return connectionFactory; 
    } 
+0

加入schedulerSupport是否已打開在代理調度的支持? –

回答

0

使用默認配置的activemq不支持延遲消息,您應該首先打開它。

在activemq.conf

<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" schedulerSupport="true">