简体   繁体   中英

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.

I looked at Any good Spring threading with a TaskExecutor examples? but I couldn't figure out where the original TaskExecutor came from, like how would I pass that into the class?

Also is TaskExecutor the best thing to use for just creating another thread in my program?

The TaskExecutor is defined in the xml spring beans definition and passed as constructor's argument to the utility class.

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

The post that you mention gives a good example of what you want to do. taskExecutor is injected via the constructor. Checkout the spring beans configuration given in that post. I believe it is a good practice to use TaskExecutor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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