Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向ContainerFactory添加多个@RabbitListener bean的优雅方式_Java_Spring Mvc_Rabbitmq_Spring Amqp_Spring Rabbit - Fatal编程技术网

Java 向ContainerFactory添加多个@RabbitListener bean的优雅方式

Java 向ContainerFactory添加多个@RabbitListener bean的优雅方式,java,spring-mvc,rabbitmq,spring-amqp,spring-rabbit,Java,Spring Mvc,Rabbitmq,Spring Amqp,Spring Rabbit,这是我的@配置 这里是@RabbitListener类 只有当我分别实例化processListener、processListener2和processListener3时,我才开始在进程队列的RabbitMQ管理中看到多个使用者,并且每个侦听器都在处理消息,否则,尽管指定了setConcurrentConsumers,我只看到一个使用者 是否有一种优雅的方法可以根据需要声明多个侦听器,并根据需要增减。还是将多个@Beans声明为唯一选项?还是我做错了什么?您使用的是什么版本 我刚刚复制了你们

这是我的@配置

这里是@RabbitListener类

只有当我分别实例化processListener、processListener2和processListener3时,我才开始在进程队列的RabbitMQ管理中看到多个使用者,并且每个侦听器都在处理消息,否则,尽管指定了setConcurrentConsumers,我只看到一个使用者


是否有一种优雅的方法可以根据需要声明多个侦听器,并根据需要增减。还是将多个@Beans声明为唯一选项?还是我做错了什么?

您使用的是什么版本

我刚刚复制了你们的集装箱工厂,它对我来说很好2.1.3

顺便说一句,从2.0版开始,您可以向@RabbitListener添加并发性,它将覆盖容器工厂中的任何值

/**
 * Set the concurrency of the listener container for this listener. Overrides the
 * default set by the listener container factory. Maps to the concurrency setting of
 * the container type.
 * <p>For a
 * {@link org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer
 * SimpleMessageListenerContainer} if this value is a simple integer, it sets a fixed
 * number of consumers in the {@code concurrentConsumers} property. If it is a string
 * with the form {@code "m-n"}, the {@code concurrentConsumers} is set to {@code m}
 * and the {@code maxConcurrentConsumers} is set to {@code n}.
 * <p>For a
 * {@link org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer
 * DirectMessageListenerContainer} it sets the {@code consumersPerQueue} property.
 * @return the concurrency.
 * @since 2.0
 */
String concurrency() default "";

另外,不相关,但不应在bean声明中执行此rabbitAdmin.declareExchangedirExchange-在应用程序上下文生命周期中,连接到RabbitMQ还为时过早。将交换、队列和绑定添加为@Beans,管理员将自动查找并声明它们。

此外,为什么要在factory bean定义中创建并丢弃容器?我没有使用Spring Boot,只是使用Spring framework mvc 5.1.4版。不使用Boot并不重要;我指的是Spring AMQP版本。很抱歉,我的错误,我使用的是Spring AMQP版本2.1.3。此外,吊着的集装箱也是罪魁祸首。谢谢您的快速回复@garyrussellal也谢谢您指出declareeexchange为@Beans
@RabbitListener(containerFactory = "rabbitListenerContainerFactory", queues = "process")
public class ProcessQueueListener
{

    public ProcessQueueListener()
    {
    }

    @RabbitHandler
    void receiveMessage(String message)
    {
        // doSomething
    }

} 
/**
 * Set the concurrency of the listener container for this listener. Overrides the
 * default set by the listener container factory. Maps to the concurrency setting of
 * the container type.
 * <p>For a
 * {@link org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer
 * SimpleMessageListenerContainer} if this value is a simple integer, it sets a fixed
 * number of consumers in the {@code concurrentConsumers} property. If it is a string
 * with the form {@code "m-n"}, the {@code concurrentConsumers} is set to {@code m}
 * and the {@code maxConcurrentConsumers} is set to {@code n}.
 * <p>For a
 * {@link org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer
 * DirectMessageListenerContainer} it sets the {@code consumersPerQueue} property.
 * @return the concurrency.
 * @since 2.0
 */
String concurrency() default "";