1. 程式人生 > >springMVC配置定時任務使用案例

springMVC配置定時任務使用案例

定時任務常常是資料統計必不可少的任務處理方法,這裡介紹的是quartz定時任務外掛的配置和使用案例,使用場景是定時給餘額不足的使用者傳送簡訊通知;關於簡訊通知的設計和實現如下:

1.在applicationContext.xml配置載入定時任務觸發器的spring-quartz.xml配置,如:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <!-- 資原始檔 -->
    <!-- 引入配置 -->
    <bean class="com./.prop.EncryptProperty">
        <property name="locations">
            <list>
                <value>classpath:config/cloud.properties</value>
            </list>
        </property>
    </bean>

    <bean id="appContext" lazy-init="false" class="com./.spring.AppContext"/>


    <!-- 匯入其他模組的配置 -->
    <import resource="classpath:spring/*.xml"/>
    <bean class="com./.cms.util.SysCodeUtil"></bean>
    <bean class="com./.cms.util.SysThirdUserUtil"></bean>
    <bean class="com./.cms.util.ConfigParamUtil"></bean>

</beans>  

2.在 spring-quartz.xml中配置定時任務的掃描路徑及其觸發器引數配置:

<?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: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/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/task 
      http://www.springframework.org/schema/task/spring-task.xsd">
    <!-- 定時任務掃描路徑 -->
    <context:component-scan base-package="com./.cms.quartz"/>

    <task:executor id="executor" pool-size="5"/>
    <task:scheduler id="scheduler" pool-size="10"/>
    <task:annotation-driven executor="executor"
                            scheduler="scheduler"/>
</beans>

3.對應的com./.cms.quartz包下的任務例項如下:

package com./.cms.quartz;

import com./.cms.bo.DriverAccountBO;
import com./.cms.dao.DriverAccountDao;
import com./.cms.service.DriverAccountIsEnoughService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chengaofeng5
 * @Description:賬戶餘額是否足額(不小於0)狀態更新定時任務
 * @date 2018年10月17日 15:27
 */

@Component
@Transactional
public class DriverAccountIsEnoughTask {

   private final Logger log = LoggerFactory
         .getLogger(DriverAccountIsEnoughTask.class);

   /** CPU核心數 */
   private int cpuCoreNum = 4;

   @Autowired
   private DriverAccountDao dao;
   @Resource
   private DriverAccountIsEnoughService service;

   /**
    * @Description: 定義定時任務每天0點2分執行一次,查詢要更新狀態的賬戶
    * @author 
    * @date 2018年10月17日 17:16:01
    *
    */
   //@Scheduled(cron = "0 0/2 * * * ?")
   @Scheduled(cron = "0 2 0 ? * *")
   public void updateDriverAmountStatus() {
      log.info("DriverAccountIsEnoughTask updateDriverAmountStatus begin...");
      try {
         // 查詢待更新的記錄併發簡訊
         List<DriverAccountBO> accountList = dao.selectUpdateList();
         if (!accountList.isEmpty()) {
            service.sendMethods(accountList);
            // 簡訊通知成功,更新正常->欠費的狀態
            dao.updateAmountStatus1();
         }
         // 更新欠費->正常的狀態
         dao.updateAmountStatus0();

      } catch (Exception e) {
         log.error("賬戶餘額狀態定時任務更新異常", e);
      }

      log.info("DriverAccountIsEnoughTask updateDriverAmountStatus end...");
   }

}

4.service處理簡訊邏輯為:如果amount<=0且amount_status=0的記錄傳送簡訊,否則amount>0且amount_status=1的記錄更新狀態,其中 `amount_status` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '賬戶餘額狀態: 0 正常(賬戶餘額不小於0) 1欠費',`amount` int(11) NOT NULL COMMENT '賬戶餘額:單位分';處理程式碼如下:

/**
 * @author 
 * @Description:賬戶餘額是否充足實時更新方法
 * @date 2018年10月18日 10:53
 */
@Service
public class DriverAccountIsEnoughServiceImpl implements
      DriverAccountIsEnoughService {

   protected static final Logger log = Logger
         .getLogger(DriverAccountIsEnoughServiceImpl.class);

   // 賬戶餘額狀態: 0 正常(賬戶餘額不小於0) 1欠費
   private final static byte amount_status_0 = (byte) 0;
   private final static byte amount_status_1 = (byte) 1;

   /**
    * @Description: 批量處理所有的賬戶記錄
    * @author 
    * @date 
    * @param accountList
    * @return
    *
    */
   @Override
   public void sendMethods(List<DriverAccountBO> accountList) {

      // 判斷記錄條件傳送簡訊
      for (DriverAccountBO da : accountList) {
         // 傳送簡訊條件為:amount<=0且amount_status=0的記錄(此判斷可去掉,在查詢的時候已處理),且不為免傳送簡訊的手機號
         Boolean isSend = ((da.getAmount() <= 0 && da.getAmountStatus()
               .equals(amount_status_0)))
               && null != da.getMobilePhone()
               && !da.getMobilePhone().equals(
                     SysCodeUtil.getString("ignoreMobilePhone"));
         if (isSend) {
            // 發放簡訊方法
            this.sendSubmial(da.getMobilePhone());
         }
      }

   }

}

其中,傳送簡訊的記錄查出來,即dao層selectUpdateList方法其查詢語句如下:

<!--查詢所有待簡訊通知的賬戶記錄-->
<select id="selectUpdateList" resultType="com.hikvision.cms.bo.DriverAccountBO">
    <![CDATA[

    SELECT DISTINCT
        a.account_id, a.driver_id, a.user_id, a.amount, a.create_time, a.amount_status,
        b.mobile_phone
    FROM driver_account a
    LEFT JOIN sys_user b ON b.user_id=a.user_id
    WHERE amount <= 0  AND amount_status = 0

    ]]>
</select>

簡訊通知成功,更新正常->欠費的狀態updateAmountStatus1方法sql為:

<!--更新使用者賬戶記錄為1的狀態-->
<update id="updateAmountStatus1">
    <![CDATA[

    update driver_account
    set amount_status = 1
    where amount <= 0 AND amount_status = 0

    ]]>
</update>

 更新欠費->正常狀態的updateAmountStatus0方法sql為:

<!--更新使用者賬戶記錄為0的狀態-->
<update id="updateAmountStatus0">
    <![CDATA[

    update driver_account
    set amount_status = 0
    where amount > 0 AND amount_status = 1

    ]]>
</update>

至此,定時任務傳送簡訊的功能編碼邏輯結束...哈哈哈,是不是很簡單?