简体   繁体   English

如何通过两个账户使用AmazonSQS监听器

[英]How to use AmazonSQS listener with two accounts

I have application with two worker classes. 我有两个工人阶级的申请。 I want them to pull from AWS SQS ,but from two different accounts. 我希望他们从AWS SQS提取,但要从两个不同的帐户提取。 I am using @SQSListener to achive this. 我正在使用@SQSListener实现此目的。 I am having trouble to set the right AmazonSQS client for each queue.Tried to use custom destionationResolver but again it cannot access the right amazonSQS client bean. 我在为每个队列设置正确的AmazonSQS客户端时遇到麻烦。尝试使用自定义destionationResolver,但同样无法访问正确的AmazonSQS客户端bean。 I'm using AmazonSQSAsync maybe this is part of the problem. 我正在使用AmazonSQSAsync,也许这是问题的一部分。 Whit the custom destination resolver i am getting access denied for one of the queues. 惠特自定义目标解析器,我无法访问其中一个队列。 My config code: 我的配置代码:

@Bean(destroyMethod = "shutdown")
    @Primary
    public AmazonSQSAsync amazonSQS() {

        AmazonSQSAsync amazonSQSAsyncClient = new AmazonSQSAsyncClient(new AWSCredentialsProvider() {
            public void refresh() {}

            public AWSCredentials getCredentials() {
                return new AWSCredentials() {
                    public String getAWSSecretKey() {return secretKey;}

                    public String getAWSAccessKeyId() {return accessKey;}
                };
            }
        });

        QueueBufferConfig config = new QueueBufferConfig();
        config.setMaxBatchOpenMs(maxBatchOpenMs);
        config.setMaxBatchSize(maxBatchSize);

        LOGGER.info("SQS Client Initialized Successfully");
        return new AmazonSQSBufferedAsyncClient(amazonSQSAsyncClient, config);
    }

@Bean(destroyMethod = "shutdown")
    @Qualifier("workerSQS")
    public AmazonSQSAsync workerSQS() {
        final ClientConfiguration cc = new ClientConfiguration();
        cc.setConnectionTimeout(listenerConnectionTimeout);
        cc.setSocketTimeout(listenerSocketTimeout);
        cc.setMaxConnections(listenerMaxConnection);
        cc.setRequestTimeout(listenerRequestTimeout);
        cc.setUseReaper(true);
        //cc.setConnectionMaxIdleMillis();

        AWSCredentialsProvider awsCredentialsProvider = new AWSCredentialsProvider() {
            public void refresh() {}
            public AWSCredentials getCredentials() {
                return new AWSCredentials() {
                    public String getAWSSecretKey() {return routingSecretKey;}

                    public String getAWSAccessKeyId() {return routingAccessKey;}
                };
            }
        };

        AmazonSQSAsync amazonSQSAsyncClient = AmazonSQSAsyncClientBuilder.standard()
                .withCredentials(awsCredentialsProvider)
                .withRegion(Regions.US_EAST_1)
                .withClientConfiguration(cc)
                .build();

        // See https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-client-side-buffering-request-batching.html
        // for QueueBufferConfig Configuration Parameters
        QueueBufferConfig config = new QueueBufferConfig();
        config.setLongPoll(true);

        return new AmazonSQSBufferedAsyncClient(amazonSQSAsyncClient, config);
    }

 @Bean
    public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory() {
        SimpleMessageListenerContainerFactory msgListenerContainerFactory = new SimpleMessageListenerContainerFactory();
        msgListenerContainerFactory.setBackOffTime(listenerBackOffTime);
        msgListenerContainerFactory.setWaitTimeOut(listenerWaitTimeOut);
        msgListenerContainerFactory.setVisibilityTimeout(listenerVisibilityTimeOut);
        msgListenerContainerFactory.setMaxNumberOfMessages(listenerMaxMessagesPerPoll);
        msgListenerContainerFactory.setDestinationResolver(destinationResolver());
        return msgListenerContainerFactory;
    }

    @Bean
    public CustomDestinationResolver destinationResolver(){
        return new CustomDestinationResolver();
    }

    @Component
    public static class CustomDestinationResolver implements DestinationResolver{

        @Autowired
        private AmazonSQS amazonSQS;

        @Autowired
        @Qualifier("workerSQS")
        private AmazonSQSAsync amazonSQSAsync;

        @Override
        public String resolveDestination(String name) throws DestinationResolutionException {
            String queueName = name;

            if (queueName.startsWith("tl")) {
                try {
                    GetQueueUrlResult getQueueUrlResult = amazonSQSAsync.getQueueUrl(new GetQueueUrlRequest(name));
                    return getQueueUrlResult.getQueueUrl();
                } catch (QueueDoesNotExistException var4) {
                    throw new DestinationResolutionException(var4.getMessage(), var4);
                }
            } else {
                try {
                    GetQueueUrlResult getQueueUrlResult = amazonSQS.getQueueUrl(new GetQueueUrlRequest(name));
                    return getQueueUrlResult.getQueueUrl();
                } catch (QueueDoesNotExistException var4) {
                    throw new DestinationResolutionException(var4.getMessage(), var4);
                }
            }

        }
    }

I was not able to do it with SQS Listener,so i tried with JMS listener and it worked. 我无法使用SQS侦听器执行此操作,因此我尝试使用JMS侦听器,但此方法有效。 I simply created two JMS listenerContainerFactory and used them. 我只是创建了两个JMS listenerContainerFactory并使用了它们。 Each listener have different AWS account 每个监听器都有不同的AWS账户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM