Spring 运行时未创建RabbitMQ队列
我有一个spring boot 1.5.22+amqp的简单示例,问题是 队列不是动态创建的,应该是Spring 运行时未创建RabbitMQ队列,spring,spring-boot,rabbitmq,spring-rabbit,Spring,Spring Boot,Rabbitmq,Spring Rabbit,我有一个spring boot 1.5.22+amqp的简单示例,问题是 队列不是动态创建的,应该是 @Component class ReceiverComponent { @RabbitListener(queues = 'spring-boot-queue-2') public void receive_2(String content) { System.out.println("[ReceiveMsg-2] receive msg: " + content); } @Com
@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";
}
我得到的错误是:
原因:com.rabbitmq.client.ShutdownSignalException:通道错误;协议方法:#方法(回复代码=404,回复文本=未找到-vhost'/'中没有队列'spring-boot-queue-2',类id=50,方法id=10)
这和兔子身上的权利有关系吗?
安装的版本为3.7.13,我的连接数据为:
spring:
# Configure rabbitMQspring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
你能说:
@Bean
public Queue queue() {
return new Queue("spring-boot-queue-2'");
}
在用
@配置注释的类中
?这是正确的。当我添加了一切都按预期工作时。但是使用spring boot,它应该在我不显式声明队列的情况下工作。但是使用spring boot,它应该在我不显式声明队列的情况下工作。当我调试导致问题的类AMQPChannel时,它显示了与rabbitmq建立的连接是:AMQChannel(amqp://guest@127.0.0.1:5672/,1),当我尝试像这样创建队列时:channel.queueDeclare(..)它可以工作,但AMQPChannel的toString给了我:AMQChannel(amqp://guest@0:0:0:0:0:0:0:1:5672/,1)但那不是真的。您应该在使用队列之前以某种方式创建它。您可以使用Rabbit的管理界面来创建它,或者让Spring使用@Bean
来创建它。一旦创建了队列(第二次运行及以后),就不再需要了。Boot无法对队列的声明方式做出任何假设(自动删除、独占、TTL、DLQ等)。必须添加具有所需特征的队列@Bean
。或者您可以将@QueueBinding
添加到@RabbitListener
中,这相当于添加一个队列bean。