Reputation: 4112
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.
Upvotes: 1
Views: 797
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