clintonmagro / embedded-qpid-spring-boot-starter

Apache License 2.0
6 stars 2 forks source link

Custom port not picked up by Spring #2

Open dschulten opened 5 years ago

dschulten commented 5 years ago

I added the starter jar to the spring guide example git clone https://github.com/spring-guides/gs-messaging-rabbitmq.git and applied the property to redefine the port:

@SpringBootTest
@TestPropertySource(properties = {"embedded.qpid.port=5673"})
public class MessagingRabbitmqApplicationTest {

  @MockBean private Runner runner;

  @Autowired private RabbitTemplate rabbitTemplate;

  @Autowired private Receiver receiver;

  @Test
  public void test() throws Exception {
    rabbitTemplate.convertAndSend(MessagingRabbitmqApplication.queueName, "Hello from RabbitMQ!");
    receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
  }
}

The log shows that the embedded qpid comes up at the custom port, but Spring still tries to connect on port 5672.

2019-10-24 08:35:42.494  INFO 6232 --- [  Broker-Config] qpid.message.broker.listening            : [Broker] BRK-1002 : Starting : Listening on TCP port 5673
2019-10-24 08:35:42.534  INFO 6232 --- [-default-Config] q.message.messagestore.recovery_start    : [Broker] [vh(/default)/ms(MemoryMessageStore)] MST-1004 : Recovery Start
2019-10-24 08:35:42.541  INFO 6232 --- [-default-Config] q.message.transactionlog.recovery_start  : [Broker] [vh(/default)/ms(MemoryMessageStore)] TXN-1004 : Recovery Start
2019-10-24 08:35:42.544  INFO 6232 --- [-default-Config] q.m.transactionlog.recovery_complete     : [Broker] [vh(/default)/ms(MemoryMessageStore)] TXN-1006 : Recovery Complete
2019-10-24 08:35:42.545  INFO 6232 --- [-default-Config] qpid.message.messagestore.recovered      : [Broker] [vh(/default)/ms(MemoryMessageStore)] MST-1005 : Recovered 0 messages
2019-10-24 08:35:42.546  INFO 6232 --- [-default-Config] q.m.messagestore.recovery_complete       : [Broker] [vh(/default)/ms(MemoryMessageStore)] MST-1006 : Recovery Complete
[Broker] BRK-1004 : Qpid Broker Ready
2019-10-24 08:35:42.552  INFO 6232 --- [  Broker-Config] qpid.message.broker.ready                : [Broker] BRK-1004 : Qpid Broker Ready
2019-10-24 08:35:42.553  INFO 6232 --- [           main] n.c.embedded.qpid.broker.EmbeddedBroker  : Started Embedded Qpid broker
2019-10-24 08:35:43.036  INFO 6232 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
2019-10-24 08:35:45.047  INFO 6232 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Broker not available; cannot force queue declarations during start: java.net.ConnectException: Connection refused: connect
2019-10-24 08:35:45.055  INFO 6232 --- [    container-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
2019-10-24 08:35:47.071 ERROR 6232 --- [    container-1] o.s.a.r.l.SimpleMessageListenerContainer : Failed to check/redeclare auto-delete queue(s).

org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
dschulten commented 5 years ago

Adding a custom CachingConnectionFactory in the test was the missing link. If that is indeed the best way to do it, I can write a PR for the readme. Or is there a better way?

@SpringBootTest
@TestPropertySource(properties = {"embedded.qpid.port=0"})
public class MessagingRabbitmqApplicationTest {

  @TestConfiguration
  public static class Conf {

    @Bean
    CachingConnectionFactory cachingConnectionFactory(EmbeddedBroker embeddedBroker) {
      return new CachingConnectionFactory(embeddedBroker.getPort());
    }
  }

  @MockBean private Runner runner;

  @Autowired private RabbitTemplate rabbitTemplate;

  @Autowired private Receiver receiver;

  @Test
  public void test() throws Exception {
    rabbitTemplate.convertAndSend(MessagingRabbitmqApplication.queueName, "Hello from RabbitMQ!");
    receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
  }
}