如何为SpringBoot配置多个SQS JMS侦听器?

时间:2019-03-04 06:45:11

标签: java spring-boot jms amazon-sqs

我有2个AWS SQS,其中一个包含活动,另一个包含附件信息,我具有JmsSqsListener和ActivitySqsConfig,如下所示。

使用DefaultMessageListenerContainer我一次只能为活动或附件配置一个JmsSqsListener,但是我想为SQS和DefaultMessageListenerContainer都配置JmsSqsListener不允许我这样做。我还使用不同的类配置了两个不同的JmsSqsListener配置,但即使这样也不起作用。它只是在听一个队列。

@Configuration
public class JmsSqsListener {

    @Value("${aws.access.key}")
    private String accessKey;

    @Value("${aws.secret.key}")
    private String secretKey;

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

    @Value("${leaks.activity.queue}")
    private String activityQueue;

    @Value("${leaks.attachment.queue}")
    private String attachmentQueue;

    @Autowired
    private ActivityEngine activityListener;

    @Autowired
    private AttachmentEngine attachmentListener;   // How to add Listener for attachmentQueue

    @Bean
    public DefaultMessageListenerContainer jmsListenerContainer() {
        SQSConnectionFactory sqsConnectionFactory = new SQSConnectionFactory(new ProviderConfiguration(),
                AmazonSQSClientBuilder.standard().withRegion(region).withCredentials(awsCredentialsProvider));
        DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
        dmlc.setConnectionFactory(sqsConnectionFactory);
        dmlc.setDestinationName(activityQueue);// Also want to add attachmentQueue
        dmlc.setMessageListener(activityListener);  // Also want to add attachmentListener

        return dmlc;
    }

    @Bean
    public JmsTemplate createJMSTemplate() {
        SQSConnectionFactory sqsConnectionFactory = new SQSConnectionFactory(new ProviderConfiguration(),
                AmazonSQSClientBuilder.standard().withRegion(region).withCredentials(awsCredentialsProvider));

        JmsTemplate jmsTemplate = new JmsTemplate(sqsConnectionFactory);
        jmsTemplate.setDefaultDestinationName(queueName);
        jmsTemplate.setDeliveryPersistent(false);

        return jmsTemplate;
    }

    private final AWSCredentialsProvider awsCredentialsProvider = new AWSCredentialsProvider() {
        @Override
        public AWSCredentials getCredentials() {
            return new BasicAWSCredentials(accessKey, secretKey);
        }

        @Override
        public void refresh() { }
    };
}

SQS配置

@Configuration
public class SqsConfig {

    @Value("${aws.access.key}")
    private String accessKey;

    @Value("${aws.secret.key}")
    private String secretKey;

    @Value("${aws.queue.endpoint}")
    private String endpoint;

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

    @Value("${leaks.activity.queue}")
    private String activityQueue;

    @Value("${leaks.attachment.queue}")
    private String attachmentQueue;

    @Bean
    public AmazonSQS createSQSClient() {
        AmazonSQSClientBuilder builder = AmazonSQSClientBuilder.standard().withCredentials(awsCredentialsProvider);
        builder.setEndpointConfiguration(new EndpointConfiguration(endpoint, region));
        AmazonSQS amazonSQSClient = builder.build();
        amazonSQSClient.createQueue(activityQueue); // want to create attachmentQueue also

        return amazonSQSClient;
    }

    private final AWSCredentialsProvider awsCredentialsProvider = new AWSCredentialsProvider() {
        @Override
        public AWSCredentials getCredentials() {
            return new BasicAWSCredentials(accessKey, secretKey);
        }

        @Override
        public void refresh() { }
    };
}

请帮助我配置JMS侦听器,以便 1)ActivityEngine可以监听activityQueue 同时 2)AttachmentEngine可以监听attachmentQueue;

0 个答案:

没有答案