一、Cron定时器
1.类上添加@EnableScheduling
2.方法上添加@Scheduled ,并制定cron 定时策略
/**
* Project Name:DamSurfDispMicroService
* File Name:PublishDSDDataInfo.java
* Package Name:com.bh.service
* Date:下午4:20:22
* Copyright (c) 2020, bluemobi All Rights Reserved.
*
*/
package com.bh.service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.bh.common.AESEncryptUtils;
import com.bh.dao.MonitorPointInfoDao;
import com.bh.dao.PointWarnRealTimeDataDao;
import com.bh.entity.DSDDataInfo;
import com.bh.entity.PointWarnRealTimeData;
/**
* Description: <br/>
* Date: 下午4:20:22 <br/>
*
* @author 冯鑫龙
* @version
* @see
*/
@Service
@EnableScheduling
public class PublishDSDDataInfoService {
// AES加密密码
@Value("${myYml.password}")
private String aesPassword;
// 定时器策略
@Value("${myYml.cron}")
private String cron;
@Value("${myYml.dataType}")
private String dataType; // 0302表面位移数据信息
@Autowired
private PointWarnRealTimeDataDao pointWarnRealTimeDataDao;
@Autowired
private MonitorPointInfoDao monitorPointInfoDao;
@Autowired
private RequestOtherService serviceClient;
@Scheduled(cron = "* * * * * ?")
public void scheduling() {
// 执行定时方法
}
public String publishDSDDataInfo() {
String jsonString = "";
try {
List<PointWarnRealTimeData> rawDataList = pointWarnRealTimeDataDao.findAll();
List<DSDDataInfo> resultDataList = new ArrayList<DSDDataInfo>();
for (PointWarnRealTimeData map : rawDataList) {
DSDDataInfo dsdDataInfo = new DSDDataInfo();
dsdDataInfo.setSensorNo(
(monitorPointInfoDao.findByMonitorPointNum(map.getMonitorPointNum())).getDeviceId());
dsdDataInfo.setxValue(
(monitorPointInfoDao.findByMonitorPointNum(map.getMonitorPointNum())).getxCoordinate());
dsdDataInfo.setyValue(
(monitorPointInfoDao.findByMonitorPointNum(map.getMonitorPointNum())).getyCoordinate());
dsdDataInfo.setzValue(
(monitorPointInfoDao.findByMonitorPointNum(map.getMonitorPointNum())).getyCoordinate());
dsdDataInfo.setDisplacement(null);
dsdDataInfo.setDisplacementRateState(null);
dsdDataInfo.setDisplacementState(null);
dsdDataInfo.setDisplacementRate(null);
dsdDataInfo.setCollectDate(
monitorPointInfoDao.findByMonitorPointNum(map.getMonitorPointNum()).getUpdateTime().toString());
resultDataList.add(dsdDataInfo);
}
// 获取不用加密的尾矿库基础信息
Map<String, Object> baseInfo = serviceClient.getBase();
String tailingNo = baseInfo.get("tailingNo").toString();// 尾矿库编号
String departmentId = baseInfo.get("departmentId").toString();// 部门编号
String provinceId = baseInfo.get("provinceId").toString();// 省份id
String dataType = "0302"; // 表面位移数据信息
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(date);
String sendTime = format;
String aesEncrypt = AESEncryptUtils.aesEncrypt(resultDataList, aesPassword);
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("tailingNo", tailingNo);
resultMap.put("departmentId", departmentId);
resultMap.put("provinceId", provinceId);
resultMap.put("type", dataType);
resultMap.put("sendTime", sendTime);
resultMap.put("datas", aesEncrypt);
jsonString = JSON.toJSONString(resultMap);
} catch (Exception e) {
e.printStackTrace();
jsonString = AESEncryptUtils.aesEncrypt(jsonString, aesPassword);
}
return jsonString;
}
}
二、Timer定时器
1.new TimerTask(),实现run方法
2.timer.scheduleAtFixedRate()方法指名定时策略
注意:这里是util包下
/**
* Project Name:DamInteDispMicroService
* File Name:TimerDIDController.java
* Package Name:com.bh.controller
* Date:下午5:01:10
* Copyright (c) 2020, bluemobi All Rights Reserved.
*
*/
package com.bh.controller;
import java.util.Timer;
import java.util.TimerTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.stereotype.Component;
import com.bh.service.PublishDIDDataInfoService;
import com.bh.service.PublishMonitorItemDataInfoService;
import com.bh.service.PublishService;
/**
* Description: <br/>
* Date: 下午5:01:10 <br/>
*
* @author 冯鑫龙
* @version
* @see
*/
@Component
public class TimerDIDController implements ApplicationRunner {
@Autowired
private PublishDIDDataInfoService publishDIDDataInfoService;
@Autowired
private PublishService publishService;
@Autowired
private PublishMonitorItemDataInfoService publishMonitorItemDataInfoService;
@Value("${myYml.cron}")
private String cron;
@Value("${myYml.isOpen}")
private String isOpen;
Timer timer = new Timer();
TimerTask didTask = null;
@Override
public void run(ApplicationArguments arg0) throws Exception {
if ("0".equals(isOpen)) {
didTimerTask(Integer.parseInt(cron));
}
}
public void didTimerTask(int frequency) {
didTask = new TimerTask() {
@Override
public void run() {
String jsonString = publishDIDDataInfoService.getJsonString();
publishService.ouptutDIDDataInfo().send(MessageBuilder.withPayload(jsonString).build());
String jsonMonitorString = publishMonitorItemDataInfoService.findAllItemDataInfos();
publishService.monitorItemDataInfo().send(MessageBuilder.withPayload(jsonMonitorString).build());
}
};
timer.scheduleAtFixedRate(didTask, 10000, frequency * 60 * 1000);
}
}