【问题标题】:Spring boot application does not start when ActiveMQ failover transport fails当 ActiveMQ 故障转移传输失败时 Spring Boot 应用程序不启动
【发布时间】:2019-01-13 10:09:43
【问题描述】:

我希望我的 Spring Boot 应用程序能够启动,无论它是否可以连接到 JMS。我有这个最小的例子:

@SpringBootApplication
@EnableJms
public class JmsActivemqFailoverApplication {

    public static void main(String[] args) {
        SpringApplication.run(JmsActivemqFailoverApplication.class, args);
    }

    @Component
    public static class JmsReceiver {
        @JmsListener(destination = "inbox")
        public void receive(Message message) {
            System.out.println("Received <" + message + ">");
        }
    }

    @RestController
    public static class HelloWorldController {
        @GetMapping("/")
        public String helloWorld() {
            return "Hello world";
        }
    }
}

application.properties 包含:

spring.activemq.broker-url=tcp://non-existing-broker:61616

我可以从helloWorld 端点获得响应。当我将属性更改为:

spring.activemq.broker-url=failover:(tcp://non-existing-broker:61616)

应用程序不断尝试连接到代理,我无法从我的 REST 端点获得响应。

请指教,如何在不等待 ActiveMQ 故障转移传输成功的情况下运行应用程序。

https://github.com/madoxas/jms-activemq-failover 上提供的示例代码

【问题讨论】:

  • 我刚试过你的代码。应用程序启动并且rest端点是可达的,
  • @pvpkiran 请再试一次。在日志中我收到警告:Failed to connect to [tcp://non-existing-broker:61616] after: 10 attempt(s) continuing to retry. Executing curl http://localhost:8080/ 我收到 Connection refused
  • @pvpkiran 看起来您没有使用故障转移传输。请确保您在代理网址前有failover:(...。我希望它可以在没有它的情况下使用故障转移传输:)

标签: java spring spring-boot activemq spring-jms


【解决方案1】:

实现此目的的一种方法是:

  1. 使用属性spring.jms.listener.auto-startup=false 禁用自动 JMS 容器启动
  2. 在应用程序启动后启动 JMS 容器:

    @Component
    public class JmsStarter implements ApplicationRunner {
        private final JmsListenerEndpointRegistry jmsRegistry;
    
        public JmsStarter(JmsListenerEndpointRegistry jmsRegistry) {
            this.jmsRegistry = jmsRegistry;
        }
    
        @Override
        public void run(ApplicationArguments args) {
            for (MessageListenerContainer listenerContainer : jmsRegistry.getListenerContainers()) {
                listenerContainer.start();
            }
        }
    }
    

【讨论】:

  • 是的。你是对的。我错过了failover,我正要提出相同的解决方案。对于数据源,springboot 中有一个属性,例如spring.datasource.continue-on-error=true。但是jms没有这样的东西。你可以在springboot中创建一个bug
猜你喜欢
  • 2014-04-27
  • 2017-05-12
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-13
  • 2021-01-14
相关资源
最近更新 更多