Spring AMQP: Creating multiple queues dynamically

青春壹個敷衍的年華 提交于 2020-12-15 07:29:21

问题


I have a Spring @Configuration class that creates and registers RabbitMQ queues as follows:

@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);
}

So far so good. I can create an activityQueue with no issue.

However, now I need to create multiple activity queues dynamically. So I changed my code as follows (only relevant section shown):

 @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()]);
}

However, returning the array of queues doesn't help and it throws the following exception:

[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

So my question is how can I create queues dynamically such as activityQueueA, activityQueueB, activityQueueC and so on?


回答1:


See the reference manual.

Starting with version 2.1, you can use

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

Constructors:

/**
 * 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)

with 2.0.x and 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)
    );
}


来源:https://stackoverflow.com/questions/54350102/spring-amqp-creating-multiple-queues-dynamically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!