在重新启动ActiveMQ持久订阅者被删除时,在这种情况下需要有关自动创建Durable订阅者的帮助

时间:2017-11-04 09:15:40

标签: activemq spring-jms

我在ActiveMQ中为主题创建了一个Durable订阅者,但每当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;
}

1 个答案:

答案 0 :(得分:0)

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

@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,arrival = 0,brokerInTime = 1509806046425,brokerOutTime = 1509806046475,correlationId = null,replyTo = null,persistent = true,type = null,priority = 4,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:JMS消息监听器调用程序的设置失败,目的地' myTopic' - 试图恢复。原因:java.io.EOFException

     

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

     

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

     

2017-11-04 10:34:22.827 ERROR 2889 --- [container-1] o.s.j.l.DefaultMessageListenerContainer:无法刷新目的地的JMS连接' myTopic' - 使用FixedBackOff重试{interval = 5000,currentAttempts = 2,maxAttempts = unlimited}。原因:无法连接到代理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连接