我需要帮助,我的应用程序(客户端)连接到RabbitMQ服务器,并且当服务器关闭时,我的应用程序无法启动。...
无法创建监听器,并且应用无法启动。 或者当虚拟主机没有队列时,我的应用也无法启动
所以我的问题
1)如何在配置中处理异常(所有异常,如果RabbitMQ服务器出现问题,我需要启动我的应用程序)
2)我的配置中的内容看起来很糟糕,需要重构
我使用
春季4.2.9。发布
org.springframework.amqp 2.0.5.RELEASE
Java 8
我的2个课程
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) {
}
}
答案 0 :(得分:1)
我找到了解决问题的方法
首先是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并删除其他属性, 因为我不需要 希望对您有所帮助,谢谢大家的关注,对不起我的英语