Create durable topic and subscriber spring boot jms with ActiveMQ

不羁岁月 提交于 2019-12-06 01:22:07

As the previous answers pointed out, it was necessary to set the client id and the durable subscription on the factory :

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
    DefaultJmsListenerContainerFactory factory =
            new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory());
    factory.setDestinationResolver(destinationResolver());
    factory.setConcurrency("3-10");
    factory.setClientID("brokerClientId");
    factory.setSubscriptionDurable(true);
    return factory;
}

but that alone did not register the client as a durable subscriber, that is because the JMSListener needed the containerFactory specified, otherwise it would just take the defaults :

@JmsListener(
destination = "venta.topic",
id = "comercial",
subscription = "venta.topic",
//this was also needed with the same name as the bean above
containerFactory = "jmsListenerContainerFactory" 
)
public void receiveMessage(Venta venta) {
            logger.log(Level.INFO, "RECEIVED : {0}",venta);      
            repository.save(venta);
}

It's worth mentioning that, this post was the one that made me realize my mistake.

I Hope this will help someone else

The DefaultJmsListenerContainerFactory should have unique clientId and durable sub. true set like below code :

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
    DefaultJmsListenerContainerFactory factory =
            new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory());
    factory.setDestinationResolver(destinationResolver());
    factory.setConcurrency("3-10");
    factory.setClientID("brokerClientId");
    factory.setSubscriptionDurable(true);
    return factory;
}

Hard to tell for sure, but a common cause to this problem is to forget configuring a unique clientId on the connectionFactory bean. It has to be unique and is the way the broker can tell each client apart.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!