前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring配置线程池-异步执行任务 原

spring配置线程池-异步执行任务 原

作者头像
尚浩宇
发布2018-08-17 09:49:54
9260
发布2018-08-17 09:49:54
举报
文章被收录于专栏:杂烩杂烩

    系统中有个定时器,针对每个用户定时生成报告。但是每个报告需要消耗3~5秒,所以在定时器里不能去处理,然后就想到线程池,在定时器里只需要启动线程就行了,所有业务全在另起的线程里进行。

    spring的配置如下

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
     http://cxf.apache.org/jaxws  
     http://cxf.apache.org/schemas/jaxws.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-3.1.xsd">
    <-- 队列集合 -->
    <bean id="blockingQueue" class="java.util.concurrent.ArrayBlockingQueue">
        <constructor-arg index="0" value="1"/>
    </bean>
    <-- 线程池 -->
    <bean id="threadPoolExecutor" class ="java.util.concurrent.ThreadPoolExecutor">
        <constructor-arg index="0" value="1"/>
        <constructor-arg index="1" value="5"/>
        <constructor-arg index="2" value="2000"/>
        <constructor-arg index="3" value="SECONDS" type="java.util.concurrent.TimeUnit"  />
        <constructor-arg index="4" ref="blockingQueue" type="java.util.concurrent.BlockingQueue"/>
    </bean>
    <bean id="asyncExecutorService" class="com.test.AsyncExecutorService" init-method="init"  destroy-method="destroy">
        <property name="executorService" ref="threadPoolExecutor"/>
        <property name="blockingQueue" ref="blockingQueue"/>
        <property name="rejectionHandler" class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
    </bean>
</beans>

java代码

代码语言:javascript
复制
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class AsyncExecutorService {
    private ThreadPoolExecutor executorService;
    private RejectedExecutionHandler rejectionHandler;
    private BlockingQueue<Runnable> blockingQueue;
    private int defaultCoreSize;
    private int defaultMaxSize;
    private long defaultKeepAliveTime;
    public RejectedExecutionHandler getRejectionHandler() {
        return rejectionHandler;
    }
    public void setRejectionHandler(RejectedExecutionHandler rejectionHandler) {
        this.rejectionHandler = rejectionHandler;
        executorService.setRejectedExecutionHandler(rejectionHandler);
    }
    public BlockingQueue<Runnable> getBlockingQueue() {
        return blockingQueue;
    }
    public void setBlockingQueue(BlockingQueue<Runnable> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }
    public ThreadPoolExecutor getExecutorService() {
        return executorService;
    }
    public void setExecutorService(ThreadPoolExecutor executorService) {
        this.executorService = executorService;
        this.defaultCoreSize = this.executorService.getCorePoolSize();
        this.defaultMaxSize = this.executorService.getMaximumPoolSize();
        this.defaultKeepAliveTime = this.executorService.getKeepAliveTime(TimeUnit.SECONDS);
    }
    
    
    public void execute(Runner runner){
        executorService.execute(runner);
    }
    
    public void resizePoolMaxSize(int maxSize){
        this.executorService.setMaximumPoolSize(maxSize);
    }
    public void resizePoolCoreSize(int coreSize){
        this.executorService.setMaximumPoolSize(coreSize);
    }
    public void resetPoolAliveTime(long aliveTime){
        this.executorService.setKeepAliveTime(aliveTime, TimeUnit.SECONDS);
    }
    
    public void resetDefault(){
        this.executorService.setCorePoolSize(this.defaultCoreSize);
        this.executorService.setMaximumPoolSize(this.defaultMaxSize);
        this.executorService.setKeepAliveTime(this.defaultKeepAliveTime, TimeUnit.SECONDS);
    }
    
    @PostConstruct
    public void init(){
        //asyncExecutorService will be init !!!
    }
    @PreDestroy
    public void destroy(){
        //asyncExecutorService will be destroyed !!!
        executorService.shutdown();
    }
    public Integer getPoolMaxSize() {
        return executorService.getMaximumPoolSize();
    }
}

测试(调用)代码

代码语言:javascript
复制
public class Test{
    @Autowired
    private  AsyncExecutorService asyncExecutorService;
    public void startTask(){
       asyncExecutorService.execute(()->{
             syso("run");
        });   
    }
    
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015/06/24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档