java - 当 ActiveMQ 故障转移传输失败时,Spring Boot 应用程序不会启动

标签 java spring spring-boot activemq spring-jms

我希望我的 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

最佳答案

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

  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();
            }
        }
    }
    

关于java - 当 ActiveMQ 故障转移传输失败时,Spring Boot 应用程序不会启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51707799/

相关文章:

java - 无法在 Intellij Idea 中启动 GlassFish

java - 如何停止 quartz 调度程序中的特定作业

java - 如何解决 spring-boot-devtools 依赖问题?

java - 这些字符串操作在 Java 和 Python 中是否等效?

java - H2 是否支持可序列化隔离级别?

java - 如何更改 CronTrigger 中的 cron 表达式(quartz 2.2,spring 4.1)

java - org.springframework.dao.DataIntegrityViolationException : could not execute statement; SQL [n/a]; constraint [admin_pkey]; 错误

spring-boot - 读取数据时违反约束

java - Spring-Boot 1.4.0 使用随机端口示例

java - WebTestClient 变异过滤器 java.lang.IllegalStateException : No "WebTestClient-Request-Id" header