码迷,mamicode.com
首页 > 编程语言 > 详细

spring配置线程池

时间:2015-06-24 16:52:06      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

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

    spring的配置如下

<?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代码

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(BaseAsyncRunner baseRunner){
        executorService.execute(baseRunner);
    }
    
    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();
    }
}

Runner(任务)代码

public class Runner extends BaseAsyncRunner {
       @Override
    public void runrunrun() {
        //业务代码
    }    
}

测试(调用)代码

public class Test{
    @Autowired
    private  AsyncExecutorService asyncExecutorService;
    public void startTask(){
        Runner runner = new Runner();
        asyncExecutorService.execute(runner);   
    }
    
}


spring配置线程池

标签:

原文地址:http://my.oschina.net/shyloveliyi/blog/470270

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!