简体   繁体   English

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

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

I am sending message to the queue in the following manner: 我以以下方式将消息发送到队列:

I want to schedule repeat my message. 我想安排重复我的消息。 I mean, whatever message my this line jsmClient.send(identifier); 我的意思是,无论这行是什么消息, jsmClient.send(identifier); in the controller(shown below) is sending, I want to keep on sending, say for 10 or 100 times (depending upon the timer I set). 在控制器(如下所示)正在发送中,我想继续发送10或100次(具体取决于我设置的计时器)。 My consumer(not shown below) will keep on consuming the same message until I ask it to stop. 我的消费者(以下未显示)将继续使用相同的消息,直到我要求停止它。 For example, even though my producer is going to send the message 10 or 100 times, if I want to stop receiving the message at 5th time(in case of producer sending message 10 times) or 50th time (in case of producer sending message 100 times), I should be able to do that. 例如,即使我的生产者将发送消息10或100次,但如果我想在第5次(生产者发送消息10次的情况下)或第50次(生产者发送消息100的情况下)停止接收消息次),我应该能够做到。

Since I am using JMS 2 and ActiveMQ (version 5.15.8), I am not able to figure out the following: 由于我使用的是JMS 2和ActiveMQ(版本5.15.8),因此无法确定以下内容:

The Delay and Schedule Message Delivery documentation talks about the AMQ_SCHEDULED_REPEAT in the following section: 延迟和计划消息传递文档在以下部分中讨论了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);

If I understood correctly, the above code isn't considering JMS 2 but JMS 1.1? 如果我理解正确,那么上面的代码不是在考虑JMS 2,而是在考虑JMS 1.1? I am wondering what changes I need to make in my code below so that I could do something like this message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat); 我想知道我需要在下面的代码中进行哪些更改,以便可以执行以下操作: message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat); . I couldn't find much useful info regarding schedule repeat in the Spring documentation . Spring文档中找不到有关计划重复的有用信息。

My JmsProducer class : 我的JmsProducer类:

@Component
public class JmsProducer {
    @Autowired
    JmsTemplate jmsTemplate;

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

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

JmsClient Interface: JmsClient接口:

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

}

JmsClientImpl class : JmsClientImpl类:

@Service
public class JmsClientImpl implements JmsClient{


    @Autowired
    JmsProducer jmsProducer;

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


}

In my REST Controller, I am sending a message like this : 在我的REST控制器中,我正在发送如下消息:

try {

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



        }

Based on my Research: 根据我的研究:

In this stackoverflow thread , JMS 2.0 is not supported in the activemq package, so should I switch to artemis instead? 此stackoverflow线程中 ,activemq软件包不支持JMS 2.0,因此我应该改为使用artemis吗? But then, the questions I asked from jmsTemplate side above are still in my mind. 但是,从我上面jmsTemplate方面提出的问题仍在我脑海中。 Please advise what's the best course of action in this situation for me. 请告诉我在这种情况下最好的行动方案。 Thanks 谢谢

The way delayed & scheduled message delivery works in ActiveMQ 5.x is that the producer sets the delay/schedule on the message using special properties and sends the message once . ActiveMQ 5.x中延迟和计划的消息传递的工作方式是,生产者使用特殊属性在消息上设置延迟/计划,并发送一次消息。 Once the broker receives the message it will then deliver the message to the queue based on the delay & schedule set on the message. 代理收到消息后,它将根据消息上设置的延迟和时间表将消息传递到队列中。 Therefore, it is not accurate to say, "...my producer is going to send the message 10 or 100 times..." in the context of delayed & scheduled messages. 因此,在延迟和计划的消息的上下文中说“ ...我的生产者将发送消息10或100次...”是不准确的。

Delayed & scheduled message delivery is a feature of ActiveMQ 5.x and not part of the JMS specification. 延迟和计划的消息传递是ActiveMQ 5.x的功能,而不是JMS规范的一部分。 Spring JMS libraries and/or documentation won't mention this feature since it is unique to ActiveMQ 5.x. Spring JMS库和/或文档不会提及此功能,因为它是ActiveMQ 5.x特有的。

As you note, ActiveMQ 5.x doesn't support JMS 2.0 so if you want JMS 2.0 support you'll need to switch to ActiveMQ Artemis. 正如您所注意到的,ActiveMQ 5.x不支持JMS 2.0,因此,如果您希望JMS 2.0支持,则需要切换到ActiveMQ Artemis。 However, ActiveMQ Artemis doesn't support delayed & scheduled messages as discussed on the user mailing list . 但是,ActiveMQ Artemis不支持用户邮件列表中讨论的延迟和计划的消息。 Therefore, if you want to delayed & scheduled messages you might want to stick with JMS 1.1 or figure out a different way to implement the functionality you're looking for with JMS 2.0 and ActiveMQ Artemis. 因此,如果您想延迟和计划消息,则可能要坚持使用JMS 1.1,或者想出一种不同的方式来实现所需的JMS 2.0和ActiveMQ Artemis功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM