`
pavel
  • 浏览: 915578 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring异步任务

阅读更多

<!-- 任务从此处开始加载 -->        

<bean id="notifySpringScheduledExecutorFactoryBean" class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean">            <property name="scheduledExecutorTasks">        

  1.         <list>        
  2.             <ref bean="notifySpringScheduledExecutorTask" />        
  3.         </list>        
  4.     </property>        
  5. </bean>        
  6. <!-- 待加入Spring Schedual进行调度的task列表 -->        
  7. <bean id="notifySpringScheduledExecutorTask" class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">        
  8.     <property name="runnable" ref="notifyScheduledMainExecutor" />        
  9.     <!-- 初次执行任务delay时间,单位为ms,默认值为0,代表首次加载任务时立即执行;比如1min -->        
  10.     <property name="delay" value="60000" />        
  11.     <!-- 间隔时间,单位为ms,默认值为0,代表任务只执行一次;比如2min -->        
  12.     <property name="period" value="120000" />        
  13.     <!-- 是否采用fixedRate方式进行任务调度,默认为false,即采用fixedDelay方式 -->        
  14.     <!-- fixedRate:定时间隔执行,不管上次任务是否已执行完毕;fixedDelay:每次任务执行完毕之后delay固定的时间 -->        
  15.     <property name="fixedRate" value="true" />        
  16. </bean>        
  17. <!-- 任务调度主线程 -->        
  18. <bean id="notifyScheduledMainExecutor" class="com.alisoft.aep.notify.schedual.NotifyScheduledMainExecutor">        
  19.     <!-- 针对Notify服务端的Service,用于更新Notify重试信息等 -->        
  20.     <property name="notifyServerService" ref="notifyServerService" />        
  21.     <!-- notify.notifyId缓存策略实现类,可自行扩展 -->        
  22.     <property name="notifyIdCacheStrategy" ref="defaultNotifyIdCacheStrategy" />        
  23.     <!-- notify.load_balance_num字段值生成、以及调度时where条件中取值的策略实现类,可自行扩展 -->        
  24.     <!-- 当有多台notify服务器时才有用,用于平衡各台server间的压力;一般不用配置 -->        
  25.     <!-- <property name="loadBalanceNumStrategy" ref="alternateLoadBalanceNumStrategy" /> -->        
  26.     <!-- notify.handler字段值在调度时where条件中取值的策略实现类,可自行扩展 -->        
  27.     <!-- 当有多台notify服务器时才有用,用于表明某台server可执行哪些handler;一般不用配置 -->        
  28.     <!-- <property name="notifyHandlerStrategy" ref="defaultNotifyHandlerStrategy" /> -->        
  29.     <!-- 当有多台notify服务器时才有用,用于设置某台server调度时每次读取的Notify最大数,用于覆盖maxNum;一般不用配置 -->        
  30.     <!-- <property name="notifyMaxNumPerJobStrategy" ref="defaultNotifyMaxNumPerJobStrategy" /> -->        
  31.     <!-- 用于并发的线程池 -->        
  32.     <property name="notifyTaskExecutor" ref="notifyTaskExecutor" />        
  33.     <!-- 每次调度读取的Notify最大记录数,默认为1000 -->        
  34.     <property name="maxNum" value="1000" />        
  35.     <property name="notifyDao" ref="notifyDao" />        
  36. </bean>        
  37.                  
  38. <!-- 异步线程池 -->        
  39. <bean id="notifyTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">        
  40.     <!-- 核心线程数,默认为1 -->        
  41.     <property name="corePoolSize" value="10" />        
  42.     <!-- 最大线程数,默认为Integer.MAX_VALUE -->        
  43.     <property name="maxPoolSize" value="50" />        
  44.     <!-- 队列最大长度,一般需要设置值>=notifyScheduledMainExecutor.maxNum;默认为Integer.MAX_VALUE -->        
  45.     <property name="queueCapacity" value="1000" />        
  46.     <!-- 线程池维护线程所允许的空闲时间,默认为60s -->        
  47.     <property name="keepAliveSeconds" value="300" />        
  48.     <!-- 线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy;默认为后者 -->        
  49.     <property name="rejectedExecutionHandler">        
  50.         <!-- AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 -->        
  51.         <!-- CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,可以有效降低向线程池内添加任务的速度 -->        
  52.         <!-- DiscardOldestPolicy:抛弃旧的任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->        
  53.         <!-- DiscardPolicy:抛弃当前任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->        
  54.         <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />        
  55.     </property>        
  56. </bean>        
  57. <bean id="notifyServerService" class="com.alisoft.aep.notify.service.impl.NotifyServerServiceImpl">        
  58.     <!-- 针对任务执行失败后Notify如何重试的策略实现类,可自行扩展 -->        
  59.     <property name="notifyRetryStrategy" ref="defaultNotifyRetryStrategy" />        
  60.     <!-- 针对任务执行失败后异常处理策略实现类,可自行扩展 -->        
  61.     <!-- 默认不对异常进行补救,具体handler实现类中若返回NULL或抛出异常,则均按异常处理,直接将Notify记录迁移到历史表中,不进行重试; -->        
  62.     <!-- <property name="notifyHandlerExceptionStrategy" ref="defaultNotifyHandlerExceptionStrategy" /> -->        
  63.     <!-- 描述见notifyScheduledMainExecutor -->        
  64.     <property name="notifyIdCacheStrategy" ref="defaultNotifyIdCacheStrategy" />        
  65.     <!-- 事务模板,需保证能够找到对应的bean -->        
  66.     <property name="transactionTemplate" ref="transactionTemplate" />        
  67.     <property name="notifyDao" ref="notifyDao" />        
  68. </bean>   

 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sfdev/archive/2009/04/08/4056114.aspx

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics