使用ActiveMQ创建持久的主题和订户spring boot jms [英] Create durable topic and subscriber spring boot jms with ActiveMQ

查看:180
本文介绍了使用ActiveMQ创建持久的主题和订户spring boot jms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为ActiveMQ创建一个主题和一个长期订阅者,我的问题是我不知道在哪里指定。我可以创建主题并使用消息,但是当我关闭订阅服务器然后继续发送消息并再次打开订阅服务器时,它将无法读取它们。



这是我到目前为止所拥有的:



发送消息:

  JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); 
jmsTemplate.setPubSubDomain(true);
jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT);
jmsTemplate.setDeliveryPersistent(true);
jmsTemplate.convertAndSend( venta.topic,venta);

接收消息:

  @JmsListener(destination = venta.topic,id = comercial,subscription = venta.topic)
public void receiveMessage(Venta venta){
记录器。 log(Level.INFO, RECEIVED:{0},venta);
repository.save(venta);
}

我已阅读本文,并且我了解我需要创建持久订阅者。



我还阅读了春季文档



我认为这与 DefaultJmsListenerContainerFactory (我没有实现,我使用的是默认配置),文档显示:

  @Bean 
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(){
DefaultJmsListenerContainerFactory factory =
new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setDestinationResolver(destinationResolver());
factory.setConcurrency( 3-10);
退货工厂;
}

但是我似乎找不到在哪里创建持久会话。生产者和订户都已连接到独立的activemq二进制文件。



我希望您能为我提供帮助,谢谢。

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

但仅此一项并没有将客户端注册为持久订户,这是因为 JMSListener 需要指定的 containerFactory ,否则将只采用默认值:

  @JmsListener(
destination = venta.topic,
id = comercial,
subscription = venta.topic ,
//也需要与
上方的bean同名的容器containerFactory = jmsListenerContainerFactory

public void receiveMessage(Venta venta){
logger.log(Level.INFO, RECEIVED:{0},venta);
repository.save(venta);
}

值得一提的是,这篇文章是让我意识到自己的错误的那篇文章。



我希望这会帮助别人


I need to create a topic and a durable subscriber for ActiveMQ, my problem is that I don't know where to specify that. I am able to create the topic and consume the messages, but when I turn off the subscriber then keep sending messages and turn on the subscriber again, it won't read them.

This is what I have so far:

Sending the message :

    JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
    jmsTemplate.setPubSubDomain(true);
    jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT);
    jmsTemplate.setDeliveryPersistent(true);
    jmsTemplate.convertAndSend("venta.topic",venta);

Receiving the message :

@JmsListener(destination = "venta.topic",id = "comercial",subscription = "venta.topic")
public void receiveMessage(Venta venta) {
    logger.log(Level.INFO, "RECEIVED : {0}",venta);      
    repository.save(venta);
}

I have read this article and I understand that I need to create the durable subscriber.

I've also read the spring docs

And I think it has something to do with the DefaultJmsListenerContainerFactory (which I didn't implement, I am using the default configuration), the docs shows:

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

But I can't seem to find where to create the durable session. Both my producer and my subscriber are connected to a standalone activemq binary.

I hope you can help me , thanks in advance.

解决方案

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

这篇关于使用ActiveMQ创建持久的主题和订户spring boot jms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆