spring 线程之TaskExecutor

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/neweastsun/article/details/83152610

spring 线程之TaskExecutor

web应用程序中使用线程普遍,尤其当你需要开发非常耗时的任务。在使用spring时需要强调,尽量使用其已经提供的功能,而不要从头自己实现。

我们希望线程由Spring管理,这样就可以不受任何影响地使用应用程序中其他组件。还可以优雅地关闭应用程序,而不需要进行任何其他工作。

spring 提供TaskExecutor 做处理执行器的抽象。spring 的TaskExecutor 接口等同于java.util.concurrent.Executor interface接口。在sping中有很多预定义的TaskExecutor实现。

配置TaskExecutor 实现

首先我们增加TaskExecutor 配置,定义TaskExecutor 的实现类:

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;

@Configuration
public class ThreadConfig {
    @Bean
    public TaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(4);
        executor.setMaxPoolSize(4);
        executor.setThreadNamePrefix("default_task_executor_thread");
        executor.initialize();
        return executor;
    }
}

注入TaskExecutor

配置TaskExecutor的实现后,则可以注入TaskExecutor至bean中并访问托管线程。

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;

@Service
public class AsynchronousService {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private TaskExecutor taskExecutor;
    public void executeAsynchronously() {
        taskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                //TODO add long running task
            }
        });
    }
}

定义任务类

配置好executor 之后,处理过程就简单了。即在spring组件中注入executor,然后提交包含可执行任务的Runnable类。

既然异步代码可能需要和我们应用中的其他组件进行交互(已注入),最好方法是定义为原型实例。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
public class MyThread implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyThread.class);
    @Override
    public void run() {
        LOGGER.info("Called from thread");
    }
}

异步执行任务

下面示例注入executor至业务类并异步执行任务:

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;

@Service
public class AsynchronousService {
    @Autowired
    private TaskExecutor taskExecutor;
    @Autowired
    private ApplicationContext applicationContext;
    public void executeAsynchronously() {
        MyThread myThread = applicationContext.getBean(MyThread.class);
        taskExecutor.execute(myThread);
    }
}

总结

本文介绍了在spring中如何定义TaskExecutor,通过TaskExecutor异步执行任务。学习本文可以更好地理解spring的异步注解,更简化方式实现异步处理任务。可以参考spring 使用@Async注解实现异步执行

猜你喜欢

转载自blog.csdn.net/neweastsun/article/details/83152610
今日推荐