使用TaskExecutor示例的任何好的Spring线程? [英] Any good Spring threading with a TaskExecutor examples?

查看:114
本文介绍了使用TaskExecutor示例的任何好的Spring线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;


ThreadPoolTask​​Executor

ThreadPoolTaskExecutor

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

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.

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

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.

推荐答案

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

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().

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

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 );
               }
          });
     }
}

以下是春豆:

<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>

这篇关于使用TaskExecutor示例的任何好的Spring线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆