taskexecutor_弹簧和线程:TaskExecutor
taskexecutor
在Web應用程序中使用線程并不罕見,尤其是當您必須開發長期運行的任務時。
考慮到spring,我們必須格外注意并使用它已經提供的工具,而不是生成我們自己的線程。 我們希望線程由spring管理,因此能夠在沒有任何影響的情況下使用應用程序的其他組件,并在不進行任何工作的情況下優雅地關閉應用程序。
Spring提供TaskExecutor作為與執行程序打交道的抽象。 Spring的TaskExecutor接口與java.util.concurrent.Executor接口相同。 Spring發行版中包含許多TaskExecutor的預構建實現,您可以從官方文檔中找到有關它們的更多信息。 通過為您的Spring環境提供TaskExecutor實現,您將能夠將TaskExecutor注入到您的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應用程序中。
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;}}一旦我們的執行程序設置完成,過程就很簡單。 我們將執行程序注入到spring組件中,然后提交包含要執行任務的Runnable類。
由于我們的異步代碼可能還需要與應用程序的其他組件進行交互并注入它們,因此一種不錯的方法是創建原型范圍內的可運行實例。
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");} }然后,我們準備將執行程序注入到我們的服務中,并使用它來執行可運行的實例。
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的異步函數將多重讀取的代碼庫提高到一個新的水平。
您可以在github上找到源代碼。
翻譯自: https://www.javacodegeeks.com/2017/10/spring-threads-taskexecutor.html
taskexecutor
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的taskexecutor_弹簧和线程:TaskExecutor的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 体组词 体组词有哪些
- 下一篇: 没去掉三点水是什么字 没去掉三点水应该是