SqsListener没有针对本地堆栈队列运行?

时间:2018-12-14 13:18:27

标签: java spring-boot amazon-sqs

我是Spring Boot的新手,正在尝试使用sqs侦听器轮询本地堆栈中的测试队列。我可以将消息推送到本地堆栈队列中。但是,然后我想轮询相同的队列并记录消息的内容。但是,我没有得到由sqs lister记录到控制台的任何消息吗?

application-local.properties

cloud.aws.region=us-east-1
cloud.local.sqs=http://localstack:4576
cloud.local.s3=http://localstack:4572

app.sqs.maxmessages=1
app.sqs.input=http://localstack:4576/queue/test-queue
app.sqs.output=http://localstack:4576/queue/test-queue

AppController

@Log4j2
@RestController
public class AppController {

    private AmazonS3 s3;

    private SQSOutput output;

    @Autowired
    public AppController(AmazonS3 s3, SQSOutput output) {

        this.s3 = s3;
        this.output = output;
    }

    @RequestMapping("/send")
    public Map<String, String> sendMessage() {
        output.send("Test Message!");

        Map<String, String> response = new HashMap<>();
        response.put("message", "Message sent!");
        return response;
    }

    @SqsListener(value = "${app.sqs.input}", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
    public void getMessage(String message) {

        log.info("Received message: " + message);
    }

}

SetupBeans

@Component
public class SetupBeans {

    @Value("${cloud.aws.region}")
    private String region;

    @Value("${cloud.local.s3}")
    private String localCloudS3;

    @Value("${cloud.local.sqs}")
    private String localCloudSQS;

    @Value("${app.sqs.output}")
    private String outputUrl;

    @Value("${app.sqs.maxmessages}")
    private int maxMessages;

    @Bean
    @Primary
    private AWSCredentialsProvider credProvider() {
        return DefaultAWSCredentialsProviderChain.getInstance();
    }

    @Bean
    @Primary
    public AmazonS3 amazonS3() {
        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder
                .EndpointConfiguration(localCloudS3, region);
        return AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(endpointConfiguration)
                .build();
    }

    @Bean
    @Primary
    private AmazonSQSAsync amazonSQSAsync() {
        AmazonSQSAsyncClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder
                .EndpointConfiguration(localCloudSQS, region);
        return AmazonSQSAsyncClientBuilder.standard()
                .withEndpointConfiguration(endpointConfiguration)
                .build();
    }

    @Bean
    private QueueMessagingTemplate queueMessagingTemplate() {

        return new QueueMessagingTemplate(amazonSQSAsync());
    }

    @Bean
    public QueueMessageHandlerFactory queueMessageHandlerFactory() {
        QueueMessageHandlerFactory factory = new QueueMessageHandlerFactory();
        MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter();

        // set strict content type match to false
        messageConverter.setStrictContentTypeMatch(false);
        factory.setArgumentResolvers(Collections.<HandlerMethodArgumentResolver>singletonList(new PayloadArgumentResolver(messageConverter)));
        return factory;
    }

    @Bean
    public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSQS){
        SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
        factory.setAmazonSqs(amazonSQS);
        factory.setMaxNumberOfMessages(maxMessages);
        return factory;
    }

    @Bean
    private QueueMessageChannel getQueueMessageChannel() {
        return new QueueMessageChannel(amazonSQSAsync(), outputUrl);
    }

    @Bean
    public SQSOutput getSQSOutput() {
        return new SQSOutput(queueMessagingTemplate(), getQueueMessageChannel());
    }
}

1 个答案:

答案 0 :(得分:1)

我正在使用internal class Sound { public bool HasAudio { get { return mediaPlayer.HasAudio; } } private MediaPlayer mediaPlayer = new MediaPlayer(); public void Play(string fileName) { mediaPlayer.Open(new Uri(@"sounds/" + fileName, UriKind.RelativeOrAbsoute)); mediaPlayer.Play(); } public void Stop() { mediaPlayer.Stop(); } } ,但需要使用org.springframework.cloud:spring-cloud-aws-messaging