gpt4 book ai didi

java - RabbitMQ 连接失败

转载 作者:行者123 更新时间:2023-12-02 10:55:42 24 4
gpt4 key购买 nike

我需要帮助,我的应用程序(客户端)连接到 RabbitMQ 服务器,当服务器关闭时,我的应用程序无法启动......

无法创建监听器并且应用程序启动失败。或者当虚拟主机没有队列时我的应用程序也无法启动

所以我的问题

1) 如何处理我的配置中的异常(所有异常,如果 RabbitMQ 服务器出现问题,我需要启动我的应用程序)

2) 我的配置中的内容看起来很糟糕并且需要重构

我用

  • Spring 4.2.9.RELEASE

  • org.springframework.amqp 2.0.5.RELEASE

  • Java 8

我的两个类

1) Beans RabbitMq 配置

2)监听器注解

    @EnableRabbit
@Configuration
public class RabbitMQConfig {
@Bean
public ConnectionFactory connectionFactory() {
com.rabbitmq.client.ConnectionFactory factoryRabbit = new com.rabbitmq.client.ConnectionFactory();
factoryRabbit.setNetworkRecoveryInterval(10000);
factoryRabbit.setAutomaticRecoveryEnabled(true);

CachingConnectionFactory connectionFactory =
new CachingConnectionFactory(factoryRabbit);

connectionFactory.setHost("DRIVER_APP_IP");
connectionFactory.setPort(5672);
connectionFactory.setConnectionTimeout(5000);
connectionFactory.setRequestedHeartBeat(10);
connectionFactory.setUsername("user");
connectionFactory.setPassword("pass");
connectionFactory.setVirtualHost("/vhost");

return connectionFactory;
}

@Bean
public RabbitTemplate rabbitTemplate() {
try {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
rabbitTemplate.setRoutingKey(this.DRIVER_QUEUE);
rabbitTemplate.setQueue(this.DRIVER_QUEUE);
return rabbitTemplate;
} catch (Exception ex){
return new RabbitTemplate();
}
}

@Bean
public Queue queue() {
return new Queue(this.DRIVER_QUEUE);
}


@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setConcurrentConsumers(3);
factory.setMaxConcurrentConsumers(10);
return factory;
}
}

@Component
public class RabbitMqListener {

@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = DRIVER_QUEUE, durable = "true"),
exchange = @Exchange(value = "exchange", ignoreDeclarationExceptions = "true", autoDelete = "true"))
)
public String balancer(byte[] message) throws InterruptedException {
String json = null;
try {
"something move"
} catch (Exception ex) {
}
}

最佳答案

我找到了解决我的问题

首先它是 Bean 容器!我们需要这个

factory.setMissingQueuesFatal(false);

此属性使我们的队列在服务器 RabbitMQ 上丢失时,我们的应用程序不会崩溃并且可以启动

@Bean

public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setMissingQueuesFatal(false);
factory.setConcurrentConsumers(3);
factory.setStartConsumerMinInterval(3000L);
factory.setMaxConcurrentConsumers(10);

factory.setRecoveryInterval(15000L);
factory.setStartConsumerMinInterval(1000L);
factory.setReceiveTimeout(10000L);
factory.setChannelTransacted(true);
return factory;
}

第二个

@Component
public class RabbitMqListener {

@RabbitListener(containerFactory = "rabbitListenerContainerFactory", queues = DRIVER_QUEUE)
public String balancer(byte[] message) throws InterruptedException {
String json = null;
try {
"something move"
} catch (Exception ex) {
}
}

我在@RabbitListener中设置了containerFactory和Queue并删除了其他属性,因为我不需要它我希望它对某人有帮助,感谢大家的关注,抱歉我的英语

关于java - RabbitMQ 连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51770597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com