Asyn4j是一个开源的异步任务管理框架,关于该框架的使用,网上有很多的例子可以查看,这里不再赘述。由于项目需要,我对该框架进行了封装和改进,改进点就是通过和spring的结合,将线程池、异步队列、线程池的配置都放在配置文件中;除此之外,该框架有一个问题就是如果存在的相同类型的任务过多,成百上千个,那么这些任务就会阻塞后续的任务执行,该框架也解决了该问题。就是通过配置不同的线程池,通过设置属性excuteType,如果设置为byClass那么该类的操作都放在一个线程池中,如果设置为byMethod那么只有该方法的操作放在线程池中执行。其他补多少,直接上代码。
首先是配置文件:
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName" default-lazy-init="true"> <bean id="springBeanUtil" class="com.inspur.incloud.common.util.AsynSpringUtil" autowire="byName" lazy-init="false" /> <bean id="dbJobServiceAsynHandler" class="com.inspur.incloud.common.util.asynjob.core.handler.DbJobServiceAsynHandler" /> <bean id="fileAsynServiceHandler" class="com.inspur.incloud.common.util.asynjob.core.handler.FileAsynServiceHandler"/> <bean id="cacheWorkQueueFullHandler" class="com.inspur.incloud.common.util.asynjob.core.handler.CacheAsynWorkHandler"> <constructor-arg name="maxLength" value="400" ></constructor-arg> </bean> <bean id="asynJobService" init-method="getObject" lazy-init="false" class="com.inspur.incloud.common.util.asynjob.spring.AsynServiceFactoryBean" > <!-- 异步服务器名称 --> <property name="serviceName" value="iresourceJobService"></property> <!-- 最大工作队列缓存工作数 --> <property name="maxCacheWork" value="3000"></property> <!-- 当工作队列满时添加工作等待时间 --> <property name="addWorkWaitTime" value="2000"></property> <!-- 异步工作执行线程池大小 默认:(cpu核数/2)+1 --> <property name="workThreadNum" value="100"></property> <!-- 回调执行线程池大小 默认:cpu核数/2 --> <property name="callbackThreadNum" value="100"></property> <property name="operateMap"> <map> <!-- 虚拟机操作的线程最大允许个数 --> <entry key="com.inspur.incloud.common.util.asynjob.util.TargetService" value-ref="vmOpExcute"/> </map> </property> <!-- 是否允许负载均衡 --> <property name="allowLB" value="true"></property> <property name="workQueueFullHandler" ref="cacheWorkQueueFullHandler" /> <!-- 任务持久化处理器 --> <property name="jobServiceAsynHandler" ref="dbJobServiceAsynHandler" /> <!-- 服务启动和关闭处理器 --> <property name="serviceHandler" ref="fileAsynServiceHandler" /> </bean> <bean id="vmOpExcute" class="com.inspur.incloud.common.util.asynjob.core.ExcutePool"> <property name="name" value="openVmThread" /> <!--有两种可供选择 byClass or byMethod, 不区分大小写--> <property name="excuteType" value="byClass" /> <property name="maxCacheWork" value="100" /> <property name="workThreadNum" value="100" /> <!-- 当工作队列满时添加工作等待时间 --> <property name="addWorkWaitTime" value="2000"></property> <property name="callbackThreadNum" value="100" /> <property name="workQueueSize" value="100" /> </bean> </beans>
由于机密需要,只上传了该包的代码,有问题可以直接留言