aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/broadcast
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2023-12-17 11:43:51 -0800
committerAnton Tananaev <anton@traccar.org>2023-12-17 11:43:51 -0800
commitc165968c2eb24b1c4a35dab39174b4df3576551c (patch)
tree6dae35ddec3feb14151e5c711f4d0f5865cbc64e /src/main/java/org/traccar/broadcast
parent4dc2e86ae5e403143b3c4e2c1a3bd671dfeaf5f8 (diff)
downloadtrackermap-server-c165968c2eb24b1c4a35dab39174b4df3576551c.tar.gz
trackermap-server-c165968c2eb24b1c4a35dab39174b4df3576551c.tar.bz2
trackermap-server-c165968c2eb24b1c4a35dab39174b4df3576551c.zip
Improve cache update performance
Diffstat (limited to 'src/main/java/org/traccar/broadcast')
-rw-r--r--src/main/java/org/traccar/broadcast/BaseBroadcastService.java37
-rw-r--r--src/main/java/org/traccar/broadcast/BroadcastInterface.java16
-rw-r--r--src/main/java/org/traccar/broadcast/MulticastBroadcastService.java2
-rw-r--r--src/main/java/org/traccar/broadcast/RedisBroadcastService.java11
4 files changed, 28 insertions, 38 deletions
diff --git a/src/main/java/org/traccar/broadcast/BaseBroadcastService.java b/src/main/java/org/traccar/broadcast/BaseBroadcastService.java
index 1c4660320..01b212c60 100644
--- a/src/main/java/org/traccar/broadcast/BaseBroadcastService.java
+++ b/src/main/java/org/traccar/broadcast/BaseBroadcastService.java
@@ -69,10 +69,8 @@ public abstract class BaseBroadcastService implements BroadcastService {
}
@Override
- public void invalidateObject(
- boolean local,
- Class<? extends BaseModel> clazz, long id,
- ObjectOperation operation) {
+ public <T extends BaseModel> void invalidateObject(
+ boolean local, Class<T> clazz, long id, ObjectOperation operation) {
BroadcastMessage message = new BroadcastMessage();
var invalidateObject = new BroadcastMessage.InvalidateObject();
invalidateObject.setClazz(Permission.getKey(clazz));
@@ -83,11 +81,8 @@ public abstract class BaseBroadcastService implements BroadcastService {
}
@Override
- public void invalidatePermission(
- boolean local,
- Class<? extends BaseModel> clazz1, long id1,
- Class<? extends BaseModel> clazz2, long id2,
- boolean link) {
+ public synchronized <T1 extends BaseModel, T2 extends BaseModel> void invalidatePermission(
+ boolean local, Class<T1> clazz1, long id1, Class<T2> clazz2, long id2, boolean link) {
BroadcastMessage message = new BroadcastMessage();
var invalidatePermission = new BroadcastMessage.InvalidatePermission();
invalidatePermission.setClazz1(Permission.getKey(clazz1));
@@ -101,7 +96,7 @@ public abstract class BaseBroadcastService implements BroadcastService {
protected abstract void sendMessage(BroadcastMessage message);
- protected void handleMessage(BroadcastMessage message) {
+ protected void handleMessage(BroadcastMessage message) throws Exception {
if (message.getDevice() != null) {
listeners.forEach(listener -> listener.updateDevice(false, message.getDevice()));
} else if (message.getPosition() != null) {
@@ -112,17 +107,21 @@ public abstract class BaseBroadcastService implements BroadcastService {
listeners.forEach(listener -> listener.updateCommand(false, message.getCommandDeviceId()));
} else if (message.getInvalidateObject() != null) {
var invalidateObject = message.getInvalidateObject();
- listeners.forEach(listeners -> listeners.invalidateObject(
- false,
- Permission.getKeyClass(invalidateObject.getClazz()), invalidateObject.getId(),
- invalidateObject.getOperation()));
+ for (BroadcastInterface listener : listeners) {
+ listener.invalidateObject(
+ false,
+ Permission.getKeyClass(invalidateObject.getClazz()), invalidateObject.getId(),
+ invalidateObject.getOperation());
+ }
} else if (message.getInvalidatePermission() != null) {
var invalidatePermission = message.getInvalidatePermission();
- listeners.forEach(listener -> listener.invalidatePermission(
- false,
- Permission.getKeyClass(invalidatePermission.getClazz1()), invalidatePermission.getId1(),
- Permission.getKeyClass(invalidatePermission.getClazz2()), invalidatePermission.getId2(),
- invalidatePermission.getLink()));
+ for (BroadcastInterface listener : listeners) {
+ listener.invalidatePermission(
+ false,
+ Permission.getKeyClass(invalidatePermission.getClazz1()), invalidatePermission.getId1(),
+ Permission.getKeyClass(invalidatePermission.getClazz2()), invalidatePermission.getId2(),
+ invalidatePermission.getLink());
+ }
}
}
diff --git a/src/main/java/org/traccar/broadcast/BroadcastInterface.java b/src/main/java/org/traccar/broadcast/BroadcastInterface.java
index 25fdf4d93..d0a491cd2 100644
--- a/src/main/java/org/traccar/broadcast/BroadcastInterface.java
+++ b/src/main/java/org/traccar/broadcast/BroadcastInterface.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2022 - 2023 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,16 +35,12 @@ public interface BroadcastInterface {
default void updateCommand(boolean local, long deviceId) {
}
- default void invalidateObject(
- boolean local,
- Class<? extends BaseModel> clazz, long id,
- ObjectOperation operation) {
+ default <T extends BaseModel> void invalidateObject(
+ boolean local, Class<T> clazz, long id, ObjectOperation operation) throws Exception {
}
- default void invalidatePermission(
- boolean local,
- Class<? extends BaseModel> clazz1, long id1,
- Class<? extends BaseModel> clazz2, long id2,
- boolean link) {
+ default <T1 extends BaseModel, T2 extends BaseModel> void invalidatePermission(
+ boolean local, Class<T1> clazz1, long id1, Class<T2> clazz2, long id2, boolean link) throws Exception {
}
+
}
diff --git a/src/main/java/org/traccar/broadcast/MulticastBroadcastService.java b/src/main/java/org/traccar/broadcast/MulticastBroadcastService.java
index 1c02b319b..793c6df36 100644
--- a/src/main/java/org/traccar/broadcast/MulticastBroadcastService.java
+++ b/src/main/java/org/traccar/broadcast/MulticastBroadcastService.java
@@ -103,7 +103,7 @@ public class MulticastBroadcastService extends BaseBroadcastService {
}
publisherSocket = null;
socket.leaveGroup(group, networkInterface);
- } catch (IOException e) {
+ } catch (Exception e) {
throw new RuntimeException(e);
}
}
diff --git a/src/main/java/org/traccar/broadcast/RedisBroadcastService.java b/src/main/java/org/traccar/broadcast/RedisBroadcastService.java
index e87ad5e61..697c45a4a 100644
--- a/src/main/java/org/traccar/broadcast/RedisBroadcastService.java
+++ b/src/main/java/org/traccar/broadcast/RedisBroadcastService.java
@@ -38,7 +38,6 @@ public class RedisBroadcastService extends BaseBroadcastService {
private final ExecutorService service = Executors.newSingleThreadExecutor();
- private final String url;
private final String channel = "traccar";
private Jedis subscriber;
@@ -48,7 +47,7 @@ public class RedisBroadcastService extends BaseBroadcastService {
public RedisBroadcastService(Config config, ObjectMapper objectMapper) throws IOException {
this.objectMapper = objectMapper;
- url = config.getString(Keys.BROADCAST_ADDRESS);
+ String url = config.getString(Keys.BROADCAST_ADDRESS);
try {
subscriber = new Jedis(url);
@@ -69,9 +68,7 @@ public class RedisBroadcastService extends BaseBroadcastService {
try {
String payload = id + ":" + objectMapper.writeValueAsString(message);
publisher.publish(channel, payload);
- } catch (IOException e) {
- LOGGER.warn("Broadcast failed", e);
- } catch (JedisConnectionException e) {
+ } catch (IOException | JedisConnectionException e) {
LOGGER.warn("Broadcast failed", e);
}
}
@@ -114,13 +111,11 @@ public class RedisBroadcastService extends BaseBroadcastService {
if (messageChannel.equals(channel) && parts.length == 2 && !id.equals(parts[0])) {
handleMessage(objectMapper.readValue(parts[1], BroadcastMessage.class));
}
- } catch (IOException e) {
+ } catch (Exception e) {
LOGGER.warn("Broadcast handleMessage failed", e);
}
}
}, channel);
- } catch (JedisConnectionException e) {
- throw new RuntimeException(e);
} catch (JedisException e) {
throw new RuntimeException(e);
}