aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/traccar/database/NotificationManager.java13
-rw-r--r--src/main/java/org/traccar/session/ConnectionManager.java7
-rw-r--r--src/main/java/org/traccar/session/cache/CacheManager.java19
3 files changed, 32 insertions, 7 deletions
diff --git a/src/main/java/org/traccar/database/NotificationManager.java b/src/main/java/org/traccar/database/NotificationManager.java
index 92f4c818a..1314a3d0a 100644
--- a/src/main/java/org/traccar/database/NotificationManager.java
+++ b/src/main/java/org/traccar/database/NotificationManager.java
@@ -118,8 +118,17 @@ public class NotificationManager {
}
public void updateEvents(Map<Event, Position> events) {
- for (Entry<Event, Position> event : events.entrySet()) {
- updateEvent(event.getKey(), event.getValue());
+ for (Entry<Event, Position> entry : events.entrySet()) {
+ Event event = entry.getKey();
+ Position position = entry.getValue();
+ try {
+ cacheManager.addDevice(event.getDeviceId());
+ updateEvent(event, position);
+ } catch (StorageException e) {
+ throw new RuntimeException(e);
+ } finally {
+ cacheManager.removeDevice(event.getDeviceId());
+ }
}
}
}
diff --git a/src/main/java/org/traccar/session/ConnectionManager.java b/src/main/java/org/traccar/session/ConnectionManager.java
index 05c4893fd..fe6521d18 100644
--- a/src/main/java/org/traccar/session/ConnectionManager.java
+++ b/src/main/java/org/traccar/session/ConnectionManager.java
@@ -144,7 +144,10 @@ public class ConnectionManager {
endpointSessions.put(device.getUniqueId(), deviceSession);
sessionsByEndpoint.put(endpoint, endpointSessions);
sessionsByDeviceId.put(device.getId(), deviceSession);
- cacheManager.addDevice(device.getId());
+
+ if (oldSession == null) {
+ cacheManager.addDevice(device.getId());
+ }
return deviceSession;
} else {
@@ -190,8 +193,8 @@ public class ConnectionManager {
public void deviceUnknown(long deviceId) {
updateDevice(deviceId, Device.STATUS_UNKNOWN, null);
DeviceSession deviceSession = sessionsByDeviceId.remove(deviceId);
- cacheManager.removeDevice(deviceId);
if (deviceSession != null) {
+ cacheManager.removeDevice(deviceId);
Endpoint endpoint = new Endpoint(deviceSession.getChannel(), deviceSession.getRemoteAddress());
sessionsByEndpoint.computeIfPresent(endpoint, (e, sessions) -> {
sessions.remove(deviceSession.getUniqueId());
diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java
index 87384f746..abc8ca4c9 100644
--- a/src/main/java/org/traccar/session/cache/CacheManager.java
+++ b/src/main/java/org/traccar/session/cache/CacheManager.java
@@ -60,6 +60,7 @@ public class CacheManager {
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Map<CacheKey, CacheValue> deviceCache = new HashMap<>();
+ private final Map<Long, Integer> deviceReferences = new HashMap<>();
private final Map<Long, Map<Class<? extends BaseModel>, List<Long>>> deviceLinks = new HashMap<>();
private final Map<Long, Position> devicePositions = new HashMap<>();
@@ -136,9 +137,14 @@ public class CacheManager {
public void addDevice(long deviceId) throws StorageException {
try {
lock.writeLock().lock();
- if (!deviceLinks.containsKey(deviceId)) {
+ Integer references = deviceReferences.get(deviceId);
+ if (references != null) {
+ references += 1;
+ } else {
unsafeAddDevice(deviceId);
+ references = 1;
}
+ deviceReferences.put(deviceId, references);
} finally {
lock.writeLock().unlock();
}
@@ -147,8 +153,15 @@ public class CacheManager {
public void removeDevice(long deviceId) {
try {
lock.writeLock().lock();
- if (deviceLinks.containsKey(deviceId)) {
- unsafeRemoveDevice(deviceId);
+ Integer references = deviceReferences.get(deviceId);
+ if (references != null) {
+ references -= 1;
+ if (references <= 0) {
+ unsafeRemoveDevice(deviceId);
+ deviceReferences.remove(deviceId);
+ } else {
+ deviceReferences.put(deviceId, references);
+ }
}
} finally {
lock.writeLock().unlock();