title: Spring JMS(ActiveMQ) delayed delivery of messages
body: <p>We're trying to set a delay on some JMS messages, so that a message will only be added to the queue/ received by the listener after x time. So far we've tried 2 approaches that didn't work.</p>
  
  <p>1) According to the spring documentation, we can set the delivery delay on the JMSTemplate. This is the sample code we tried:</p>
  
  <pre><code>@Autowired
  private JmsTemplate jmsTemplate;
  
  ...
  long deliveryDelay = ...;
  this.jmsTemplate.setDeliveryDelay(deliveryDelay);
  this.jmsTemplate.convertAndSend(
                      queue.getName(),
                      event);
  ...
  </code></pre>
  
  <p>However, we get the following exception, even though our spring jms version is 4.0.5:</p>
  
  <pre><code>java.lang.IllegalStateException: setDeliveryDelay requires JMS 2.0
  </code></pre>
  
  <p>2) We also tried setting the delay on the message itself, but it looks like the delay was ignored, and the message was delivered immediately anyway.</p>
  
  <pre><code>@Component
  public class MyMessageConverter implements MessageConverter {
  
  ...
  
  @Override
  public Message toMessage(Object eventObject, Session session) throws JMSException, MessageConversionException {
  
  ...
  long deliveryDelay = ...;
  objectMessage.setLongProperty(
                    ScheduledMessage.AMQ_SCHEDULED_DELAY,
                    deliveryDelay);
  return objectMessage;
  }
  }
  </code></pre>
  
  <p>The jmsTemplate definition in the spring xml:</p>
  
  <pre><code>&lt;bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"&gt;
      &lt;property name="connectionFactory" ref="cachingConnectionFactory" /&gt;
      &lt;property name="messageConverter" ref="myMessageConverter" /&gt;
      &lt;property name="sessionTransacted" value="true" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Does anyone has any suggestions on what the problems are / other ideas on how to achieve delayed messaging?
  Thanks!</p>