使用spEL

时间:2016-06-15 15:03:44

标签: java spring rabbitmq spring-amqp spring-rabbitmq

我从RabbitMQ开始我使用Maven创建了一个Spring Boot项目,并且我使用Spring依赖项来使事情正常工作,但是,尝试将队列名称外部化并尝试创建它#34;多队列"通过注释:

@RabbitListener(containerFactory = "rabbitListenerContainerFactory", queues = {"#{'${my.property.containing.the.queues}'.split(',')}"})

正如 spring amqp

的文档中所述
  

从1.5版开始,您可以使用外部化队列名称   财产占位符和SpEL:

@Component public class MyService {

@RabbitListener(queues = "#{'${property.with.comma.delimited.queue.names}'.split(',')}" )
     public void processOrder(String data, @Header(AmqpHeaders.CONSUMER_QUEUE) String queue) {
         ...
}

我刚刚得到以下stacktrace的错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myListenerBean' defined in file [F:\workspace\MyProject\target\classes\co\com\listener\MyListenerClass.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: @RabbitListener can't resolve '[Ljava.lang.String;@4d18b73a' as either a String or a Queue
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:690)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:970)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:959)
    at co.com.ExtendedAppConfig.main(ExtendedAppConfig.java:23)
Caused by: java.lang.IllegalArgumentException: @RabbitListener can't resolve '[Ljava.lang.String;@4d18b73a' as either a String or a Queue
    at org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.resolveQueues(RabbitListenerAnnotationBeanPostProcessor.java:307)
    at org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.processAmqpListener(RabbitListenerAnnotationBeanPostProcessor.java:242)
    at org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor$1.doWith(RabbitListenerAnnotationBeanPostProcessor.java:210)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:493)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:473)
    at org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.postProcessAfterInitialization(RabbitListenerAnnotationBeanPostProcessor.java:205)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1577)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    ... 14 common frames omitted

我的 pom.xml 如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Organization-RabbitMQ</groupId>
    <artifactId>Extended-Orchestrator</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>
    <dependencies>
        <!-- Dependency to another project -->
        <dependency>
            <groupId>Organization-RabbitMQ</groupId>
            <artifactId>Generic-Orchestrator</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <version>1.3.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.3.2.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestone</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestone</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

我错过了什么?提前谢谢

2 个答案:

答案 0 :(得分:0)

这绝对是1.5 GAhttps://jira.spring.io/browse/AMQP-533中引入的。

您的spring-rabbit依赖关系仍然以某种方式出现在1.4.x版本中:https://github.com/spring-projects/spring-amqp/blob/1.4.x/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/annotation/RabbitListenerAnnotationBeanPostProcessor.java#L307

尝试提供

<spring-amqp.version>1.5.6.RELEASE</spring-amqp.version>

属性进入你的pom。或者甚至更好地通过mvn dependency:tree -Dverbose来了解谁为你提供了错误的版本并解决了这个冲突。

答案 1 :(得分:0)

如果在System变量中使用Queue名称,并且需要使用条件自定义名称值,则可以使用Spel:

@RabbitListener(queues = "#{'${cluster.name}' != null?'batch.queue-${cluster.name}':'batch.queue'}", containerFactory = "listenerContainerFactory")