关于对异步任务执行的操作

异步任务执行的操作

一直对公司的批量任务执行保持着神秘的态度,感觉总不是现在的自己能掌握的,早上突然从掘金上看一篇关于异步任务执行的讲解,突然感觉这不就是公司同事所说的关于批量的东西嘛。然后趁着时间充足研究了一把,在好基友的帮助下,终于把他的神秘面纱揭开,现在就开始走入我们的异步执行任务中,当然可能本人技能才疏学浅,理解的不是很深入,希望大家多多指出共同进步。
首先使用异步任务执行我们需要在xml文件中配置好任务执行器的使用。

  • 两个xml 这个xml名称可以写readTask.xml
   <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns="http://www.springframework.org/schema/beans"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd ">
    <context:component-scan base-package="com.taotao.task" />
    <import resource="taskScan.xml"/>

    <!-- 读取配置文件 -->
    <bean id="propertyConfig"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:taskConfig.properties</value>
        </property>
        <property name="fileEncoding" value="utf-8" />
    </bean>

    <bean id="TaskDealExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
      <!-- 设置最大线程数目 -->  
      <property name="maxPoolSizeNum" value="${task.thread.pool.maxPoolSizeNum}" />    
      <!-- 设置核心线程数数目 -->     
      <property name="corePoolSizeNum" value="${task.thread.pool.corePoolSizeNum}" />

      <!--设置队列最大长度-->
      <property name="queueCapacityLength" value="${task.thread.pool.queueCapacityLength}" />
      <!-- 线程池维护线程所允许的空闲时间,默认为60s -->
      <property name="keepAliveSecondsTime" value="${task.thread.pool.keepAliveSecondsTime}" />
    </bean>
    <!-- 使用注解 -->
    <task:annotation-driven />

</beans> 
        <!--我们在使用的一些配置都是在配置文件中就写好的可以自己单独配置value值都是可以改变的-->
  • taskScan.xml 内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/task 
       http://www.springframework.org/schema/task/spring-task.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <task:scheduled-tasks>
        <task:scheduled ref="scanerTask" method="scan" cron="0 0/1 * * * ?" />
    </task:scheduled-tasks>
</beans>

第二个xml文件中这个是Spring中的一个设定时间自动任务调度 参数为

  1. ref是工作类 就是执行的类名称

  2. method是工作类中要执行的方法

  3. initial-delay是任务第一次被调用前的延时,单位毫秒

  4. fixed-delay是上一个调用完成后再次调用的延时

  5. fixed-rate是上一个调用开始后再次调用的延时(不用等待上一次调用完成)

  6. cron是表达式,表示在什么时候进行任务调度
    我们这里根据cron表达式设置时间 表达式规则可以看

http://www.cnblogs.com/mingyue1818/p/5764050.html 下面规则来自这个博客

下面只是举例说明

一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。
按顺序依次为
秒(0~59)
分钟(0~59)
小时(0~23)
天(月)(0~31,但是你需要考虑你月的天数)
月(0~11)
天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)

7.年份(1970-2099)
其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。
由于”月份中的日期”和”星期中的日期”这两个元素互斥的,必须要对其中一个设置.

“0 0 10,14,16 * * ?” 每天上午10点,下午2点,4点
“0 0/30 9-17 * * ? ” 朝九晚五工作时间内每半小时
“0 0 12 ? * “WED 表示每个星期三中午12点
“0 0 12 * * ?” 每天中午12点触发
“0 15 10 ? * *” 每天上午10:15触发
“0 15 10 * * ?” 每天上午10:15触发
“0 15 10 * * ? *” 每天上午10:15触发
“0 15 10 * * ? 2005” 2005年的每天上午10:15触发
“0 * 14 * * ?” 在每天下午2点到下午2:59期间的每1分钟触发
“0 0/5 14 * * ?” 在每天下午2点到下午2:55期间的每5分钟触发
“0 0/5 14,18 * * ?” 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
“0 0-5 14 * * ?” 在每天下午2点到下午2:05期间的每1分钟触发
“0 10,44 14 ? 3 WED” 每年三月的星期三的下午2:10和2:44触发
“0 15 10 ? * MON-FRI” 周一至周五的上午10:15触发
“0 15 10 15 * ?” 每月15日上午10:15触发
“0 15 10 L * ?” 每月最后一日的上午10:15触发
“0 15 10 ? * 6L” 每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6L 2002-2005” 2002年至2005年的每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6#3” 每月的第三个星期五上午10:15触发

我们的执行时间是每天的每分钟的执行。相当于时时刻刻都在执行。扫描的任务。

下面开始说我们的具体代码执行

<!--每个代码都需要继承的任务执行器,只是一个简单的接口-->

public interface TaskDealExecutor {
    public void execute() throws Exception;
}

所有实现该任务执行器的都实现其接口。让其加入到任务执行中,然后根据任务扫描的类去扫描具体的任务执行并且在数据库当中将claas路径加载好,我们会在任务执行器的class中获取到该任务当中可以执行的类,
然后去遍历该类。
因为我们在配置文件中已经配置好该任务执行器,多长时间执行一次,那么到达指定时间后该任务执行器会自动的去执行该类。
这里我们使用的Spring中的任务执行接口

public interface TaskExecutor extends Executor {

    /**
     * Execute the given {@code task}.
     * <p>The call might return immediately if the implementation uses
     * an asynchronous execution strategy, or might block in the case
     * of synchronous execution.
     * @param task the {@code Runnable} to execute (never {@code null})
     * @throws TaskRejectedException if the given task was not accepted
     */
    @Override
    void execute(Runnable task);

}

在我们的代码中使用自动注入上蒙面的执行器加入到我们的任务当中,根据查询到数据库中的bean进行任务执行。

taskExecutor.execute(new TaskInvokerProcess(task));

TaskInvokerProcess是一个实现了Runnable接口的线程类。在run方法中我们会执行具体业务操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值