Commit 6c409f4a by 杨林龙

优化激活同步数据

1 parent a290bf45
package cn.com.glsx.stdinterface.modules.multithread;
import cn.com.glsx.devicemonitor.api.dto.response.GpsBsData;
import cn.com.glsx.devicemonitor.api.dto.response.GpsLocation;
import cn.com.glsx.stdinterface.common.constants.Constants;
import cn.com.glsx.stdinterface.modules.entity.VehicleDevice;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
@Slf4j
@Service
public class AsyncTasks {
/**
* 异步填充设备激活经纬度和激活地址信息
*
* @param snList
* @param countDownLatch
* @param remoteGpsBsService
*/
@Async
public void saveActiveInfo(List<String> snList, CountDownLatch countDownLatch, RemoteGpsBsService remoteGpsBsService, VehicleDeviceMapper vehicleDeviceMapper) {
if (CollectionUtils.isEmpty(snList)) {
log.info("countDownLatch查询数据为空关闭资源");
countDownLatch.countDown();
return;
}
try {
List<GpsBsData> lastGpsBsList = remoteGpsBsService.getLastGpsBsListBySns(snList);
if (CollectionUtil.isEmpty(lastGpsBsList)) {
log.info("lastGpsBsList is null");
return;
}
lastGpsBsList.forEach(gpsBsData -> {
VehicleDevice vehicleDevice = vehicleDeviceMapper.selectBindingDeviceBySn(gpsBsData.getSn());
GpsLocation gpsLocation = gpsBsData.getGpsLocation();
// 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据
if (Objects.nonNull(gpsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtil.parseDateTime(gpsLocation.getDateTime()));
vehicleDevice.setActiveLocationType(Constants.LocationType.GPS);
vehicleDevice.setActiveLocationLng(gpsLocation.getLng());
vehicleDevice.setActiveLocationLat(gpsLocation.getLat());
vehicleDevice.setActiveLocationAddress(gpsLocation.getAddress());
} else {
GpsLocation bsLocation = gpsBsData.getBsLocation();
if (Objects.nonNull(bsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtil.parseDateTime(bsLocation.getDateTime()));
vehicleDevice.setActiveLocationType(Constants.LocationType.BS);
vehicleDevice.setActiveLocationLng(bsLocation.getLng());
vehicleDevice.setActiveLocationLat(bsLocation.getLat());
vehicleDevice.setActiveLocationAddress(bsLocation.getAddress());
}
}
// 保存激活信息
log.info("active device info:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateByPrimaryKeySelective(vehicleDevice);
});
} catch (Exception e) {
log.info("saveActiveInfo异常信息===" + e.getMessage());
} finally {
countDownLatch.countDown();
log.info(Thread.currentThread().getName() + "countDownLatch关闭资源");
}
}
}
package cn.com.glsx.stdinterface.modules.service;
import cn.com.glsx.devicemonitor.api.dto.response.GpsBsData;
import cn.com.glsx.devicemonitor.api.dto.response.GpsLocation;
import cn.com.glsx.stdinterface.common.constants.Constants;
import cn.com.glsx.stdinterface.modules.entity.VehicleDevice;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.modules.multithread.AsyncTasks;
import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.glsx.plat.common.multithread.ListSplitIterator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
/**
* 设备激活 service
......@@ -33,6 +31,9 @@ public class DeviceActiveService {
@Autowired
private VehicleDeviceMapper vehicleDeviceMapper;
@Autowired
private AsyncTasks asyncTasks;
/**
* 保存设备激活信息
*
......@@ -43,31 +44,34 @@ public class DeviceActiveService {
log.info("snList is null");
return;
}
List<GpsBsData> lastGpsBsList = remoteGpsBsService.getLastGpsBsListBySns(snList);
lastGpsBsList.forEach(gpsBsData -> {
VehicleDevice vehicleDevice = vehicleDeviceMapper.selectBindingDeviceBySn(gpsBsData.getSn());
GpsLocation gpsLocation = gpsBsData.getGpsLocation();
// 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据
if (Objects.nonNull(gpsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtil.parseDateTime(gpsLocation.getDateTime()));
vehicleDevice.setActiveLocationType(Constants.LocationType.GPS);
vehicleDevice.setActiveLocationLng(gpsLocation.getLng());
vehicleDevice.setActiveLocationLat(gpsLocation.getLat());
vehicleDevice.setActiveLocationAddress(gpsLocation.getAddress());
} else {
GpsLocation bsLocation = gpsBsData.getBsLocation();
if (Objects.nonNull(bsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtil.parseDateTime(bsLocation.getDateTime()));
vehicleDevice.setActiveLocationType(Constants.LocationType.BS);
vehicleDevice.setActiveLocationLng(bsLocation.getLng());
vehicleDevice.setActiveLocationLat(bsLocation.getLat());
vehicleDevice.setActiveLocationAddress(bsLocation.getAddress());
}
}
// 保存激活信息
log.info("active device info:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateByPrimaryKeySelective(vehicleDevice);
});
saveActiveInfoSync(snList);
}
/**
* 多线程补充设备激活经纬度数据和激活地址
*
* @param dataList 设备号集合大小
*/
public void saveActiveInfoSync(List<String> dataList) {
log.info("###save active info data start###");
if (CollectionUtils.isEmpty(dataList)) {
return;
}
StopWatch sw = new StopWatch();
sw.start("saveActiveInfoSync");
ListSplitIterator<String> listSplitIterator = new ListSplitIterator<>(dataList, 100);
CountDownLatch countDownLatch = new CountDownLatch(listSplitIterator.size());
while (listSplitIterator.hasNext()) {
List<String> snList = listSplitIterator.next();
asyncTasks.saveActiveInfo(snList, countDownLatch, remoteGpsBsService, vehicleDeviceMapper);
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
sw.stop();
log.info("###save active info data end" + sw.getLastTaskTimeMillis() + "ms###");
}
/**
......
......@@ -49,6 +49,7 @@
where del_flag = 0
and sn = #{sn}
and bind_status = 1
limit 1
</select>
<select id="selectBindingDevices" resultMap="BaseResultMap">
......@@ -82,7 +83,7 @@
order by id desc limit 1
</select>
<select id="getUnActiveInfoSnList" resultMap="BaseResultMap">
<select id="getUnActiveInfoSnList" resultType="java.lang.String">
select
<include refid="Base_Column_List"/>
from t_vehicle_device
......@@ -92,9 +93,8 @@
and active_location_date is null
</select>
<select id="getAllUnActiveInfoSnList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
<select id="getAllUnActiveInfoSnList" resultType="java.lang.String">
select sn
from t_vehicle_device
where del_flag = 0
and bind_status = 1
......
package cn.com.glsx.stdinterface.modules.service;
import cn.com.glsx.Application;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DeviceActiveServiceTest {
@Autowired
private DeviceActiveService deviceActiveService;
@Test
public void saveActiveInfoFlushAll() {
System.out.println("start===");
deviceActiveService.saveActiveInfoFlushAll();
System.out.println("end===");
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!