简体   繁体   English

Spring Boot中的任务执行器

[英]Task executor in Spring Boot

In my Spring Boot application I'm listening message queue. 在我的Spring Boot应用程序中,我正在侦听消息队列。 When a message appears I need to execute it synchronously (one by one) in some task-executor. 当出现一条消息时,我需要在某些任务执行器中同步执行该操作(一个接一个)。

I'm using Amazon SQS, this is my config: 我正在使用Amazon SQS,这是我的配置:

    /**
     * AWS Credentials Bean
     */
    @Bean
    public AWSCredentials awsCredentials() {
        return new BasicAWSCredentials(accessKey, secretAccessKey);
    }

    /**
     * AWS Client Bean
     */
    @Bean
    public AmazonSQS amazonSQSAsyncClient() {
        AmazonSQS sqsClient = new AmazonSQSClient(awsCredentials());
        sqsClient.setRegion(Region.getRegion(Regions.US_EAST_1));
        return sqsClient;
    }

    /**
     * AWS Connection Factory
     */
    @Bean
    public SQSConnectionFactory connectionFactory() {
        SQSConnectionFactory.Builder factoryBuilder = new SQSConnectionFactory.Builder(
                Region.getRegion(Regions.US_EAST_1));
        factoryBuilder.setAwsCredentialsProvider(new AWSCredentialsProvider() {

            @Override
            public AWSCredentials getCredentials() {
                return awsCredentials();
            }

            @Override
            public void refresh() {
            }

        });
        return factoryBuilder.build();
    }

    /**
     * Registering QueueListener for queueName
     */
    @Bean
    public DefaultMessageListenerContainer defaultMessageListenerContainer() {
        DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
        messageListenerContainer.setConnectionFactory(connectionFactory());
        messageListenerContainer.setMessageListener(new MessageListenerAdapter(new QueueListener()));
        messageListenerContainer.setDestinationName(queueName);

        return messageListenerContainer;
    }

Also I need to have possibility to check the status of this task-executor, for example - number of scheduled tasks. 另外,我还需要检查此任务执行器的状态,例如-计划任务的数量。

Is it a good idea to use Spring SyncTaskExecutor for this purpose ? 为此目的使用Spring SyncTaskExecutor是个好主意吗? If so, could you please show an example how it can be used with Spring Boot. 如果是这样,请您举例说明如何将其与Spring Boot结合使用。

EDIT: 编辑:

After revealing your messaging technology and Spring configuration for it, simplest way for you is to configure SyncTaskExecutor (or Executors.newFixedThreadPool(1) would do the job also) as executor for your DefaultMessageListenerContainer . 揭示了消息传递技术和Spring的配置之后,最简单的方法是将SyncTaskExecutor (或Executors.newFixedThreadPool(1)也可以完成此工作)配置为DefaultMessageListenerContainer执行者。 Use this method . 使用此方法

You can register Task executor as separate bean (via @Bean annotation) and autowire it to defaultMessageListenerContainer() method (just add TaskExectuor as parameter). 您可以将Task executor注册为单独的bean(通过@Bean批注),并将其自动连接到defaultMessageListenerContainer()方法(只需将TaskExectuor添加为参数)。


Below answer is relevant for JMS messaging. 下面的答案与JMS消息传递有关。 It was created before AWS SQS usage was revealed in question: 它是在有问题的AWS SQS使用情况披露之前创建的:

You didn't mention which messaging technology are you using, therefore I assume JMS. 您没有提到要使用哪种消息传递技术,因此我假设使用JMS。

If synchronous execution is requirement, I believe you can't use native JMS listeners (need to avoid SimpleJmsListenerContainerFactory or SimleMessageListenerContainer ). 如果需要同步执行,我相信您不能使用本机JMS侦听器(需要避免SimpleJmsListenerContainerFactorySimleMessageListenerContainer )。

Instead I would suggest to use @JmsListener annotation with DefaultJmsListenerContainerFactory (this uses long polling instead of native JMS listeners) and configure SyncTaskExecutor (or Executors.newFixedThreadPool(1) would do the job also) as executor for mentioned container factory: DefaultJmsListenerContainerFactory.setTaskExecutor() . 相反,我建议将@JmsListener批注与DefaultJmsListenerContainerFactory (这将使用长轮询而不是本机JMS侦听器)一起使用,并配置SyncTaskExecutor (或Executors.newFixedThreadPool(1)也会执行此工作)作为上述容器工厂的执行者: DefaultJmsListenerContainerFactory.setTaskExecutor()

This is simple Spring Boot JMS example with DefaultJmsListenerContainerFactory configured . 这是配置了DefaultJmsListenerContainerFactory的简单Spring Boot JMS示例 You just need to plug in suitable task executor. 您只需要插入合适的任务执行器即可。

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

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