如何配置具有特定区域的 QueueMessagingTemplate 以将消息发送到 SQS?

How to configure QueueMessagingTemplate ,with specific region, to send message to SQS?

我目前正在使用 Spring 4.1.4 的 STS 3.6.3SR1。尝试测试 spring-cloud-aws 在我们的应用程序中使用的可行性。

与问题相关的罐子:

  1. spring-cloud-aws-autoconfigure-1.0.0.RC2.jar
  2. spring-cloud-aws-context-1.0.0.RC2.jar
  3. spring-cloud-aws-core-1.0.0.RC2.jar
  4. spring-cloud-aws-messaging-1.0.0.RC2.jar

使用 aws-sdk-1.9.19

这是我的 applicationContext 文件,它位于 spring-cloud-aws starter page 之后。

 <aws-context:context-region region="us-east-1"/>

    <aws-context:context-credentials>
          <aws-context:simple-credentials 
                access-key="*******" secret-key="*******"/>
    </aws-context:context-credentials>

    <aws-messaging:queue-messaging-template id="queueMessagingTemplate" />
    <aws-messaging:annotation-driven-queue-listener />

Spring-cloud-aws 的起始页建议 queuemessagingtemplate bean 在使用消息命名空间时会自动注入 AmazonSQS bean。但是,据我所知,我 运行 遇到了关于将字符串值区域转换为 AWS 枚举值 REGIONS 的错误。

这里是相关日志:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amazonSQSAsync': Cannot create inner bean '(inner bean)#23ea780a' of type [com.amazonaws.services.sqs.AmazonSQSAsyncClient] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#23ea780a': Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.amazonaws.regions.Region' to required type 'com.amazonaws.regions.Regions' for property 'region'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.amazonaws.regions.Region] to required type [com.amazonaws.regions.Regions] for property 'region': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:129)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:140)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1131)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1034)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory.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:762)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5524)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#23ea780a': Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.amazonaws.regions.Region' to required type 'com.amazonaws.regions.Regions' for property 'region'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.amazonaws.regions.Region] to required type [com.amazonaws.regions.Regions] for property 'region': no matching editors or conversion strategy found
    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.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299)
    ... 26 more
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.amazonaws.regions.Region' to required type 'com.amazonaws.regions.Regions' for property 'region'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.amazonaws.regions.Region] to required type [com.amazonaws.regions.Regions] for property 'region': no matching editors or conversion strategy found
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:475)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:511)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:505)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1515)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1474)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    ... 28 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.amazonaws.regions.Region] to required type [com.amazonaws.regions.Regions] for property 'region': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:287)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:460)
    ... 34 more

有人可以阐明我的上下文文件是否不正确吗?我相信 us-east-1 是指定区域的正确方法,但我是否遗漏了什么?

谢谢。

首先将您的 spring 版本升级到 4.1。 Spring Cloud AWS 需要 Spring 框架的 4.1 版,因为它使用新引入的消息传递 API。

我认为您的依赖版本不正确,因为您得到的错误是(无法将类型 'com.amazonaws.regions.Region' 的 属性 值转换为所需的类型 'com.amazonaws.regions.Regions' 属性 'region') 已用 RC2 修复(参见 https://github.com/spring-cloud/spring-cloud-aws/issues/36)。

最后一点,您应该使用与Spring Cloud AWS 相同的SDK 版本,即1.9.3。