Home:ALL Converter>ActiveMQ, discard unconsumed messages

ActiveMQ, discard unconsumed messages

Ask Time:2018-04-10T19:49:14         Author:Beto Neto

Json Formatter

How can I configure my BrokerService to discard unconsumed messages?

I don't want that my client receive all old messages sent by server when it subscribe to server queue.

This is my current broker:

BrokerService ret = new BrokerService();
ret.setPersistent(false);
ret.setUseJmx(false);
ret.addConnector("tcp://0.0.0.0:4444");

Currently, my server starts first and send about 10 messages. After that, my client subscribes to the queue and it receives all that 10 messages sent when there was no clients subscribed. I don't want this behavior.


SOLVED

@Bean
public JmsTemplate jsmTemplate(ConnectionFactory connectionFactory) {
    JmsTemplate ret = new JmsTemplate();
    ret.setConnectionFactory(connectionFactory);
    ret.setMessageConverter(jacksonJmsMessageConverter());

    // Enable the TimeToLive
    ret.setExplicitQosEnabled(true);
    // live time in millis of every sent message, unconsumed messages will be removed from the queue
    ret.setTimeToLive(10000L);
    return ret;
}

Then I send a message like this:

// after 10 seconds this message will be discarded from the queue
jmsTemplate.convertAndSend("messages", "hello from server");

Solution #2

I created a TOPIC instead of a QUEUE.

http://javasampleapproach.com/java-integration/activemq-work-spring-jms-activemq-topic-publisher-subcribers-pattern-using-springboot

Author:Beto Neto,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49752874/activemq-discard-unconsumed-messages
yy