定时任务

来源:互联网 发布:2016黑马java百度网盘 编辑:程序博客网 时间:2024/06/05 13:22
<task:annotation-driven />
<!--  注册bean  -->
<bean id="expireJob" class="lb.rest.timer.ExpireJob"></bean>   
    <bean id="siteSettleJob" class="lb.rest.timer.SiteSettleJob"></bean>  
<bean id="interestsJob" class="lb.rest.timer.InterestsJob"></bean>  
<bean id="shouldPayableJob" class="lb.rest.timer.ShouldPayableJob"></bean>  
    <!--  开启任务调度  -->
    <task:scheduled-tasks> 
        <task:scheduled ref="interestsJob" method="run" cron="0 0 1 * * ?" />  
        <!-- 
        <task:scheduled ref="expireJob" method="run" cron="0 30 1 * * ?" />  
         -->
        <task:scheduled ref="expireJob" method="run" cron="0 0 3 * * ?" />  
        <task:scheduled ref="siteSettleJob" method="run" cron="0 0 2 * * ?" /> 
        <task:scheduled ref="shouldPayableJob" method="run" cron="0 0 */6 * * ?" />            
    </task:scheduled-tasks>  

<context:component-scan base-package="lb.rest.timer"/>  



////////////////////////////////////////////////////////////////

package lb.rest.timer;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


import org.springframework.beans.factory.annotation.Autowired;


import com.mlb.exception.WebApiException;


import fc.wpf.rest.web.bean.ReturnInfo;
import lb.rest.bank.db.dao.InterestDao;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class InterestsJob {


    @Autowired
private InterestDao interestDao;


    public ReturnInfo run() {
    log.info("定时任务开始!");
        try {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Date nowDate = new Date();
        Date strBeginDate = this.getDate(nowDate, -1);
        Date beginDate = sf.parse(sf.format(strBeginDate));
        Date endDate = sf.parse(sf.format(nowDate));
        log.info("利息计算的期间------起始时间:{},结束时间:{}",beginDate,endDate);
        interestDao.calculateInterest(beginDate,endDate);
        } catch(WebApiException w){
        return new ReturnInfo(w.getMessage(),1000,null,false);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnInfo.Faild;
        }
        
        return ReturnInfo.Faild;
    }
    
    /*计算获取时间
     * beginDate 起始时间
     * inrDate 增加或减少时间 -inrDate表示减少时间
     */    
  private Date getDate(Date beginDate,int inrDate){
      if(beginDate != null){
      Calendar calendar = new GregorianCalendar(); 
      calendar.setTime(beginDate); 
      calendar.add(Calendar.DATE,inrDate);
      Date afterDate=calendar.getTime();
      return afterDate;
      }else{
      return new Date();
      }
      }
    
}

原创粉丝点击