使用jmsTemplate(Spring JMS)重复计划要传递ActiveMQ的消息

时间:2019-02-25 16:30:21

标签: java jms activemq spring-jms activemq-artemis

我以以下方式将消息发送到队列:

我想安排重复我的消息。我的意思是,无论控制器中此行jsmClient.send(identifier);(如下所示)正在发送什么消息, 我想继续发送10或100次(具体取决于我设置的计时器)。我的消费者(以下未显示)将继续使用相同的消息,直到我要求停止它。例如,即使 如果我想在第5次(生产者发送消息10次的情况下)或第50次(生产者发送消息100次的情况下)停止接收消息,我的生产者将发送消息10或100次, 我应该能够做到。

由于我使用的是JMS 2和ActiveMQ(版本5.15.8),因此无法确定以下内容:

Delay and Schedule Message Delivery文档在以下部分中讨论了AMQ_SCHEDULED_REPEAT

MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("test msg");
long delay = 30 * 1000;
long period = 10 * 1000;
int repeat = 9;
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, period);
message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat);
producer.send(message);

如果我理解正确,那么上面的代码不是在考虑JMS 2,而是在考虑JMS 1.1?我想知道我需要在下面的代码中进行哪些更改,以便可以执行类似message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat);的操作。在the Spring documentation中找不到有关计划重复的有用信息。

我的JmsProducer类:

@Component
public class JmsProducer {
    @Autowired
    JmsTemplate jmsTemplate;

    @Value("${jms.queue.destination}")
    String destinationQueue;

    public void send(String msg){
        jmsTemplate.convertAndSend(destinationQueue, msg);
    }
}

JmsClient界面:

public interface JmsClient {
    public void send(String msg);

}

JmsClientImpl类:

@Service
public class JmsClientImpl implements JmsClient{


    @Autowired
    JmsProducer jmsProducer;

    @Override
    public void send(String msg) {
        jmsProducer.send(msg);
    }


}

在我的REST控制器中,我正在发送如下消息:

try {

            DataRetrieverDao dataRetrieverDao = (DataRetrieverDao) context.getBean("dataRetrieverDao");
            String identifier=dataRetrieverDao.sendDownloadInfo(user_id);
            logger.info("VALUE OF STRING: "+identifier);
            jsmClient.send(identifier);



        }

基于我的研究:

this stackoverflow thread中,activemq软件包不支持JMS 2.0,因此我应该改用artemis吗?但是,从我上面jmsTemplate方面提出的问题仍在我脑海中。请告诉我在这种情况下最好的行动方案。谢谢

1 个答案:

答案 0 :(得分:0)

ActiveMQ 5.x中延迟和计划的消息传递的工作方式是,生产者使用特殊属性在消息上设置延迟/计划,并一次发送消息。代理收到消息后,它将根据消息上设置的延迟和时间表将消息传递到队列中。因此,在延迟和计划的消息中说“ ...我的生产者将发送消息10或100次...”是不正确的。

延迟和计划的消息传递是ActiveMQ 5.x的功能,而不是JMS规范的一部分。 Spring JMS库和/或文档不会提及此功能,因为它是ActiveMQ 5.x特有的。

您已经注意到,ActiveMQ 5.x不支持JMS 2.0,因此,如果要获得JMS 2.0支持,则需要切换到ActiveMQ Artemis。但是,ActiveMQ Artemis不支持延迟和计划的消息as discussed on the user mailing list。因此,如果您要延迟和计划消息,则可能要坚持使用JMS 1.1,或者想出其他方法来实现您正在使用JMS 2.0和ActiveMQ Artemis寻找的功能。