首页 > 解决方案 > Spring rabbitmq amqp connection Factory - How override connection properties

问题描述

I am trying to use SimpleMessageListenerContainer and Default connection factory, I don't want use properties using spring deault properties spring.rabbitmq.*, I would like to set connection properties at run-time when connection Factory injected. But my container try to connect to localhost, any help is much appriciated My code example is like this

@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;
}

But still the code this container try to connect to local host. Logs :

[30m2019-03-17 09:35:43,335[0;39m [34mINFO [0;39m [[34mmain[0;39m] [33morg.springframework.amqp.rabbit.connection.AbstractConnectionFactory[0;39m: Attempting to connect to: [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: Broker not available; cannot force queue declarations during start [30m2019-03-17 09:35:45,778[0;39m [34mINFO [0;39m [[34mqueueListenerContainer-1[0;39m] [33morg.springframework.amqp.rabbit.connection.AbstractConnectionFactory[0;39m: Attempting to connect to: [localhost:5672] [30m2019-03-17 09:35:48,365[0;39m [34mINFO [0;39m [[34mmain[0;39m] [33morg.springframework.boot.StartupInfoLogger[0;39m: Started DftpEppScrubberApplication in 162.706 seconds (JVM running for 164.364)

Edit 2

This way I am getting unknowhost error. I thought initially it was firewall Issue but I checked connection everything seems to be right. I am not sure what is Problem Here!

标签: spring-bootspring-amqp

解决方案


推荐阅读