使用 jmsTemplate (Spring JMS) 重复调度消息以传递 ActiveMQ

Repeat scheduling a message for delivery ActiveMQ using jmsTemplate (Spring JMS)

我正在按以下方式向队列发送消息:

我想安排重复我的消息。我的意思是,无论我在控制器(如下所示)中的这一行 jsmClient.send(identifier); 发送什么消息, 我想继续发送,比如说 10 次或 100 次(取决于我设置的计时器)。我的消费者(下面未显示)将继续使用相同的消息,直到我要求它停止。例如,尽管 我的生产者将发送消息 10 次或 100 次,如果我想在第 5 次(如果生产者发送消息 10 次)或第 50 次(如果生产者发送消息 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 class :

@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 class :

@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 Whosebug thread中,activemq包不支持JMS 2.0,所以我应该改用artemis吗?但是,我在上面的 jmsTemplate 方面提出的问题仍然在我的脑海中。请告诉我在这种情况下最好的行动方案是什么。谢谢

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

延迟和计划的消息传递是 ActiveMQ 5.x 的一项功能,而不是 JMS 规范的一部分。 Spring JMS 库 and/or 文档不会提及此功能,因为它是 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 的功能。