简体   繁体   English

如何使用spring触发异步批处理任务?

[英]How can I use spring to fire off an asynchronous batch task?

Basically I would like to do some multithreading in my spring java program and I don't know how to use my servers spring resources to create a new thread. 基本上,我想在我的spring java程序中做一些多线程处理,而且我不知道如何使用服务器的spring资源来创建新线程。

I looked at Any good Spring threading with a TaskExecutor examples? 我看了一个带有TaskExecutor示例的良好Spring线程? but I couldn't figure out where the original TaskExecutor came from, like how would I pass that into the class? 但我不知道原始TaskExecutor来源,例如如何将其传递给班级?

Also is TaskExecutor the best thing to use for just creating another thread in my program? 在仅在程序中创建另一个线程时, TaskExecutor还是最好的选择吗?

The TaskExecutor is defined in the xml spring beans definition and passed as constructor's argument to the utility class. TaskExecutor在xml spring bean定义中定义,并作为构造函数的参数传递给实用程序类。

 <constructor-arg type="org.springframework.core.task.TaskExecutor" ref="taskExecutor" />

If you have the choice, you should definitely work with TaskExecutor and Threadpool instead of managing your thread the old way. 如果您有选择,则绝对应该使用TaskExecutor和Threadpool,而不是用旧方法管理线程。

The post that you mention gives a good example of what you want to do. 您提到的帖子很好地说明了您想做什么。 taskExecutor is injected via the constructor. taskExecutor通过构造函数注入。 Checkout the spring beans configuration given in that post. 检查该帖子中给出的spring bean配置。 I believe it is a good practice to use TaskExecutor. 我相信使用TaskExecutor是一个好习惯。

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

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