-1

I have an easy example of spring boot 1.5.22 + amqp and the problem is that queue is not getting created dynamically, and it should.

@Component
class ReceiverComponent {

@RabbitListener(queues = 'spring-boot-queue-2')
public void receive_2(String content) {
    System.out.println("[ReceiveMsg-2] receive msg: " + content);
}

@Component
class SenderComponent {

@Autowired
private AmqpAdmin amqpAdmin;

// The default implementation of this interface is RabbitTemplate, which 
currently has only one implementation.
@Autowired
private AmqpTemplate amqpTemplate;

/**
 * send message
 *
 * @param msgContent
 */
public void send_2(String msgContent) {
    amqpTemplate.convertAndSend(RabbitConfig.SPRING_BOOT_EXCHANGE, 
RabbitConfig.SPRING_BOOT_BIND_KEY, msgContent);
}

@Configuration
class RabbitConfig {

// Queue name
public final static String SPRING_BOOT_QUEUE = "spring-boot-queue-2";
// Switch name
public final static String SPRING_BOOT_EXCHANGE = "spring-boot-exchange- 
2";
// Bound values
public static final String SPRING_BOOT_BIND_KEY = "spring-boot-bind-key- 
2";
}

The error i'm getting is :

Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method(reply-code=404, reply-text=NOT_FOUND - no queue 'spring-boot-queue-2' in vhost '/', class-id=50, method-id=10)

Does it has to do something with right on the rabbitmq ? The version installed is 3.7.13 and my coonection data is :

spring:
# Configure rabbitMQspring:
rabbitmq:
  host: 127.0.0.1
  port: 5672
  username: guest
  password: guest
Marko
  • 11
  • 3

1 Answers1

1

Can you put:

@Bean
public Queue queue() {
    return new Queue("spring-boot-queue-2'");
}

in your class annotated with @Configuration?

Hasan Can Saral
  • 2,950
  • 5
  • 43
  • 78
  • That's true. When I added that everything worked as expected. – Marko Oct 11 '19 at 12:53
  • But with spring boot it should have worked without me declaring queue explicitly. – Marko Oct 11 '19 at 12:53
  • But with spring boot it should have worked without me declaring queue explicitly. When I debugged the classes AMQPChannel which has caused the problem, it has shown that the connection established to rabbitmq is : AMQChannel(amqp://guest@127.0.0.1:5672/,1), and when i try to create the queue like this : channel.queueDeclare(..) it works but the toString for AMQPChannel gives me : AMQChannel(amqp://guest@0:0:0:0:0:0:0:1:5672/,1) ?? – Marko Oct 11 '19 at 13:02
  • That's not true though. You should create the queue somehow before using it. You can use the management interface of Rabbit to create it, or let Spring create it with `@Bean`. Once it's created (2nd run and onwards) then there's no need anymore. – Hasan Can Saral Oct 11 '19 at 13:04
  • 1
    Boot cannot make any assumptions about how you want the queue declared (auto-delete, exclusive, TTL, DLQ, etc, etc). You must add a `Queue` `@Bean` with the required characteristics. Or you can add `@QueueBinding` to your `@RabbitListener` which is the equivalent of adding a queue bean. – Gary Russell Oct 11 '19 at 15:36