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; 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.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.modules.multithread.AsyncTasks;
import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService; import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil; import com.glsx.plat.common.multithread.ListSplitIterator;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.concurrent.CountDownLatch;
/** /**
* 设备激活 service * 设备激活 service
...@@ -33,6 +31,9 @@ public class DeviceActiveService { ...@@ -33,6 +31,9 @@ public class DeviceActiveService {
@Autowired @Autowired
private VehicleDeviceMapper vehicleDeviceMapper; private VehicleDeviceMapper vehicleDeviceMapper;
@Autowired
private AsyncTasks asyncTasks;
/** /**
* 保存设备激活信息 * 保存设备激活信息
* *
...@@ -43,31 +44,34 @@ public class DeviceActiveService { ...@@ -43,31 +44,34 @@ public class DeviceActiveService {
log.info("snList is null"); log.info("snList is null");
return; return;
} }
List<GpsBsData> lastGpsBsList = remoteGpsBsService.getLastGpsBsListBySns(snList); saveActiveInfoSync(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())); * @param dataList 设备号集合大小
vehicleDevice.setActiveLocationType(Constants.LocationType.GPS); */
vehicleDevice.setActiveLocationLng(gpsLocation.getLng()); public void saveActiveInfoSync(List<String> dataList) {
vehicleDevice.setActiveLocationLat(gpsLocation.getLat()); log.info("###save active info data start###");
vehicleDevice.setActiveLocationAddress(gpsLocation.getAddress()); if (CollectionUtils.isEmpty(dataList)) {
} else { return;
GpsLocation bsLocation = gpsBsData.getBsLocation(); }
if (Objects.nonNull(bsLocation)) { StopWatch sw = new StopWatch();
vehicleDevice.setActiveLocationDate(DateUtil.parseDateTime(bsLocation.getDateTime())); sw.start("saveActiveInfoSync");
vehicleDevice.setActiveLocationType(Constants.LocationType.BS); ListSplitIterator<String> listSplitIterator = new ListSplitIterator<>(dataList, 100);
vehicleDevice.setActiveLocationLng(bsLocation.getLng()); CountDownLatch countDownLatch = new CountDownLatch(listSplitIterator.size());
vehicleDevice.setActiveLocationLat(bsLocation.getLat()); while (listSplitIterator.hasNext()) {
vehicleDevice.setActiveLocationAddress(bsLocation.getAddress()); List<String> snList = listSplitIterator.next();
} asyncTasks.saveActiveInfo(snList, countDownLatch, remoteGpsBsService, vehicleDeviceMapper);
} }
// 保存激活信息 try {
log.info("active device info:" + JSONUtil.toJsonStr(vehicleDevice)); countDownLatch.await();
vehicleDeviceMapper.updateByPrimaryKeySelective(vehicleDevice); } catch (InterruptedException e) {
}); e.printStackTrace();
}
sw.stop();
log.info("###save active info data end" + sw.getLastTaskTimeMillis() + "ms###");
} }
/** /**
......
...@@ -49,6 +49,7 @@ ...@@ -49,6 +49,7 @@
where del_flag = 0 where del_flag = 0
and sn = #{sn} and sn = #{sn}
and bind_status = 1 and bind_status = 1
limit 1
</select> </select>
<select id="selectBindingDevices" resultMap="BaseResultMap"> <select id="selectBindingDevices" resultMap="BaseResultMap">
...@@ -82,7 +83,7 @@ ...@@ -82,7 +83,7 @@
order by id desc limit 1 order by id desc limit 1
</select> </select>
<select id="getUnActiveInfoSnList" resultMap="BaseResultMap"> <select id="getUnActiveInfoSnList" resultType="java.lang.String">
select select
<include refid="Base_Column_List"/> <include refid="Base_Column_List"/>
from t_vehicle_device from t_vehicle_device
...@@ -92,9 +93,8 @@ ...@@ -92,9 +93,8 @@
and active_location_date is null and active_location_date is null
</select> </select>
<select id="getAllUnActiveInfoSnList" resultMap="BaseResultMap"> <select id="getAllUnActiveInfoSnList" resultType="java.lang.String">
select select sn
<include refid="Base_Column_List"/>
from t_vehicle_device from t_vehicle_device
where del_flag = 0 where del_flag = 0
and bind_status = 1 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 \ 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!