Java 任何带有 TaskExecutor 示例的良好 Spring 线程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/852743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:11:57  来源:igfitidea点击:

Any good Spring threading with a TaskExecutor examples?

javamultithreadingspringexecutor

提问by James McMahon

I'm trying to get a handle on how to implement threading in a Java application that uses Spring for transaction management. I've found the TaskExecutor section in the Spring documentation, and ThreadPoolTaskExecutor looks like it would fit my needs;

我正在尝试了解如何在使用 Spring 进行事务管理的 Java 应用程序中实现线程。我在Spring 文档中找到了 TaskExecutor 部分,ThreadPoolTask​​Executor 看起来很适合我的需要;

ThreadPoolTaskExecutor

This implementation can only be used in a Java 5 environment but is also the most commonly used one in that environment. It exposes bean properties for configuring a java.util.concurrent.ThreadPoolExecutor and wraps it in a TaskExecutor. If you need something advanced such as a ScheduledThreadPoolExecutor, it is recommended that you use a ConcurrentTaskExecutor instead.

线程池任务执行器

此实现只能在 Java 5 环境中使用,但也是该环境中最常用的一种。它公开用于配置 java.util.concurrent.ThreadPoolExecutor 的 bean 属性并将其包装在 TaskExecutor 中。如果您需要诸如 ScheduledThreadPoolExecutor 之类的高级功能,建议您改用 ConcurrentTaskExecutor。

However I have no idea how to go about using it. I've been searching for good examples for awhile now with no luck. If anyone can help me out I would appreciate it.

但是我不知道如何使用它。我一直在寻找好的例子,但没有运气。如果有人可以帮助我,我将不胜感激。

采纳答案by Jacob Mattison

It's pretty simple. The idea is that you have an executor object that's a bean, which is passed into whatever object wants to fire the new task (in a new thread). The nice thing is that you can modify what type of task executor to use just by changing the Spring config. In the example below I'm taking some example class (ClassWithMethodToFire) and wrapping it in a Runnable object to do the fire; you could also actually implement Runnable in a class of your own, and then in the execute method you'd just call classWithMethodToFire.run().

这很简单。这个想法是你有一个 executor 对象,它是一个 bean,它被传递到任何想要触发新任务的对象(在一个新线程中)。好消息是您可以通过更改 Spring 配置来修改要使用的任务执行器类型。在下面的示例中,我采用了一些示例类 (ClassWithMethodToFire) 并将其包装在一个 Runnable 对象中以进行触发;您实际上也可以在您自己的类中实现 Runnable,然后在 execute 方法中调用classWithMethodToFire.run().

Here's a very simple example.

这是一个非常简单的例子。

public class SomethingThatShouldHappenInAThread {
     private TaskExecutor taskExecutor;
     private ClassWithMethodToFire classWithMethodToFire;

     public SomethingThatShouldHappenInAThread(TaskExecutor taskExecutor,
                                               ClassWithMethodToFire classWithMethodToFire) {
          this.taskExecutor = taskExecutor;
          this.classWithMethodToFire = classWithMethodToFire;
     }

     public void fire(final SomeParameterClass parameter) {
          taskExecutor.execute( new Runnable() {
               public void run() {
                    classWithMethodToFire.doSomething( parameter );
               }
          });
     }
}

And here are the Spring beans:

这是 Spring bean:

<bean name="somethingThatShouldHappenInAThread" class="package.name.SomethingThatShouldHappenInAThread">
     <constructor-arg type="org.springframework.core.task.TaskExecutor" ref="taskExecutor" />
     <constructor-arg type="package.name.ClassWithMethodToFire" ref="classWithMethodToFireBean"/>
</bean>

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
     <property name="corePoolSize" value="5" />
     <property name="maxPoolSize" value="10" />
     <property name="queueCapacity" value="25" />
</bean>