在spring启动时在activeMQ中延迟传送消息
我想在'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;
}
回答:
使用默认配置的activemq不支持延迟消息,您应该首先打开它。
在activemq.conf
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" schedulerSupport="true">
以上是 在spring启动时在activeMQ中延迟传送消息 的全部内容, 来源链接: utcz.com/qa/266383.html