使用SQS侦听器从SQS队列读取消息时,在simpleMessageListenerContainer中遇到问题

时间:2018-07-20 23:35:08

标签: spring spring-boot listener javabeans amazon-sqs

1.SQSConfig:我正在尝试使用Spring Boot框架使用SQS侦听器从Amazon SQS队列读取数据,其中我编写了以下代码。但是,对于simpleMessageListenerContainer,存在一个问题。谁能解释我该如何为该对象而不是常规的Java对象创建bin。

我的主要目的是读取SQS队列并获取队列中可用的消息。如果有人举任何例子,将会很有帮助。

package com.wxyz.imaging.configuration;

import com.amazonaws.auth.AWSCredentialsProvider;

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.services.sqs.AmazonSQSAsync;
import com.amazonaws.services.sqs.AmazonSQSAsyncClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;


@Configuration
public class SQSConfiguration {

@Bean
AWSCredentialsProvider credentialsProvider() {
    return new DefaultAWSCredentialsProviderChain();
}

@Autowired
protected AWSCredentialsProvider credProvider;

@Bean
@Primary
public AmazonSQSAsync amazonSQSAsync() {
    return AmazonSQSAsyncClientBuilder.standard().
            withCredentials(credProvider).
            build();
}


@Bean
public SimpleMessageListenerContainer simpleMessageListenerContainer(AmazonSQSAsync amazonSQSAsync) {
    SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
    simpleMessageListenerContainer.setAmazonSqs(amazonSQSAsync);
    simpleMessageListenerContainer.setMaxNumberOfMessages(10);
    return simpleMessageListenerContainer;
}

@Bean
public QueueMessagingTemplate messagingTemplate(@Autowired AmazonSQSAsync amazonSQSAsync) {
    return new QueueMessagingTemplate(amazonSQSAsync);
}

} 2.SQSListener

package com.wxyz.imaging.Listener;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.aws.messaging.listener.SqsMessageDeletionPolicy;
import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;

import java.util.Map;

public class SQSListener {
private static final Logger logger = LoggerFactory.getLogger(SQSListener.class);

@SqsListener(value = "${imaging-scanner-queue}", deletionPolicy = SqsMessageDeletionPolicy.ALWAYS)
public void receive(@Headers Map<String, String> header, @Payload String message) {
    try {
        logger.info("Message payload is: " + message);
        logger.info("Header from SQS is: " + header);


    } catch (Exception e) {
        System.out.print(e);
    }
}

}

3.Application.properties

sqs.region= us-west-2
sqs.queueName= imaging-scanner-queue

Error
2018-07-20 15:44:05.348  WARN 48026 --- [           main]             ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleMessageListenerContainer' defined in class path resource [com/wxyz/imaging/configuration/SQSConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: messageHandler must not be null
2018-07-20 15:44:05.352  INFO 48026 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-07-20 15:44:05.366  INFO 48026 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-07-20 15:44:05.372 ERROR 48026 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleMessageListenerContainer' defined in class path resource [com/wxyz/imaging/configuration/SQSConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: messageHandler must not be null
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1708) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:581) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at com.wxyz.imaging.Application.main(Application.java:11) [classes/:na]
Caused by: java.lang.IllegalStateException: messageHandler must not be null
at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at     org.springframework.cloud.aws.messaging.listener.AbstractMessageListenerContainer.validateConfiguration(AbstractMessageListenerContainer.java:255) ~[spring-cloud-aws-messaging-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at     org.springframework.cloud.aws.messaging.listener.AbstractMessageListenerContainer.afterPropertiesSet(AbstractMessageListenerContainer.java:249) ~[spring-cloud-aws-messaging-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at     org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.a    fterPropertiesSet(SimpleMessageListenerContainer.java:44) ~[spring-cloud-aws-messaging-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1767) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
... 16 common frames omitted

以退出代码1完成的过程

0 个答案:

没有答案