Commit 9a80e10f6ead2382503a4059f4472471bfd45b8b
1 parent
95642d0b
使用 java.time.Instant代替 java.util.Date
Showing
11 changed files
with
78 additions
and
109 deletions
src/main/java/com/genersoft/iot/vmp/conf/DynamicTask.java
| 1 | 1 | package com.genersoft.iot.vmp.conf; |
| 2 | 2 | |
| 3 | 3 | import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask; |
| 4 | -import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.response.cmd.CatalogResponseMessageHandler; | |
| 5 | 4 | import org.slf4j.Logger; |
| 6 | 5 | import org.slf4j.LoggerFactory; |
| 7 | 6 | import org.springframework.beans.factory.annotation.Autowired; |
| ... | ... | @@ -9,25 +8,27 @@ import org.springframework.context.annotation.Bean; |
| 9 | 8 | import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; |
| 10 | 9 | import org.springframework.stereotype.Component; |
| 11 | 10 | |
| 12 | -import java.util.Date; | |
| 11 | +import java.time.Instant; | |
| 13 | 12 | import java.util.Map; |
| 14 | 13 | import java.util.Set; |
| 15 | 14 | import java.util.concurrent.ConcurrentHashMap; |
| 16 | 15 | import java.util.concurrent.ScheduledFuture; |
| 16 | +import java.util.concurrent.TimeUnit; | |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * 动态定时任务 |
| 20 | + * @author lin | |
| 20 | 21 | */ |
| 21 | 22 | @Component |
| 22 | 23 | public class DynamicTask { |
| 23 | 24 | |
| 24 | - private Logger logger = LoggerFactory.getLogger(DynamicTask.class); | |
| 25 | + private final Logger logger = LoggerFactory.getLogger(DynamicTask.class); | |
| 25 | 26 | |
| 26 | 27 | @Autowired |
| 27 | 28 | private ThreadPoolTaskScheduler threadPoolTaskScheduler; |
| 28 | 29 | |
| 29 | - private Map<String, ScheduledFuture<?>> futureMap = new ConcurrentHashMap<>(); | |
| 30 | - private Map<String, Runnable> runnableMap = new ConcurrentHashMap<>(); | |
| 30 | + private final Map<String, ScheduledFuture<?>> futureMap = new ConcurrentHashMap<>(); | |
| 31 | + private final Map<String, Runnable> runnableMap = new ConcurrentHashMap<>(); | |
| 31 | 32 | |
| 32 | 33 | @Bean |
| 33 | 34 | public ThreadPoolTaskScheduler threadPoolTaskScheduler() { |
| ... | ... | @@ -47,7 +48,7 @@ public class DynamicTask { |
| 47 | 48 | * @return |
| 48 | 49 | */ |
| 49 | 50 | public void startCron(String key, Runnable task, int cycleForCatalog) { |
| 50 | - ScheduledFuture future = futureMap.get(key); | |
| 51 | + ScheduledFuture<?> future = futureMap.get(key); | |
| 51 | 52 | if (future != null) { |
| 52 | 53 | if (future.isCancelled()) { |
| 53 | 54 | logger.debug("任务【{}】已存在但是关闭状态!!!", key); |
| ... | ... | @@ -76,7 +77,9 @@ public class DynamicTask { |
| 76 | 77 | */ |
| 77 | 78 | public void startDelay(String key, Runnable task, int delay) { |
| 78 | 79 | stop(key); |
| 79 | - Date starTime = new Date(System.currentTimeMillis() + delay); | |
| 80 | + | |
| 81 | + // 获取执行的时刻 | |
| 82 | + Instant startInstant = Instant.now().plusMillis(TimeUnit.MILLISECONDS.toMillis(delay)); | |
| 80 | 83 | |
| 81 | 84 | ScheduledFuture future = futureMap.get(key); |
| 82 | 85 | if (future != null) { |
| ... | ... | @@ -88,7 +91,7 @@ public class DynamicTask { |
| 88 | 91 | } |
| 89 | 92 | } |
| 90 | 93 | // scheduleWithFixedDelay 必须等待上一个任务结束才开始计时period, cycleForCatalog表示执行的间隔 |
| 91 | - future = threadPoolTaskScheduler.schedule(task, starTime); | |
| 94 | + future = threadPoolTaskScheduler.schedule(task, startInstant); | |
| 92 | 95 | if (future != null){ |
| 93 | 96 | futureMap.put(key, future); |
| 94 | 97 | runnableMap.put(key, task); | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/auth/DigestServerAuthenticationHelper.java
| ... | ... | @@ -27,8 +27,7 @@ package com.genersoft.iot.vmp.gb28181.auth; |
| 27 | 27 | |
| 28 | 28 | import java.security.MessageDigest; |
| 29 | 29 | import java.security.NoSuchAlgorithmException; |
| 30 | -import java.text.DecimalFormat; | |
| 31 | -import java.util.Date; | |
| 30 | +import java.time.Instant; | |
| 32 | 31 | import java.util.Random; |
| 33 | 32 | |
| 34 | 33 | import javax.sip.address.URI; |
| ... | ... | @@ -90,17 +89,12 @@ public class DigestServerAuthenticationHelper { |
| 90 | 89 | * @return a generated nonce. |
| 91 | 90 | */ |
| 92 | 91 | private String generateNonce() { |
| 93 | - // Get the time of day and run MD5 over it. | |
| 94 | - Date date = new Date(); | |
| 95 | - long time = date.getTime(); | |
| 92 | + long time = Instant.now().toEpochMilli(); | |
| 96 | 93 | Random rand = new Random(); |
| 97 | 94 | long pad = rand.nextLong(); |
| 98 | - // String nonceString = (new Long(time)).toString() | |
| 99 | - // + (new Long(pad)).toString(); | |
| 100 | 95 | String nonceString = Long.valueOf(time).toString() |
| 101 | 96 | + Long.valueOf(pad).toString(); |
| 102 | 97 | byte mdbytes[] = messageDigest.digest(nonceString.getBytes()); |
| 103 | - // Convert the mdbytes array into a hex string. | |
| 104 | 98 | return toHexString(mdbytes); |
| 105 | 99 | } |
| 106 | 100 | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/bean/CatalogData.java
| 1 | 1 | package com.genersoft.iot.vmp.gb28181.bean; |
| 2 | 2 | |
| 3 | -import java.util.Date; | |
| 3 | +import java.time.Instant; | |
| 4 | 4 | import java.util.List; |
| 5 | 5 | |
| 6 | 6 | public class CatalogData { |
| 7 | 7 | private int sn; // 命令序列号 |
| 8 | 8 | private int total; |
| 9 | 9 | private List<DeviceChannel> channelList; |
| 10 | - private Date lastTime; | |
| 10 | + private Instant lastTime; | |
| 11 | 11 | private Device device; |
| 12 | 12 | private String errorMsg; |
| 13 | 13 | |
| ... | ... | @@ -41,11 +41,11 @@ public class CatalogData { |
| 41 | 41 | this.channelList = channelList; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - public Date getLastTime() { | |
| 44 | + public Instant getLastTime() { | |
| 45 | 45 | return lastTime; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - public void setLastTime(Date lastTime) { | |
| 48 | + public void setLastTime(Instant lastTime) { | |
| 49 | 49 | this.lastTime = lastTime; |
| 50 | 50 | } |
| 51 | 51 | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/bean/RecordInfo.java
| 1 | 1 | package com.genersoft.iot.vmp.gb28181.bean; |
| 2 | 2 | |
| 3 | -import java.util.Date; | |
| 3 | +import java.time.Instant; | |
| 4 | 4 | import java.util.List; |
| 5 | 5 | |
| 6 | 6 | /** |
| ... | ... | @@ -20,7 +20,7 @@ public class RecordInfo { |
| 20 | 20 | |
| 21 | 21 | private int sumNum; |
| 22 | 22 | |
| 23 | - private Date lastTime; | |
| 23 | + private Instant lastTime; | |
| 24 | 24 | |
| 25 | 25 | private List<RecordItem> recordList; |
| 26 | 26 | |
| ... | ... | @@ -72,11 +72,11 @@ public class RecordInfo { |
| 72 | 72 | this.sn = sn; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - public Date getLastTime() { | |
| 75 | + public Instant getLastTime() { | |
| 76 | 76 | return lastTime; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - public void setLastTime(Date lastTime) { | |
| 79 | + public void setLastTime(Instant lastTime) { | |
| 80 | 80 | this.lastTime = lastTime; |
| 81 | 81 | } |
| 82 | 82 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/bean/RecordItem.java
| ... | ... | @@ -5,7 +5,8 @@ import com.genersoft.iot.vmp.utils.DateUtil; |
| 5 | 5 | import org.jetbrains.annotations.NotNull; |
| 6 | 6 | |
| 7 | 7 | import java.text.ParseException; |
| 8 | -import java.util.Date; | |
| 8 | +import java.time.Instant; | |
| 9 | +import java.time.temporal.TemporalAccessor; | |
| 9 | 10 | |
| 10 | 11 | /** |
| 11 | 12 | * @description:设备录像bean |
| ... | ... | @@ -116,17 +117,17 @@ public class RecordItem implements Comparable<RecordItem>{ |
| 116 | 117 | |
| 117 | 118 | @Override |
| 118 | 119 | public int compareTo(@NotNull RecordItem recordItem) { |
| 119 | - try { | |
| 120 | - Date startTime_now = DateUtil.format.parse(startTime); | |
| 121 | - Date startTime_param = DateUtil.format.parse(recordItem.getStartTime()); | |
| 122 | - if (startTime_param.compareTo(startTime_now) > 0) { | |
| 123 | - return -1; | |
| 124 | - }else { | |
| 125 | - return 1; | |
| 126 | - } | |
| 127 | - } catch (ParseException e) { | |
| 128 | - e.printStackTrace(); | |
| 120 | + TemporalAccessor startTimeNow = DateUtil.formatter.parse(startTime); | |
| 121 | + TemporalAccessor startTimeParam = DateUtil.formatter.parse(recordItem.getStartTime()); | |
| 122 | + Instant startTimeParamInstant = Instant.from(startTimeParam); | |
| 123 | + Instant startTimeNowInstant = Instant.from(startTimeNow); | |
| 124 | + if (startTimeNowInstant.equals(startTimeParamInstant)) { | |
| 125 | + return 0; | |
| 126 | + }else if (Instant.from(startTimeParam).isAfter(Instant.from(startTimeNow)) ) { | |
| 127 | + return -1; | |
| 128 | + }else { | |
| 129 | + return 1; | |
| 129 | 130 | } |
| 130 | - return 0; | |
| 131 | + | |
| 131 | 132 | } |
| 132 | 133 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/event/SipSubscribe.java
| ... | ... | @@ -9,11 +9,14 @@ import org.springframework.stereotype.Component; |
| 9 | 9 | import javax.sip.*; |
| 10 | 10 | import javax.sip.header.CallIdHeader; |
| 11 | 11 | import javax.sip.message.Response; |
| 12 | -import java.util.Calendar; | |
| 13 | -import java.util.Date; | |
| 12 | +import java.time.Instant; | |
| 14 | 13 | import java.util.Map; |
| 15 | 14 | import java.util.concurrent.ConcurrentHashMap; |
| 15 | +import java.util.concurrent.TimeUnit; | |
| 16 | 16 | |
| 17 | +/** | |
| 18 | + * @author lin | |
| 19 | + */ | |
| 17 | 20 | @Component |
| 18 | 21 | public class SipSubscribe { |
| 19 | 22 | |
| ... | ... | @@ -23,28 +26,25 @@ public class SipSubscribe { |
| 23 | 26 | |
| 24 | 27 | private Map<String, SipSubscribe.Event> okSubscribes = new ConcurrentHashMap<>(); |
| 25 | 28 | |
| 26 | - private Map<String, Date> okTimeSubscribes = new ConcurrentHashMap<>(); | |
| 27 | - private Map<String, Date> errorTimeSubscribes = new ConcurrentHashMap<>(); | |
| 29 | + private Map<String, Instant> okTimeSubscribes = new ConcurrentHashMap<>(); | |
| 30 | + private Map<String, Instant> errorTimeSubscribes = new ConcurrentHashMap<>(); | |
| 28 | 31 | |
| 29 | 32 | // @Scheduled(cron="*/5 * * * * ?") //每五秒执行一次 |
| 30 | -// @Scheduled(fixedRate= 100 * 60 * 60 ) | |
| 33 | + // @Scheduled(fixedRate= 100 * 60 * 60 ) | |
| 31 | 34 | @Scheduled(cron="0 0/5 * * * ?") //每5分钟执行一次 |
| 32 | 35 | public void execute(){ |
| 33 | 36 | logger.info("[定时任务] 清理过期的SIP订阅信息"); |
| 34 | - Calendar calendar = Calendar.getInstance(); | |
| 35 | - calendar.setTime(new Date()); | |
| 36 | - calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - 5); | |
| 37 | + | |
| 38 | + Instant instant = Instant.now().minusMillis(TimeUnit.MINUTES.toMillis(5)); | |
| 37 | 39 | |
| 38 | 40 | for (String key : okTimeSubscribes.keySet()) { |
| 39 | - if (okTimeSubscribes.get(key).before(calendar.getTime())){ | |
| 40 | -// logger.info("[定时任务] 清理过期的订阅信息: {}", key); | |
| 41 | + if (okTimeSubscribes.get(key).isBefore(instant)){ | |
| 41 | 42 | okSubscribes.remove(key); |
| 42 | 43 | okTimeSubscribes.remove(key); |
| 43 | 44 | } |
| 44 | 45 | } |
| 45 | 46 | for (String key : errorTimeSubscribes.keySet()) { |
| 46 | - if (errorTimeSubscribes.get(key).before(calendar.getTime())){ | |
| 47 | -// logger.info("[定时任务] 清理过期的订阅信息: {}", key); | |
| 47 | + if (errorTimeSubscribes.get(key).isBefore(instant)){ | |
| 48 | 48 | errorSubscribes.remove(key); |
| 49 | 49 | errorTimeSubscribes.remove(key); |
| 50 | 50 | } |
| ... | ... | @@ -117,12 +117,12 @@ public class SipSubscribe { |
| 117 | 117 | |
| 118 | 118 | public void addErrorSubscribe(String key, SipSubscribe.Event event) { |
| 119 | 119 | errorSubscribes.put(key, event); |
| 120 | - errorTimeSubscribes.put(key, new Date()); | |
| 120 | + errorTimeSubscribes.put(key, Instant.now()); | |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | public void addOkSubscribe(String key, SipSubscribe.Event event) { |
| 124 | 124 | okSubscribes.put(key, event); |
| 125 | - okTimeSubscribes.put(key, new Date()); | |
| 125 | + okTimeSubscribes.put(key, Instant.now()); | |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | public SipSubscribe.Event getErrorSubscribe(String key) { | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/session/CatalogDataCatch.java
| ... | ... | @@ -4,16 +4,15 @@ import com.genersoft.iot.vmp.gb28181.bean.CatalogData; |
| 4 | 4 | import com.genersoft.iot.vmp.gb28181.bean.Device; |
| 5 | 5 | import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; |
| 6 | 6 | import com.genersoft.iot.vmp.gb28181.bean.SyncStatus; |
| 7 | -import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder; | |
| 8 | -import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage; | |
| 9 | 7 | import com.genersoft.iot.vmp.storager.IVideoManagerStorage; |
| 10 | -import com.genersoft.iot.vmp.vmanager.bean.WVPResult; | |
| 11 | 8 | import org.springframework.beans.factory.annotation.Autowired; |
| 12 | 9 | import org.springframework.scheduling.annotation.Scheduled; |
| 13 | 10 | import org.springframework.stereotype.Component; |
| 14 | 11 | |
| 12 | +import java.time.Instant; | |
| 15 | 13 | import java.util.*; |
| 16 | 14 | import java.util.concurrent.ConcurrentHashMap; |
| 15 | +import java.util.concurrent.TimeUnit; | |
| 17 | 16 | |
| 18 | 17 | @Component |
| 19 | 18 | public class CatalogDataCatch { |
| ... | ... | @@ -21,9 +20,6 @@ public class CatalogDataCatch { |
| 21 | 20 | public static Map<String, CatalogData> data = new ConcurrentHashMap<>(); |
| 22 | 21 | |
| 23 | 22 | @Autowired |
| 24 | - private DeferredResultHolder deferredResultHolder; | |
| 25 | - | |
| 26 | - @Autowired | |
| 27 | 23 | private IVideoManagerStorage storager; |
| 28 | 24 | |
| 29 | 25 | public void addReady(Device device, int sn ) { |
| ... | ... | @@ -34,7 +30,7 @@ public class CatalogDataCatch { |
| 34 | 30 | catalogData.setDevice(device); |
| 35 | 31 | catalogData.setSn(sn); |
| 36 | 32 | catalogData.setStatus(CatalogData.CatalogDataStatus.ready); |
| 37 | - catalogData.setLastTime(new Date(System.currentTimeMillis())); | |
| 33 | + catalogData.setLastTime(Instant.now()); | |
| 38 | 34 | data.put(device.getDeviceId(), catalogData); |
| 39 | 35 | } |
| 40 | 36 | } |
| ... | ... | @@ -48,7 +44,7 @@ public class CatalogDataCatch { |
| 48 | 44 | catalogData.setDevice(device); |
| 49 | 45 | catalogData.setChannelList(Collections.synchronizedList(new ArrayList<>())); |
| 50 | 46 | catalogData.setStatus(CatalogData.CatalogDataStatus.runIng); |
| 51 | - catalogData.setLastTime(new Date(System.currentTimeMillis())); | |
| 47 | + catalogData.setLastTime(Instant.now()); | |
| 52 | 48 | data.put(deviceId, catalogData); |
| 53 | 49 | }else { |
| 54 | 50 | // 同一个设备的通道同步请求只考虑一个,其他的直接忽略 |
| ... | ... | @@ -59,7 +55,7 @@ public class CatalogDataCatch { |
| 59 | 55 | catalogData.setDevice(device); |
| 60 | 56 | catalogData.setStatus(CatalogData.CatalogDataStatus.runIng); |
| 61 | 57 | catalogData.getChannelList().addAll(deviceChannelList); |
| 62 | - catalogData.setLastTime(new Date(System.currentTimeMillis())); | |
| 58 | + catalogData.setLastTime(Instant.now()); | |
| 63 | 59 | } |
| 64 | 60 | } |
| 65 | 61 | |
| ... | ... | @@ -102,16 +98,13 @@ public class CatalogDataCatch { |
| 102 | 98 | @Scheduled(fixedRate = 5 * 1000) //每5秒执行一次, 发现数据5秒未更新则移除数据并认为数据接收超时 |
| 103 | 99 | private void timerTask(){ |
| 104 | 100 | Set<String> keys = data.keySet(); |
| 105 | - Calendar calendarBefore5S = Calendar.getInstance(); | |
| 106 | - calendarBefore5S.setTime(new Date()); | |
| 107 | - calendarBefore5S.set(Calendar.SECOND, calendarBefore5S.get(Calendar.SECOND) - 5); | |
| 108 | 101 | |
| 109 | - Calendar calendarBefore30S = Calendar.getInstance(); | |
| 110 | - calendarBefore30S.setTime(new Date()); | |
| 111 | - calendarBefore30S.set(Calendar.SECOND, calendarBefore30S.get(Calendar.SECOND) - 30); | |
| 102 | + Instant instantBefore5S = Instant.now().minusMillis(TimeUnit.SECONDS.toMillis(5)); | |
| 103 | + Instant instantBefore30S = Instant.now().minusMillis(TimeUnit.SECONDS.toMillis(30)); | |
| 104 | + | |
| 112 | 105 | for (String deviceId : keys) { |
| 113 | 106 | CatalogData catalogData = data.get(deviceId); |
| 114 | - if ( catalogData.getLastTime().before(calendarBefore5S.getTime())) { // 超过五秒收不到消息任务超时, 只更新这一部分数据 | |
| 107 | + if ( catalogData.getLastTime().isBefore(instantBefore5S)) { // 超过五秒收不到消息任务超时, 只更新这一部分数据 | |
| 115 | 108 | if (catalogData.getStatus().equals(CatalogData.CatalogDataStatus.runIng)) { |
| 116 | 109 | storager.resetChannels(catalogData.getDevice().getDeviceId(), catalogData.getChannelList()); |
| 117 | 110 | if (catalogData.getTotal() != catalogData.getChannelList().size()) { |
| ... | ... | @@ -124,7 +117,7 @@ public class CatalogDataCatch { |
| 124 | 117 | } |
| 125 | 118 | catalogData.setStatus(CatalogData.CatalogDataStatus.end); |
| 126 | 119 | } |
| 127 | - if (catalogData.getStatus().equals(CatalogData.CatalogDataStatus.end) && catalogData.getLastTime().before(calendarBefore30S.getTime())) { // 超过三十秒,如果标记为end则删除 | |
| 120 | + if (catalogData.getStatus().equals(CatalogData.CatalogDataStatus.end) && catalogData.getLastTime().isBefore(instantBefore30S)) { // 超过三十秒,如果标记为end则删除 | |
| 128 | 121 | data.remove(deviceId); |
| 129 | 122 | } |
| 130 | 123 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/session/RecordDataCatch.java
| ... | ... | @@ -3,16 +3,15 @@ package com.genersoft.iot.vmp.gb28181.session; |
| 3 | 3 | import com.genersoft.iot.vmp.gb28181.bean.*; |
| 4 | 4 | import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder; |
| 5 | 5 | import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage; |
| 6 | -import com.genersoft.iot.vmp.storager.IVideoManagerStorage; | |
| 7 | -import com.genersoft.iot.vmp.utils.DateUtil; | |
| 8 | 6 | import com.genersoft.iot.vmp.vmanager.bean.WVPResult; |
| 9 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
| 10 | 8 | import org.springframework.scheduling.annotation.Scheduled; |
| 11 | 9 | import org.springframework.stereotype.Component; |
| 12 | 10 | |
| 13 | -import java.text.SimpleDateFormat; | |
| 11 | +import java.time.Instant; | |
| 14 | 12 | import java.util.*; |
| 15 | 13 | import java.util.concurrent.ConcurrentHashMap; |
| 14 | +import java.util.concurrent.TimeUnit; | |
| 16 | 15 | |
| 17 | 16 | /** |
| 18 | 17 | * @author lin |
| ... | ... | @@ -35,7 +34,7 @@ public class RecordDataCatch { |
| 35 | 34 | recordInfo.setSn(sn.trim()); |
| 36 | 35 | recordInfo.setSumNum(sumNum); |
| 37 | 36 | recordInfo.setRecordList(Collections.synchronizedList(new ArrayList<>())); |
| 38 | - recordInfo.setLastTime(new Date(System.currentTimeMillis())); | |
| 37 | + recordInfo.setLastTime(Instant.now()); | |
| 39 | 38 | recordInfo.getRecordList().addAll(recordItems); |
| 40 | 39 | data.put(key, recordInfo); |
| 41 | 40 | }else { |
| ... | ... | @@ -44,7 +43,7 @@ public class RecordDataCatch { |
| 44 | 43 | return 0; |
| 45 | 44 | } |
| 46 | 45 | recordInfo.getRecordList().addAll(recordItems); |
| 47 | - recordInfo.setLastTime(new Date(System.currentTimeMillis())); | |
| 46 | + recordInfo.setLastTime(Instant.now()); | |
| 48 | 47 | } |
| 49 | 48 | return recordInfo.getRecordList().size(); |
| 50 | 49 | } |
| ... | ... | @@ -52,14 +51,12 @@ public class RecordDataCatch { |
| 52 | 51 | @Scheduled(fixedRate = 5 * 1000) //每5秒执行一次, 发现数据5秒未更新则移除数据并认为数据接收超时 |
| 53 | 52 | private void timerTask(){ |
| 54 | 53 | Set<String> keys = data.keySet(); |
| 55 | - Calendar calendarBefore5S = Calendar.getInstance(); | |
| 56 | - calendarBefore5S.setTime(new Date()); | |
| 57 | - calendarBefore5S.set(Calendar.SECOND, calendarBefore5S.get(Calendar.SECOND) - 5); | |
| 58 | - | |
| 54 | + // 获取五秒前的时刻 | |
| 55 | + Instant instantBefore5S = Instant.now().minusMillis(TimeUnit.SECONDS.toMillis(5)); | |
| 59 | 56 | for (String key : keys) { |
| 60 | 57 | RecordInfo recordInfo = data.get(key); |
| 61 | 58 | // 超过五秒收不到消息任务超时, 只更新这一部分数据 |
| 62 | - if ( recordInfo.getLastTime().before(calendarBefore5S.getTime())) { | |
| 59 | + if ( recordInfo.getLastTime().isBefore(instantBefore5S)) { | |
| 63 | 60 | // 处理录像数据, 返回给前端 |
| 64 | 61 | String msgKey = DeferredResultHolder.CALLBACK_CMD_RECORDINFO + recordInfo.getDeviceId() + recordInfo.getSn(); |
| 65 | 62 | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/InviteRequestProcessor.java
| ... | ... | @@ -40,7 +40,7 @@ import javax.sip.header.CallIdHeader; |
| 40 | 40 | import javax.sip.message.Request; |
| 41 | 41 | import javax.sip.message.Response; |
| 42 | 42 | import java.text.ParseException; |
| 43 | -import java.util.Date; | |
| 43 | +import java.time.Instant; | |
| 44 | 44 | import java.util.Vector; |
| 45 | 45 | |
| 46 | 46 | /** |
| ... | ... | @@ -180,16 +180,16 @@ public class InviteRequestProcessor extends SIPRequestProcessorParent implements |
| 180 | 180 | |
| 181 | 181 | Long startTime = null; |
| 182 | 182 | Long stopTime = null; |
| 183 | - Date start = null; | |
| 184 | - Date end = null; | |
| 183 | + Instant start = null; | |
| 184 | + Instant end = null; | |
| 185 | 185 | if (sdp.getTimeDescriptions(false) != null && sdp.getTimeDescriptions(false).size() > 0) { |
| 186 | 186 | TimeDescriptionImpl timeDescription = (TimeDescriptionImpl)(sdp.getTimeDescriptions(false).get(0)); |
| 187 | 187 | TimeField startTimeFiled = (TimeField)timeDescription.getTime(); |
| 188 | 188 | startTime = startTimeFiled.getStartTime(); |
| 189 | 189 | stopTime = startTimeFiled.getStopTime(); |
| 190 | 190 | |
| 191 | - start = new Date(startTime*1000); | |
| 192 | - end = new Date(stopTime*1000); | |
| 191 | + start = Instant.ofEpochMilli(startTime*1000); | |
| 192 | + end = Instant.ofEpochMilli(stopTime*1000); | |
| 193 | 193 | } |
| 194 | 194 | // 获取支持的格式 |
| 195 | 195 | Vector mediaDescriptions = sdp.getMediaDescriptions(true); |
| ... | ... | @@ -335,8 +335,8 @@ public class InviteRequestProcessor extends SIPRequestProcessorParent implements |
| 335 | 335 | sendRtpItem.setStreamId(ssrcInfo.getStream()); |
| 336 | 336 | // 写入redis, 超时时回复 |
| 337 | 337 | redisCatchStorage.updateSendRTPSever(sendRtpItem); |
| 338 | - playService.playBack(mediaServerItem, ssrcInfo, device.getDeviceId(), channelId, DateUtil.format.format(start), | |
| 339 | - DateUtil.format.format(end), null, result -> { | |
| 338 | + playService.playBack(mediaServerItem, ssrcInfo, device.getDeviceId(), channelId, DateUtil.formatter.format(start), | |
| 339 | + DateUtil.formatter.format(end), null, result -> { | |
| 340 | 340 | if (result.getCode() != 0){ |
| 341 | 341 | logger.warn("录像回放失败"); |
| 342 | 342 | if (result.getEvent() != null) { | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java
| ... | ... | @@ -24,8 +24,6 @@ import javax.sip.SipException; |
| 24 | 24 | import javax.sip.header.ViaHeader; |
| 25 | 25 | import javax.sip.message.Response; |
| 26 | 26 | import java.text.ParseException; |
| 27 | -import java.util.Calendar; | |
| 28 | -import java.util.Date; | |
| 29 | 27 | |
| 30 | 28 | @Component |
| 31 | 29 | public class KeepaliveNotifyMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler { | ... | ... |
src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java
| ... | ... | @@ -11,24 +11,18 @@ import com.genersoft.iot.vmp.gb28181.task.impl.CatalogSubscribeTask; |
| 11 | 11 | import com.genersoft.iot.vmp.gb28181.task.impl.MobilePositionSubscribeTask; |
| 12 | 12 | import com.genersoft.iot.vmp.gb28181.bean.SyncStatus; |
| 13 | 13 | import com.genersoft.iot.vmp.service.IMediaServerService; |
| 14 | -import com.genersoft.iot.vmp.service.IMediaService; | |
| 15 | 14 | import com.genersoft.iot.vmp.storager.IRedisCatchStorage; |
| 16 | 15 | import com.genersoft.iot.vmp.storager.dao.DeviceMapper; |
| 17 | 16 | import com.genersoft.iot.vmp.utils.DateUtil; |
| 18 | 17 | import org.slf4j.Logger; |
| 19 | 18 | import org.slf4j.LoggerFactory; |
| 20 | 19 | import org.springframework.beans.factory.annotation.Autowired; |
| 21 | -import org.springframework.beans.factory.annotation.Qualifier; | |
| 22 | -import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | |
| 23 | 20 | import org.springframework.stereotype.Service; |
| 24 | 21 | import org.springframework.util.StringUtils; |
| 25 | 22 | |
| 26 | -import javax.sip.DialogState; | |
| 27 | -import javax.sip.TimeoutEvent; | |
| 28 | -import java.text.ParseException; | |
| 29 | -import java.util.Calendar; | |
| 30 | -import java.util.Date; | |
| 23 | +import java.time.Instant; | |
| 31 | 24 | import java.util.List; |
| 25 | +import java.util.concurrent.TimeUnit; | |
| 32 | 26 | |
| 33 | 27 | /** |
| 34 | 28 | * 设备业务(目录订阅) |
| ... | ... | @@ -101,9 +95,7 @@ public class DeviceServiceImpl implements IDeviceService { |
| 101 | 95 | // 刷新过期任务 |
| 102 | 96 | String registerExpireTaskKey = registerExpireTaskKeyPrefix + device.getDeviceId(); |
| 103 | 97 | dynamicTask.stop(registerExpireTaskKey); |
| 104 | - dynamicTask.startDelay(registerExpireTaskKey, ()->{ | |
| 105 | - offline(device.getDeviceId()); | |
| 106 | - }, device.getExpires() * 1000); | |
| 98 | + dynamicTask.startDelay(registerExpireTaskKey, ()-> offline(device.getDeviceId()), device.getExpires() * 1000); | |
| 107 | 99 | } |
| 108 | 100 | |
| 109 | 101 | @Override |
| ... | ... | @@ -217,18 +209,9 @@ public class DeviceServiceImpl implements IDeviceService { |
| 217 | 209 | |
| 218 | 210 | @Override |
| 219 | 211 | public boolean expire(Device device) { |
| 220 | - Date registerTimeDate; | |
| 221 | - try { | |
| 222 | - registerTimeDate = DateUtil.format.parse(device.getRegisterTime()); | |
| 223 | - } catch (ParseException e) { | |
| 224 | - logger.error("设备时间格式化失败:{}->{} ", device.getDeviceId(), device.getRegisterTime() ); | |
| 225 | - return false; | |
| 226 | - } | |
| 227 | - int expires = device.getExpires(); | |
| 228 | - Calendar calendarForExpire = Calendar.getInstance(); | |
| 229 | - calendarForExpire.setTime(registerTimeDate); | |
| 230 | - calendarForExpire.set(Calendar.SECOND, calendarForExpire.get(Calendar.SECOND) + expires); | |
| 231 | - return calendarForExpire.before(DateUtil.getNow()); | |
| 212 | + Instant registerTimeDate = Instant.from(DateUtil.formatter.parse(device.getRegisterTime())); | |
| 213 | + Instant expireInstant = registerTimeDate.plusMillis(TimeUnit.SECONDS.toMillis(device.getExpires())); | |
| 214 | + return expireInstant.isBefore(Instant.now()); | |
| 232 | 215 | } |
| 233 | 216 | |
| 234 | 217 | @Override | ... | ... |