首页 > 代码库 > spring的定时任务
spring的定时任务
首先配置一个定时任务,0 35 13 * * *代表每天的下午13:25分到class="com.qianmo.qmyj.cache.impl.AutoTransImpl"执行定时任务
1 <!--开启任务注解扫描--> 2 <task:annotation-driven/> 3 <bean id="autoTransTask" class="com.qianmo.qmyj.cache.impl.AutoTransImpl"/> 4 <task:scheduled-tasks> 5 <!--此计划任务用于用户不给好评,从工人点击完工(7日后)自动 5星好评 并且 转账--> 6 <task:scheduled ref="autoTransTask" method="executeAutoTrans" cron="0 35 13 * * *"/> 7 </task:scheduled-tasks> 8 9 <!-- 启用spring事务处理(同一个service中有一条数据库操作失败,之前的方法全部回滚) --> 10 <tx:annotation-driven transaction-manager="transactionManager"/>
/* * Package com.qianmo.qmyj.cache * FileName: AutoTransImpl * Author: anpei * Date: 2017/5/7 2:44 */ package com.qianmo.qmyj.cache.impl; import com.qianmo.qmyj.bean.dto.Evaluation; import com.qianmo.qmyj.bean.dto.OrderInfo; import com.qianmo.qmyj.bean.dto.UserInfo; import com.qianmo.qmyj.cache.AutoTrans; import com.qianmo.qmyj.common.Constants; import com.qianmo.qmyj.common.Constants.autoTrans; import com.qianmo.qmyj.common.Constants.orderFinishStat; import com.qianmo.qmyj.common.Constants.orderStat; import com.qianmo.qmyj.dao.EvaluationDao; import com.qianmo.qmyj.dao.OrderInfoDao; import com.qianmo.qmyj.dao.UserInfoDao; import com.qianmo.qmyj.framework.ForMartUtil; import com.qianmo.qmyj.framework.util.DataVerifyUtil; import com.qianmo.qmyj.framework.util.QMDateUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; /** * FileName: AutoTransImpl * Author: anpei * Date: 2017/5/7 * Description: 自动转账以及好评(7日) */ @Component("autoTrans") public class AutoTransImpl implements AutoTrans { private Logger logger = LoggerFactory.getLogger(getClass()); @Resource private OrderInfoDao orderInfoDao; @Resource private UserInfoDao userInfoDao; @Resource private EvaluationDao evaluationDao; @Override @Transactional(propagation = Propagation.REQUIRES_NEW) public int executeAutoTrans() { logger.info("---------------->处理自动转账五星好评业务处理开始:" + QMDateUtil.getYYYY_MM_DD_HH_MM_SS_CN() + "<------------"); long startTime = System.currentTimeMillis(); /*搜索订单状态:已完成;支付状态:已支付;并且订单好评转账时间字段为null,的所有订单 String orderType,String txnType,*/ List<OrderInfo> oderInfoList = orderInfoDao.getOrderInfoByOrderStatOrderFinishStat(Constants.orderTypes.wechat,Constants.txnType.PAY,orderStat.SUCCESS, orderFinishStat.FINISHED_ORDERS); int count = autoTrans.ZERO_COMMIT; if (DataVerifyUtil.isEmptyList(oderInfoList)) { logger.info("---->没有检测出需要自动转账五星好评的订单,共处理【" + count + "】笔。本次定时任务结束" + QMDateUtil.getYYYY_MM_DD_HH_MM_SS_CN() + "<-----"); return count; } logger.info("-------------->列表不为空,说明有初步符合条件的订单<-------------"); String targetWorkerId = "";//接单工人id String transAmt = "";//订单交易金额 String currentBal = "";//现有余额 String executedBal = "";//增加以后的余额 UserInfo userInfo; Evaluation evaluation = new Evaluation(); for (OrderInfo orderInfo : oderInfoList) { if (Integer.parseInt(QMDateUtil.getYYYYMMDD())>Integer.parseInt(orderInfo.getAutoTransDateTime().substring(autoTrans.SUB_PREFIX, autoTrans.SUB_SUFFIX)) /*(orderInfo.getAutoTransDateTime().substring(autoTrans.SUB_PREFIX, autoTrans.SUB_SUFFIX)).compareTo(QMDateUtil.getYYYYMMDD()) == 0*/) { /*这些订单的自动转账好评日期已经等于7天*/ targetWorkerId = orderInfo.getUserId();//接单工人id transAmt = orderInfo.getOrderTxnAmt();//订单交易金额 userInfo = userInfoDao.getCustomerInfoById(targetWorkerId); currentBal = userInfo.getAcctBal();//现有余额 executedBal = ForMartUtil.forMart(Double.parseDouble(transAmt) + Double.parseDouble(currentBal));//增加以后的余额 userInfo.setAcctBal(executedBal);//赋予新的余额值 //一开始的总星级 int allStar = Integer.parseInt(userInfo.getAllStar()); int s = Integer.parseInt(userInfo.getAvgStart());//一开始的平均星级 int sumOrder = Constants.score.zero;//单数 默认为0 if (s == Constants.score.zero || allStar == Constants.score.zero){//如果是第一单,则单数为0 sumOrder = Constants.score.zero; }else{ //否则计算 sumOrder = allStar / s * Constants.evaluation.sumtype; } //平均星级等于 总星级/ 单数 * 6 allStar = allStar + Integer.parseInt(Constants.evaluation.totalRank);//总星级 int avgStart = allStar / ((sumOrder+Constants.score.one)*Constants.evaluation.sumtype); userInfo.setAllStar(String.valueOf(allStar));//总星级 userInfo.setAvgStart(String.valueOf(avgStart));//平均星级 // TODO: Rust 2017/5/9 仅作测试,后期要删 // logger.info("transAmt:" + transAmt); // logger.info("currentBal:" + currentBal); // logger.info("executeBal:" + executedBal); /*处理五星好评*/ evaluation.setEvaluationDateTime(QMDateUtil.getYYYYMMDDHHMMSS()); evaluation.setTotalStartCount(Constants.evaluation.totalRank); evaluation.setStarsCarefully(Constants.evaluation.star); evaluation.setStarsClothAttire(Constants.evaluation.star); evaluation.setStarsEmpCard(Constants.evaluation.star); evaluation.setStarsOnTime(Constants.evaluation.star); evaluation.setStarsPoliteness(Constants.evaluation.star); evaluation.setStarsQualified(Constants.evaluation.star); evaluation.setEvaluationContent("此用户暂无评价"); evaluation.setEvaluationRank("好评"); evaluation.setUserId(targetWorkerId); if (1 > evaluationDao.InsertEvaluation(evaluation)) { logger.error("-------------->将信息插入评论表失败,处理自动五星好评失败<---------"); return count; } logger.info("-------------->将信息插入评论表成功,处理自动五星好评成功<---------"); logger.info("-------------->对原订单表进行更新操作,orderFinishStat<-----------"); orderInfo.setOrderFinishStat(Constants.orderFinishStat.ALREADY_PAY); if (1 > orderInfoDao.updateOrderinfoStatus(orderInfo)) { logger.error("---------------->更新订单完成状态为 03:已到账,失败!<------------"); return count; } logger.info("--------------->开始执行余额增加操作<-------------"); if (1 > userInfoDao.updateCustomerInfo(userInfo)) { logger.error("------------------>数据库更新用户余额字段 【FAIL】<--------------"); return count; } logger.info("------------------>数据库更新用户余额字段 【SUCCESS】<-----------------"); ++count; } } logger.info("--------------->所有符合自动转账五星好评的订单处理完毕<-----------"); long endTime = System.currentTimeMillis(); logger.info("----------->定时任务结束!本次共执行[" + count + "]笔更新操作,耗时(" + (endTime - startTime + "ms)<------------")); return count; } }
spring的定时任务
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。