Not able to read messages from SQS using @SqsListener

筅森魡賤 提交于 2019-12-12 12:33:20

问题


I'm trying to develop a SQS Listener which runs in background and reads message from AWS SQS whenever a new message arrives and it should never delete the message, since there will be a separate process for deleting message.

This is a standalone application, just started developing. But not able to proceed further since the basic thing is not working. I'm sure I'm missing something. I am using spring-cloud-aws-messaging (version: 1.2.0.BUILD-SNAPSHOT).

It's a very simple standalone application, having below files: One spring config file: application-context.xml (content already pasted above) One SQS Listener: SBSQSMessageListener (content already pasted above) One main program which is loading the application-context.xml & creating ApplicationContext

My Spring config file:

<aws-context:context-region region="us-west-2"/>

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

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

<aws-messaging:annotation-driven-queue-listener task-executor="simpleTaskExecutor" />           

<bean id="simpleTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="4" />
    <property name="maxPoolSize" value="10" />
    <property name="queueCapacity" value="1" />
    <property name="WaitForTasksToCompleteOnShutdown" value="true" />
</bean>

My SQSListener:

package sb.aws.sqs.listener;

import java.util.concurrent.ExecutionException;
import org.apache.log4j.Logger;
import org.springframework.cloud.aws.messaging.config.annotation.EnableSqs;
import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener;
import org.springframework.cloud.aws.messaging.listener.Acknowledgment;
import org.springframework.cloud.aws.messaging.listener.SqsMessageDeletionPolicy;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Component;

@Component
@EnableSqs
public class SBSQSMessageListener {

final static Logger logger = Logger.getLogger(SBSQSMessageListener.class);  

@MessageMapping("sb-sqs-queue1")    
@SqsListener(value = "sb-sqs-queue1", deletionPolicy = SqsMessageDeletionPolicy.NEVER)    
public void receive(String message, Acknowledgment acknowledgment)  {

    System.out.println("Inside receive: " + message);       

    try {
        acknowledgment.acknowledge().get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

I'm getting below exception:

DT:02/17/2017 15:20:20 LL:DEBUG hostname: storeId: txnId: CL:DefaultListableBeanFactory MSG:Creating shared instance of singleton bean 'simpleMessageListenerContainer' DT:02/17/2017 15:20:20 LL:DEBUG hostname: storeId: txnId: CL:DefaultListableBeanFactory MSG:Creating instance of bean 'simpleMessageListenerContainer' DT:02/17/2017 15:20:20 LL:DEBUG hostname: storeId: txnId: CL:DefaultListableBeanFactory MSG:Returning cached instance of singleton bean 'org.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration' DT:02/17/2017 15:20:20 LL:DEBUG hostname: storeId: txnId: CL:DefaultListableBeanFactory MSG:Returning cached instance of singleton bean 'amazonSQSAsync' DT:02/17/2017 15:20:20 LL:DEBUG hostname: storeId: txnId: CL:DefaultListableBeanFactory MSG:Creating shared instance of singleton bean 'amazonSQS' DT:02/17/2017 15:20:20 LL:DEBUG hostname: storeId: txnId: CL:DefaultListableBeanFactory MSG:Creating instance of bean 'amazonSQS' DT:02/17/2017 15:20:20 LL:DEBUG hostname: storeId: txnId: CL:DefaultListableBeanFactory MSG:Returning cached instance of singleton bean 'org.springframework.cloud.aws.messaging.config.annotation.SqsClientConfiguration' DT:02/17/2017 15:20:21 LL:DEBUG hostname: storeId: txnId: CL:DefaultListableBeanFactory MSG:Eagerly caching bean 'amazonSQS' to allow for resolving potential circular references DT:02/17/2017 15:20:21 LL:DEBUG hostname: storeId: txnId: CL:DefaultListableBeanFactory MSG:Finished creating instance of bean 'amazonSQS' Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'simpleMessageListenerContainer' defined in class path resource [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.amazonaws.services.sqs.AmazonSQSAsync]: : No qualifying bean of type [com.amazonaws.services.sqs.AmazonSQSAsync] is defined: expected single matching bean but found 2: amazonSQSAsync,amazonSQS; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.amazonaws.services.sqs.AmazonSQSAsync] is defined: expected single matching bean but found 2: amazonSQSAsync,amazonSQS at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:464) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006) 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$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:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93) at sb.aws.sqs.SBSQSMain.main(SBSQSMain.java:14) Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.amazonaws.services.sqs.AmazonSQSAsync] is defined: expected single matching bean but found 2: amazonSQSAsync,amazonSQS at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ... 15 more

Please could you guide me if I'm missing anything?

Many Thanks.

来源:https://stackoverflow.com/questions/42160399/not-able-to-read-messages-from-sqs-using-sqslistener

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