Skip to content

[Enhancement]: Publishing message to Rabbitmq test-container Queue and consuming it #7452

Closed

Description

@miPlodder

Module

RabbitMQ

Proposal

  • Currently, looks like we don't support publishing message to Rabbitmq test-container Queue and consuming it.
  • Also, when this functionality is completed, we should have proper documentation around it.

Activity

  1. changed the title [-][Enhancement]: Publishing message to Rabbitmq Queue and consuming it[/-] [+][Enhancement]: Publishing message to Rabbitmq test-container Queue and consuming it[/+] on Aug 31, 2023
  2. eddumelendez commented on Aug 31, 2023

    @eddumelendez
    Member

    Currently, looks like we don't support publishing message to Rabbitmq test-container Queue and consuming it.

    Can you elaborate on this, please? Are you talking about utility methods to use rabbimq command inside the container?

  3. miPlodder commented on Aug 31, 2023

    @miPlodder
    Author

    Can you elaborate on this, please? Are you talking about utility methods to use rabbimq command inside the container?

    I'm trying to write integration test for my Spring Application which uses Rabbitmq as Message Broker. In order to do that I have made below mentioned changes,

    1. Adding Rabbitmq container details to the ConnectionFactory,
    @Bean
      public CachingConnectionFactory connectionFactory() throws Exception {
    
        RabbitMQContainer rabbit =
            new RabbitMQContainer(DockerImageName.parse("rabbitmq:3.7.25-management-alpine"));
        rabbit.start();
    
        final RabbitConnectionFactoryBean rabbitConnectionFactoryBean =
            new RabbitConnectionFactoryBean();
        rabbitConnectionFactoryBean.setHost(rabbit.getHost());
        rabbitConnectionFactoryBean.setPort(rabbit.getAmqpPort());
        rabbitConnectionFactoryBean.setUsername(rabbit.getAdminUsername());
        rabbitConnectionFactoryBean.setPassword(rabbit.getAdminPassword());
        rabbitConnectionFactoryBean.setUseSSL(Boolean.parseBoolean(userSsl));
        rabbitConnectionFactoryBean.setSslAlgorithm(sslVersion);
        rabbitConnectionFactoryBean.setEnableHostnameVerification(false);
        rabbitConnectionFactoryBean.afterPropertiesSet();
    
        final CachingConnectionFactory connectionFactory =
            new CachingConnectionFactory(rabbitConnectionFactoryBean.getObject());
    
        return connectionFactory;
    

    Similarly created RabbitTemplate, SimpleRabbitListenerContainerFactory and using above mentioned connection factory

      @Bean
      public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() throws Exception {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setMaxConcurrentConsumers(1);
           return factory;
      }
    
     @Bean
      public RabbitTemplate amqpTemplate() throws Exception {
        return new RabbitTemplate(connectionFactory());
      }
    
    @Bean
      public Queue abc() {
        return new Queue("abc", false);
      }
    
    
    1. Next I'm creating Exchange and Routing Key binding to Queues,
     @Bean
      public Declarables httpBinding() {
        val queue = QueueBuilder.durable("queue").build();
        val declarables = new Declarables(exchange, queue,
            bind(queue).to("exchange")
                .with("routingKey"); 
        return declarables;
      }
    
    1. Now, I send message to the Rabbitmq
    rabbitTemplate.convertAndSend(exchange, routingKey, "data");
    

    But I don't see my Rabbitmq listener listening to this message here,

     @RabbitListener(id = "1",
          queues = "queue")
      public void listener(final Message message) throws DimensionException {
      // .......
      }
    
    • Am I missing something here, as per my understanding the only change that needs to be done is that ConnectionFactory should point to Rabbitmq test container, ryt?
    • Also, can we do more operations with RabbitMQContainer?
    • Even, I have bean created for the Queue, I'm getting this error in logs
    2023-09-01 00:38:38,888 WARN  [1-1] listener.BlockingQueueConsumer (BlockingQueueConsumer.java:670) - Queue declaration failed; retries left=3
    org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationException: Failed to declare queue(s):[abc]
    
  4. locked and limited conversation to collaborators on Aug 31, 2023
  5. converted this issue into a discussion #7453 on Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions