From 54477507d2665d70f7dd7b501ae1857e59bc9a66 Mon Sep 17 00:00:00 2001 From: ipatini Date: Sat, 7 Oct 2023 17:06:58 +0300 Subject: [PATCH] EMS: Control Service: Configured gson instance in TopicBeacon to serialize/deserialize Instant type --- .../imu/ems/control/util/TopicBeacon.java | 81 ++++++++++++++----- 1 file changed, 59 insertions(+), 22 deletions(-) diff --git a/ems-core/control-service/src/main/java/gr/iccs/imu/ems/control/util/TopicBeacon.java b/ems-core/control-service/src/main/java/gr/iccs/imu/ems/control/util/TopicBeacon.java index a4a4fe5..15c3d98 100644 --- a/ems-core/control-service/src/main/java/gr/iccs/imu/ems/control/util/TopicBeacon.java +++ b/ems-core/control-service/src/main/java/gr/iccs/imu/ems/control/util/TopicBeacon.java @@ -10,6 +10,9 @@ package gr.iccs.imu.ems.control.util; import com.google.gson.*; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; import gr.iccs.imu.ems.baguette.server.NodeRegistryEntry; import gr.iccs.imu.ems.brokercep.BrokerCepService; import gr.iccs.imu.ems.brokercep.event.EventMap; @@ -27,6 +30,7 @@ import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.stereotype.Service; import javax.jms.JMSException; +import java.io.IOException; import java.io.Serializable; import java.time.Duration; import java.time.Instant; @@ -59,25 +63,7 @@ public class TopicBeacon implements InitializingBean { } // initialize a Gson instance - gson = new GsonBuilder() - .disableHtmlEscaping() - .registerTypeAdapter(Instant.class, - (JsonSerializer) (src, typeOfSrc, context) -> - (src==null) ? JsonNull.INSTANCE : new JsonPrimitive(src.toString())) - .registerTypeAdapter(Instant.class, - (JsonDeserializer) (json, typeOfT, context) -> { - if (json.isJsonNull()) return null; - if (!json.isJsonPrimitive()) - throw new IllegalArgumentException(""); - JsonPrimitive primitive = json.getAsJsonPrimitive(); - if (!primitive.isString()) - throw new IllegalArgumentException(""); - String s = primitive.getAsString(); - if (StringUtils.isEmpty(s)) - return null; - return Instant.parse(s); - }) - .create(); + initializeGson(); // configure and start scheduler Date startTime = new Date(System.currentTimeMillis() + properties.getInitialDelay()); @@ -101,6 +87,49 @@ public class TopicBeacon implements InitializingBean { } } + private void initializeGson() { + gson = new GsonBuilder() + .disableHtmlEscaping() + .registerTypeAdapter(Instant.class, new TypeAdapter() { + @Override + public void write(JsonWriter out, Instant value) throws IOException { + if (value==null) + out.nullValue(); + else + out.jsonValue(value.toString()); + } + + @Override + public Instant read(JsonReader in) throws IOException { + JsonToken token = in.peek(); + if (token==JsonToken.STRING) { + String s = in.nextString(); + if (StringUtils.isEmpty(s)) + return null; + return Instant.parse(s); + } else + throw new IllegalArgumentException("InstantGsonTypeAdapter: Expected a string primitive but encountered "+token); + } + }.nullSafe()) + .registerTypeAdapter(Instant.class, + (JsonSerializer) (src, typeOfSrc, context) -> + (src==null) ? JsonNull.INSTANCE : new JsonPrimitive(src.toString())) + .registerTypeAdapter(Instant.class, + (JsonDeserializer) (json, typeOfT, context) -> { + if (json.isJsonNull()) return null; + if (!json.isJsonPrimitive()) + throw new IllegalArgumentException("InstantGsonDeserializer [1]: Expected a string primitive but encountered "+json); + JsonPrimitive primitive = json.getAsJsonPrimitive(); + if (!primitive.isString()) + throw new IllegalArgumentException("InstantGsonDeserializer [2]: Expected a string primitive but encountered "+json); + String s = primitive.getAsString(); + if (StringUtils.isEmpty(s)) + return null; + return Instant.parse(s); + }) + .create(); + } + private Set emptyIfNull(Set s) { if (s==null) return Collections.emptySet(); return s; @@ -174,10 +203,10 @@ public class TopicBeacon implements InitializingBean { String nodeName = node.getPreregistration().getOrDefault("name", ""); String nodeIp = node.getIpAddress(); //String nodeIp = node.getPreregistration().getOrDefault("ip",""); - String message = gson.toJson(node); +// String message = gson.toJson(node); log.debug("Topic Beacon: Transmitting Instance info for: instance={}, ip-address={}, message={}, topics={}", - nodeName, nodeIp, message, properties.getInstanceTopics()); - sendEventToTopics(message, properties.getInstanceTopics()); + nodeName, nodeIp, node, properties.getInstanceTopics()); + sendEventToTopics(node, properties.getInstanceTopics()); } } } @@ -190,6 +219,14 @@ public class TopicBeacon implements InitializingBean { sendMessageToTopics(event, topics); } + private void sendEventToTopics(Object message, Set topics) throws JMSException { + EventMap event = new EventMap(-1); + event.put("message", message); + String s = gson.toJson(event); + log.trace("Topic Beacon: Converted event to JSON string: {}", s); + sendMessageToTopics(s, topics); + } + private void sendMessageToTopics(Serializable event, Set topics) throws JMSException { for (String topicName : topics) { log.trace("Topic Beacon: Sending event to topic: event={}, topic={}", event, topicName);