弹簧和线程:TaskExecutor
在Web應(yīng)用程序中使用線程并不常見,尤其是當(dāng)您必須開發(fā)長期運(yùn)行的任務(wù)時(shí)。
考慮到spring,我們必須格外注意并使用它已經(jīng)提供的工具,而不是生成我們自己的線程。 我們希望線程由spring管理,因此能夠在不產(chǎn)生任何影響的情況下使用應(yīng)用程序的其他組件,并在不進(jìn)行任何工作的情況下優(yōu)雅地關(guān)閉應(yīng)用程序。
Spring提供TaskExecutor作為與執(zhí)行程序打交道的抽象。 Spring的TaskExecutor接口與java.util.concurrent.Executor接口相同。 Spring發(fā)行版中包含許多TaskExecutor的預(yù)構(gòu)建實(shí)現(xiàn),您可以從官方文檔中找到有關(guān)它們的更多信息。 通過為您的Spring環(huán)境提供TaskExecutor實(shí)現(xiàn),您將能夠?qū)askExecutor注入到您的bean中并可以訪問托管線程。
package com.gkatzioura.service;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.core.task.TaskExecutor; import org.springframework.stereotype.Service; import java.util.List;/*** Created by gkatzioura on 4/26/17.*/ @Service public class AsynchronousService {@Autowiredprivate ApplicationContext applicationContext;@Autowiredprivate TaskExecutor taskExecutor;public void executeAsynchronously() {taskExecutor.execute(new Runnable() {@Overridepublic void run() {//TODO add long running task}});} }第一步是將TaskExecutor配置添加到我們的spring應(yīng)用程序中。
package com.gkatzioura.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.task.TaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;/*** Created by gkatzioura on 4/26/17.*/ @Configuration public class ThreadConfig {@Beanpublic TaskExecutor threadPoolTaskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(4);executor.setMaxPoolSize(4);executor.setThreadNamePrefix("default_task_executor_thread");executor.initialize();return executor;}}一旦我們的執(zhí)行程序設(shè)置完成,過程就很簡單。 我們將執(zhí)行程序注入到spring組件中,然后提交包含要執(zhí)行任務(wù)的Runnable類。
由于我們的異步代碼可能還需要與應(yīng)用程序的其他組件進(jìn)行交互并注入它們,因此一種不錯(cuò)的方法是創(chuàng)建原型范圍內(nèi)的可運(yùn)行實(shí)例。
package com.gkatzioura;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component;/*** Created by gkatzioura on 10/18/17.*/ @Component @Scope("prototype") public class MyThread implements Runnable {private static final Logger LOGGER = LoggerFactory.getLogger(MyThread.class);@Overridepublic void run() {LOGGER.info("Called from thread");} }然后,我們準(zhǔn)備將執(zhí)行程序注入到我們的服務(wù)中,并使用它來執(zhí)行可運(yùn)行實(shí)例。
package com.gkatzioura.service;import com.gkatzioura.MyThread; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.core.task.TaskExecutor; import org.springframework.stereotype.Service;import java.util.List;/*** Created by gkatzioura on 4/26/17.*/ @Service public class AsynchronousService {@Autowiredprivate TaskExecutor taskExecutor;@Autowiredprivate ApplicationContext applicationContext;public void executeAsynchronously() {MyThread myThread = applicationContext.getBean(MyThread.class);taskExecutor.execute(myThread);}}在下一篇文章中,我們將通過使用spring的異步函數(shù)將多重讀取的代碼庫提高到一個(gè)新的水平。
您可以在github上找到源代碼。
翻譯自: https://www.javacodegeeks.com/2017/10/spring-threads-taskexecutor.html
總結(jié)
以上是生活随笔為你收集整理的弹簧和线程:TaskExecutor的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wps行统计(wps表格做数据统计)
- 下一篇: 电脑如何复制页面设置快捷键(怎么设置电脑