Spring RabbitMQ AMQP Connection Factory-如何覆盖连接属性

时间:2019-03-17 14:56:16

标签: spring-boot spring-amqp

我正在尝试使用SimpleMessageListenerContainer和默认连接工厂,我不希望使用spring deault属性spring.rabbitmq。*使用属性,我想在运行时注入Connection Factory时设置连接属性。但是我的容器尝试连接到本地主机,任何帮助都非常有用 我的代码示例就是这样

@Bean
public SimpleMessageListenerContainer queueListenerContainer(AbstractConnectionFactory connectionFactory,
        MessageListenerAdapter listenerAdapter) { 
    connectionFactory.setHost(Arrays.toString(rabbitMqConfig.getSubscriberHosts()));
    connectionFactory.setVirtualHost("hydra.services");
    connectionFactory.setPort(rabbitMqConfig.getSubscriberPort());
    connectionFactory.setUsername(rabbitMqConfig.getSubscriberUsername());
    connectionFactory.setPassword(rabbitMqConfig.getSubscriberPassword());
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setQueueNames(rabbitMqConfig.getSubscriberQueueName());
    container.setConnectionFactory(connectionFactory);

   // container.setQueueNames("SampleQueue"); /*This just for testing.. !*/
    container.setMessageListener(listenerAdapter);
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    container.setDeclarationRetries(5);// This is default to 3, We can twick this and move this to prop
    container.setPrefetchCount(100); //Tell the broker how many messages to send to each consumer in a single request.
    return container;
}

但是该容器仍然尝试连接到本地主机的代码。 日志:

[30m2019-03-17 09:35:43,335 [0; 39m [34mINFO [0; 39m [[34mmain [0; 39m] [33morg.springframework.amqp.rabbit.connection.AbstractConnectionFactory [0; 39m:尝试连接到:[localhost:5672] [30m2019-03-17 09:35:45,499 [0; 39m [34mINFO [0; 39m [[34mmain [0; 39m]] [33morg.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer [0; 39m:经纪人不可用;启动期间无法强制执行队列声明 [30m2019-03-17 09:35:45,778 [0; 39m [34mINFO [0; 39m [[34mqueueListenerContainer-1 [0; 39m]] [33morg.springframework.amqp.rabbit.connection.AbstractConnectionFactory [0; 39m:尝试连接到:[localhost:5672] [30m2019-03-17 09:35:48,365 [0; 39m [34mINFO [0; 39m [[34mmain [0; 39m] [33morg.springframework.boot.StartupInfoLogger [0; 39m:在162.706秒内启动了DftpEppScrubberApplication 164.364)

编辑2

这样,我会收到unknowhost错误。我本来以为是防火墙问题,但我检查了连接,一切似乎都正确。我不确定这里是什么问题!

0 个答案:

没有答案