Beto Neto
Beto Neto

Reputation: 4112

ActiveMQ, discard unconsumed messages

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

Upvotes: 1

Views: 797

Answers (1)

Gary Russell
Gary Russell

Reputation: 174739

Use a topic (pub/sub) instead of a queue.

By default, subscribers to topics only get messages that are sent while the subscription is active.

Upvotes: 1

Related Questions