【问题标题】:On restart of ActiveMQ durable subscriber gets deleted, Need help on auto creation of Durable subscriber in this case在 ActiveMQ 持久订阅者重新启动时被删除,在这种情况下需要自动创建持久订阅者的帮助
【发布时间】:2017-11-04 23:53:40
【问题描述】:

我在 ActiveMQ 中为主题创建了一个持久订阅者,但是每当 ActiveMQ 服务器重新启动时,我必须重新启动我的 java 服务以使持久订阅者可用。有什么方法可以在 activemq 重新启动时自动使订阅者可用??

下面是我正在使用的示例代码。

@Bean
public ConnectionFactory connectionFactory() throws JMSException{
    ConnectionFactory factory= new ActiveMQConnectionFactory(brokerURL);
    Connection conn = factory.createConnection();
    conn.setClientID(ClientId);

    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic(exampleTopic);
    MessageConsumer messageConsumer = session.createDurableSubscriber(topic, 
    subscriberName);
    messageConsumer.setMessageListener(consumerMessageListner);
    conn.start();

    return factory;
}

【问题讨论】:

    标签: activemq spring-jms


    【解决方案1】:

    由于您使用的是 Spring,您应该使用 Spring 的内置 JMS 支持(在spring-jms jar 中)而不是编写自己的 JMS 代码。见the documentation。 DefaultMessageListenerContainer 将继续尝试为您重新连接...

    @Bean
    public DefaultMessageListenerContainer container(ConnectionFactory connectionFactory,
            MessageListener messageListener) {
        DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setDestinationName("myTopic");
        container.setPubSubDomain(true);
        container.setSubscriptionDurable(true);
        container.setSubscriptionName("mySubscriber");
        container.setClientId("myClientId");
        container.setSessionTransacted(true);
        container.setMessageListener(messageListener);
        return container;
    }
    
    @Bean
    public MessageListener listener() {
        return System.out::println;
    }
    
    @Bean
    public ApplicationRunner runner(JmsTemplate template) {
        return args -> {
            while (true) {
                template.convertAndSend("foo");
                System.in.read();
            }
        };
    }
    

    还有……

    ActiveMQTextMessage {commandId = 5, responseRequired = true, messageId = ID:gollum.local-64266-1509806046256-1:2:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:gollum.local -64266-1509806046256-1:2:1:1,destination = topic://myTopic,transactionId = null,expiration = 0,timestamp = 1509806046424,到达 = 0,brokerInTime = 1509806046425,brokerOutTime = 1509806046475,correlationId = null,replyTo = null,persistent = true,type = null,priority = 4,groupID = null,groupSequence = 0,targetConsumerId = null,compressed = false,userID = null,content = org.apache.activemq.util.ByteSequence@34691ad3,marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = foo}

    2017-11-04 10:34:12.813 WARN 2889 --- [container-1] o.s.j.l.DefaultMessageListenerContainer:目标“myTopic”的 JMS 消息侦听器调用程序设置失败 - 尝试恢复。原因:java.io.EOFException

    2017-11-04 10:34:12.816 错误 2889 --- [ container-1] o.s.j.l.DefaultMessageListenerContainer:无法刷新目标“myTopic”的 JMS 连接 - 使用 FixedBackOff{interval=5000,currentAttempts=0,maxAttempts 重试=无限}。原因:无法连接到代理 URL:tcp://10.0.0.3:61616。原因:java.net.ConnectException:连接被拒绝(连接被拒绝)

    2017-11-04 10:34:17.823 错误 2889 --- [ container-1] o.s.j.l.DefaultMessageListenerContainer:无法刷新目标“myTopic”的 JMS 连接 - 使用 FixedBackOff{interval=5000, currentAttempts=1, maxAttempts 重试=无限}。原因:无法连接到代理 URL:tcp://10.0.0.3:61616。原因:java.net.ConnectException:连接被拒绝(连接被拒绝)

    2017-11-04 10:34:22.827 错误 2889 --- [ container-1] o.s.j.l.DefaultMessageListenerContainer:无法刷新目标“myTopic”的 JMS 连接 - 使用 FixedBackOff{interval=5000, currentAttempts=2, maxAttempts 重试=无限}。原因:无法连接到代理 URL:tcp://10.0.0.3:61616。原因:java.net.ConnectException:连接被拒绝(连接被拒绝)

    2017-11-04 10:34:27.897 INFO 2889 --- [ container-1] o.s.j.l.DefaultMessageListenerContainer :成功刷新 JMS 连接

    【讨论】:

      猜你喜欢
      • 2017-01-24
      • 1970-01-01
      • 2014-06-14
      • 2017-02-20
      • 2014-10-12
      • 2017-10-31
      • 2012-07-04
      • 2016-01-14
      • 1970-01-01
      相关资源
      最近更新 更多