发表于: 2017-10-30 21:12:15

1 729


今天完成的事情:

1.学习真实项目中定时任务,编写代码

首先遍历债务表,如果债务表中没有快要到期的数据,该线程停止240000毫秒后再运行,如果有就给用户发送信息,并改变是否发送短信的状态。如果该债务快要债务到期天数还没还款,就要找到该债务管理员的ID并发送给管理员短信,如果这个债权没有管理员,发送短信给所有的管理员,提醒某债务人到多少天还没还款,该采取措施了。

public class CreditEndSendMsg {


private static final Log log = LogFactory.getLog(CreditEndSendMsg.class);

private static final long SLEEP_MILLISECOND = 240000;// 空转任务间隔休息毫秒数
 
 private static final int TASK_LEN = 50;

private int days;

private CreditService creditService;

private ConstantService constantService;

private ModuleService moduleService;

private RoleService roleService;

private ManagerService managerService;

private SMSSendService smsSendService;

public void process() throws InterruptedException {
while (true) {
try {
List<Credit> credits = getImmediateExpireCredits();
if (CollectionUtils.isEmpty(credits)) {
log.info("getImmediateExpireCredits no data, sleep " + SLEEP_MILLISECOND + " ms");
Thread.sleep(SLEEP_MILLISECOND);
}
processForData(credits);
}
catch (Throwable t) {
log.error("CreditEndSendMsg error", t);
Thread.sleep(SLEEP_MILLISECOND);
}
}
}

private void processForData(List<Credit> credits) throws ServiceException, ServiceDaoException {
for (Credit credit : credits) {
/* 第一步、发短信 */
     sendMsg(credit);
/* 第二步、维护credit表的字段 */
     credit.setHasSendMsg(true);
creditService.update(credit);
log.info("update credit hasSendMsg true success");
}
}

private List<Credit> getImmediateExpireCredits() throws ServiceException, ServiceDaoException {
List<Credit> credits = new ArrayList<>();
days = getDays();
Map<String, Object> params = DynamicUtil.getImmediateExpireCredits(days);
List<Long> ids = creditService.getIdsByDynamicCondition(Credit.class, params, 0, TASK_LEN);
if (CollectionUtils.isEmpty(ids)) {
log.info("getImmediateExpireCredits ids is empty");
}
else {
credits = creditService.getObjectsByIds(ids);
log.info("getImmediateExpireCredits credits size: " + credits.size());
}
return credits;
}

private int getDays() throws ServiceException, ServiceDaoException {
Long constantId = constantService.getConstantIdByKey("creditDeadlineDays");
Constant creditDeadlineDays = constantService.getObjectById(constantId);
String value = creditDeadlineDays.getValue();
log.info("days is: " + value);
return new Integer(value);
}

private List<Manager> getManagers() throws ServiceException, ServiceDaoException {
List<Manager> managers = new ArrayList<>();
/* 第一步、找到债权管理权限id */
   Map<String, Object> params = DynamicUtil.getCreditModule();
List<Long> ids = moduleService.getIdsByDynamicCondition(Module.class, params, 0, 1);
log.info("I--ids is: " + ids);
if (CollectionUtils.isEmpty(ids)) {
log.info("find no credit module ids");
return getAllManagers();
}
/* 第二步、找到拥有债权管理权限的角色id */
   params = DynamicUtil.getCreditRole(ids.get(0));
ids = roleService.getIdsByDynamicCondition(Role.class, params, 0, Integer.MAX_VALUE);
log.info("II--ids is: " + ids);
if (CollectionUtils.isEmpty(ids)) {
log.info("find no credit role ids");
return getAllManagers();
}
/* 第三步、找到拥有相应角色的管理员 */
   params = DynamicUtil.getCreditManager(ids);
ids = managerService.getIdsByDynamicCondition(Manager.class, params, 0, Integer.MAX_VALUE);
log.info("III--ids is: " + ids);
if (CollectionUtils.isEmpty(ids)) {
log.info("find no credit manager ids");
return getAllManagers();
}
managers = managerService.getObjectsByIds(ids);
if (CollectionUtils.isEmpty(managers)) {
log.info("find no credit managers");
return getAllManagers();
}
else {
log.info("the credit managers size is: " + managers.size());
return managers;
}
}

private List<Manager> getAllManagers() throws ServiceException, ServiceDaoException {
List<Manager> list = new ArrayList<>();
List<Long> ids = managerService.getManagerIds(0, Integer.MAX_VALUE);
if (CollectionUtils.isEmpty(ids)) {
log.info("get no managers");
}
else {
list = managerService.getObjectsByIds(ids);
log.info("get all managers, size is: " + list.size());
}
return list;
}

private void sendMsg(Credit credit) throws ServiceException, ServiceDaoException {
/* 第一步、找所要发送短信的管理员 */
   List<Manager> managers = getManagers();
if (CollectionUtils.isEmpty(managers)) {
log.info("no managers need to send msg");
return;
}
/* 第二步、发送给所有的管理员 */
   for (Manager manager : managers) {
if (StringUtil.isEmpty(manager.getMobile())) {
log.info(
"current manager's mobile is null, do not send msg, manager id is: " + manager.getId());
continue;
}
smsSendService.sendSMSByTemplate(manager.getMobile(), null,
getContent(credit.getCreditorName(), credit.getCode(), days));
}
}

private String[] getContent(String name, String code, int days) {
String[] arr = new String[1];
arr[0] = name + "的债权(" + code + ")还剩" + days + "天即将到期,请相关管理人员及时处理。";
return arr;
}

明天计划的事情:继续写定时任务

遇到的问题:定时任务复杂,超出时间预计,项目延期

收获:学习如何写定时任务


返回列表 返回列表
评论

    分享到