Spring SQS Listener - 启动后立即终止

时间:2018-04-24 18:03:47

标签: java spring amazon-web-services spring-boot spring-cloud

我正在尝试建立一个SQS消费者。下面是我用过的代码。但是,应用程序立即关闭,并且不会消耗我发布到队列的消息。但是,当我使用AWS Java SDK时,我可以使用这些消息。

@SpringBootApplication
public class QueueConsumerApplication {

    @Value("${proxy.host}")
    private String proxyHost;

    @Value("${proxy.port}")
    private int proxyPort;

    @Value("${sqs.url:<sqsurl>/data-replication.fifo}")
    private String amazonSqsEndpoint;

    public static void main(String[] args) {
        SpringApplication.run(QueueConsumerApplication.class, args);
    }

    @Bean
    public AmazonSQSAsync amazonSQSAsyncClient()
    {

        AmazonSQSAsync amazonSQSAsyncClient = AmazonSQSAsyncClientBuilder.standard()
                .withCredentials(getProfileCredentialsProvider())
                .withClientConfiguration(getClientConfiguration())
                .withRegion(Regions.US_EAST_1)
                .build();



        return amazonSQSAsyncClient;
    }

    @Bean
    public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory() {
        SimpleMessageListenerContainerFactory msgListenerContainerFactory = new SimpleMessageListenerContainerFactory();
        msgListenerContainerFactory.setAmazonSqs(amazonSQSAsyncClient());
        return msgListenerContainerFactory;
    }

    @Bean
    public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSqs) {
        return new QueueMessagingTemplate(amazonSQSAsyncClient());
    }


    public ProfileCredentialsProvider getProfileCredentialsProvider(){
        ProfileCredentialsProvider credentialsProvider = new ProfileCredentialsProvider("Dev_Developer");
        try {
            credentialsProvider.getCredentials();
            System.out.println(credentialsProvider.getCredentials().getAWSAccessKeyId());
            System.out.println(credentialsProvider.getCredentials().getAWSSecretKey());

        } catch (Exception e) {
            throw new AmazonClientException(
                    "Cannot load the credentials from the credential profiles file. " +
                            "Please make sure that your credentials file is at the correct " +
                            "location (~/.aws/credentials), and is in valid format.",
                    e);
        }
        return credentialsProvider;
    }


    private ClientConfiguration getClientConfiguration(){
        ClientConfiguration config = new ClientConfiguration();
        config.setProtocol(Protocol.HTTPS);
        config.setProxyHost(proxyHost);
        config.setProxyPort(proxyPort);
        return config;
    }
}

监听

@Component
public class QueueListener {

    @SqsListener(value = "data-replication.fifo", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
    public void receive(String message)
    {
        System.out.println("message: " + message);
    }

}

0 个答案:

没有答案