ActiveMQ: how to delete old messages?

I am learning how to use ActiveMQ, and now we are faced with the following problem.

Suppose I have a topic called topic.test on ActiveMQ that has two subscribers. At the moment, I have only one of those subscribers waiting for a message, and the manufacturer sends a message on the topic mentioned above.

Well, the connected subscriber receives the message, but should the other subscriber receive this message later when he is connected? Well, in my case this does not happen: my subscribers only receive messages when they connect. All other messages that were sent before they were connected are not received by them. What can i do wrong?

Here are some of the source code that I wrote for testing ActiveMQ. You may find that what's wrong with him.

My consummer code:

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection connection = connectionFactory.createConnection(); connection.setClientID("leitorTeste"); conexao.start(); Session sessao = conexao.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic fonte = sessao.createTopic("topic.test"); MessageConsumer consumer = sessao.createConsumer(fonte); javax.jms.Message presente = null; while ((presente = consumer.receive()) != null) { System.out.println(((TextMessage) presente).getText()); } consumer.setMessageListener(new LeitorMensagens()); conexao.close(); 

And here is my manufacturer code:

 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection connection = connectionFactory.createConnection(); Session sessao = conexao.createSession(true, Session.AUTO_ACKNOWLEDGE); connection.start(); Destination destino = sessao.createTopic("topic.test"); MessageProducer produtorMensagem = sessao.createProducer(destino); produtorMensagem.setDeliveryMode(DeliveryMode.PERSISTENT); TextMessage message = sessao.createTextMessage("Hi!"); produtorMensagem.send(message); sessao.commit(); connection.close(); 

Is there any other configuration I need to add to ActiveMQ so that my users can receive old messages?

+4
source share
2 answers

You must make your customers “permanent.” Otherwise, AMQ "forgets" about them as soon as they unsubscribe. To do this, use Session.createDurableSubscriber()

+6
source

There is something called a retroactive consumer that you can also install with a broker. This applies to topic subscribers - who are not durable, but may wish to receive the “latest” messages that they might have missed, see Also, Subscription Restore Policy

+1
source

All Articles