Spring AMQP:动态创建多个队列

时间:2019-01-24 15:28:39

标签: spring-amqp

我有一个Spring @Configuration类,该类创建和注册RabbitMQ队列,如下所示:

@Amqp
@Configuration
public class AmqpConfiguration {

@Bean
ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(HOST);
    connectionFactory.setUsername(USERNAME);
    connectionFactory.setPassword(PASSWORD);
    connectionFactory.addConnectionListener(brokerListener);
    return connectionFactory;
}

@Bean
SimpleMessageListenerContainer messageListenerContainer2(ConnectionFactory connectionFactory) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueues(activityQueue());
    container.setMessageListener(listener);
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    container.setPrefetchCount(Props.getInt(ACTIVITY_QUEUE_PREFETCH_COUNT));
    return container;
}

@Bean
AmqpAdmin amqpAdmin() {
    return new RabbitAdmin(connectionFactory());
}

@Bean
 Queue activityQueue() {
    return new Queue(ACITVITY_QUEUE);
}

到目前为止,一切都很好。我可以毫无问题地创建一个activityQueue。

但是,现在我需要动态创建多个活动队列。因此,我将代码更改如下(仅显示了相关部分):

 @Bean
SimpleMessageListenerContainer messageListenerContainer2(ConnectionFactory connectionFactory) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueues(getAllQueues());
    container.setMessageListener(listener);
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    container.setPrefetchCount(Props.getInt(ACTIVITY_QUEUE_PREFETCH_COUNT));
    return container;
}

@Bean
Queue[] getAllQueues() {
   // Detail omitted
    return queues.toArray(new Queue[queues.size()]);
}

但是,返回队列数组无济于事,它会引发以下异常:

[Thread=SimpleAsyncTaskExecutor-1] 2019-01-24 10:26:04,112 WARN : org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Reconnect failed; retries left=2
java.io.IOException
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106)
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124)
at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:790)
at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:365)
at com.sun.proxy.$Proxy168.queueDeclarePassive(Unknown Source)
at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:237)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:534)
at java.lang.Thread.run(Thread.java:748)

Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; reason: {#method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'activityQueue-F865FD8C-7AB2ABAC-153D0A6B-5BE36001-10A075A6' in vhost '/', class-id=50, method-id=10), null, ""}
at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343)
at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118)
... 11 more

所以我的问题是如何动态创建队列,例如activityQueueA,activityQueueB,activityQueueC等?

1 个答案:

答案 0 :(得分:1)

请参见the reference manual

从2.1版开始,您可以使用

@Bean
public Declarables qs() {
    return new Declarables(
            new Queue("q2", false, false, true),
            new Queue("q3", false, false, true));
}

构造函数:

/**
 * A collection of {@link Declarable} objects; used to declare multiple objects on the
 * broker using a single bean declaration for the collection.
 *
 * @author Gary Russell
 * @since 2.1
 */
public class Declarables {

    public Declarables(Declarable... declarables)

    public Declarables(Collection<Declarable> declarables)

具有2.0.x和1.7.x(reference manual)。

@Bean
public List<Queue> qs() {
    return Arrays.asList(
            new Queue("q2", false, false, true),
            new Queue("q3", false, false, true)
    );
}