春季引导异步

huangapple go评论87阅读模式
英文:

Spring boot Async

问题

我不确定Spring Boot异步是如何工作的。我知道web服务器有其自己的线程池,每个请求都由池中的一个线程处理。现在,当我们使用Spring Boot实现异步web服务并配置,假设有10个线程,这是否意味着有线程嵌套在另一个线程内?为了澄清,web服务器将请求分配给一个线程。现在,当这个线程开始执行并调用一个标有@Async的服务/函数时,它可以创建多个线程来处理该请求。如果我的理解正确,请告诉我。

英文:

I am not sure how does Spring boot async work? I know that webserver has its own thread pool and every request is handled by a thread from the pool. Now when we implement asynchronous web service with spring boot and configure let's say 10 threads, does that mean there is thread inside a thread? To clarify, the web server will assign a request to a thread. Now when this thread starts execution and calls a service/function marked with @Asynch, it can create multiple threads to handle the request. Please let me know if my understanding is correct.

答案1

得分: 2

这不是一个线程嵌套的情况。@Async会使请求在单独的线程中执行,而请求的调用者不会等待被调用方法完成。

要启用异步,可以通过添加以下内容来实现:

@Configuration
@EnableAsync
public class AsyncExample {
   // 如果需要,可以定义一个线程池执行器
   @Bean(name = "threadPoolTaskExecutor")
   public Executor threadPoolTaskExecutor() {
      return new ThreadPoolTaskExecutor();
   }
}

然后可以使用已定义的线程池执行器来定义异步方法,如下所示:

@Async("threadPoolTaskExecutor")
public void asyncMethodWithConfiguredExecutor() {
   // 方法定义
}
英文:

Its not a thread inside another. @Async will make the request execute in a separate thread and the caller of the request will not wait for called method completion

To enable Async, you can do by adding,

@Configuration
@EnableAsync
public class AsyncExample {
   // Define a threadpool executor if needed,
   @Bean(name = "threadPoolTaskExecutor")
   public Executor threadPoolTaskExecutor() {
      return new ThreadPoolTaskExecutor();
   }
}

Then you can define the async method as below with the defined threadpool executor,

@Async("threadPoolTaskExecutor")
public void asyncMethodWithConfiguredExecutor() {
   // Method Definition
}

答案2

得分: 0

使用@Async将使它在单独的线程中执行,即调用者不会等待被调用方法的完成。
并且您可以配置所使用的执行器

@Configuration
@EnableAsync
public class SpringAsyncConfig {
     
    @Bean(name = "fooExecutor")
    public Executor fooExecutor() {
        return new ThreadPoolTaskExecutor();
    }
}

并使用@Async("fooExecutor")

英文:

Using @Async will make it execute in a separate thread i.e. the caller will not wait for the completion of the called method.
And you can configure the used Executor

@Configuration
@EnableAsync
public class SpringAsyncConfig {
     
    @Bean(name = "fooExecutor")
    public Executor fooExecutor() {
        return new ThreadPoolTaskExecutor();
    }
}

And using @Async("fooExecutor")

答案3

得分: 0

以下是翻译好的部分:

你可以按照以下方式进行配置,以更好地控制线程参数,例如带有前缀的线程名称、池大小等等。通过这种方式,您可以确保对线程池进行控制。

@Configuration
@EnableAsync
public class AsyncConfiguration implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(5000);
        executor.setThreadNamePrefix("MyThread-");
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }
}

然后,在您想要使其异步化的代码bean或方法上加上@Async注解。

有关此主题的更多信息,请参考Spring的调度文档此处

希望您会发现这个有用 春季引导异步

英文:

You can make a configuration as following to control more the thread parameters such as the name of thread with prefix, the pool size,... in this way you ensure that you control the thread pool.

   @Configuration
    @EnableAsync
    public class AsyncConfiguration implements AsyncConfigurer {
    
        @Override
        public Executor getAsyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(5);
            executor.setMaxPoolSize(10);
            executor.setQueueCapacity(5000);
            executor.setThreadNamePrefix("MyThread-");
            executor.setWaitForTasksToCompleteOnShutdown(true);
            executor.initialize();
            return executor;
        }
    
        @Override
        public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
            return new SimpleAsyncUncaughtExceptionHandler();
        }
    }

And you put the @Async annotation on the code bean or the method that you want to make it asynchronous.

For more information about this subject you can refer to the scheduling documentation of spring here.

I hope you will find this helpful 春季引导异步

huangapple
  • 本文由 发表于 2020年4月4日 08:35:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/61022376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定