diff --git a/README.md b/README.md index ab0f48d..a91199b 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,8 @@ This project includes the implementation of the [SmartDataProvider](https://ligh ### Data Adapter The `ChatDataAdapter.java` class contains the source code for the Chat Data Adapter. The Data Adapter accepts message submission for the unique chat room. The sender is identified by an IP address and a nickname. -It's possible to flush chat history based on optional parameters provided in the `adapters.xml` file. +It's possible to configure a custom initial chat history (i.e. snapshot) based on optional parameters provided in the `adapters.xml` file. +It's also possible to flush chat history (and restore any configured custom initial history) based on optional parameters provided in the `adapters.xml` file. ### Metadata Adapter The `ChatMetadataAdapter.java` class contains the source code for a Metadata Adapter to be associated with the Chat Demo Data Adapter. @@ -61,6 +62,13 @@ The `adapters.xml` file for the Basic Chat Demo, should look like: com.lightstreamer.examples.chat_demo.adapters.ChatDataAdapter + + Hello + How're you doing? + diff --git a/pom.xml b/pom.xml index 7de34f6..dfbfadc 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.lightstreamer.examples chat-adapter-java - 1.1.0 + 1.1.1 jar Lightstreamer Chat Demo Java Adapter diff --git a/src/main/java/com/lightstreamer/examples/chat_demo/adapters/ChatDataAdapter.java b/src/main/java/com/lightstreamer/examples/chat_demo/adapters/ChatDataAdapter.java index b66b3ab..af467d4 100644 --- a/src/main/java/com/lightstreamer/examples/chat_demo/adapters/ChatDataAdapter.java +++ b/src/main/java/com/lightstreamer/examples/chat_demo/adapters/ChatDataAdapter.java @@ -20,6 +20,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; +import java.util.LinkedList; import java.util.Map; import java.util.Timer; import java.util.TimerTask; @@ -74,6 +75,11 @@ public class ChatDataAdapter implements SmartDataProvider { */ private volatile Object subscribed; + /** + * A configurable list of messages for a custom initial snapshot. + */ + private final LinkedList customSnapshot = new LinkedList<>(); + /** * Boolean flag for periodic flush of snapshot (call clearSnaphot). */ @@ -115,6 +121,15 @@ public void init(Map params, File configDir) throws DataProviderException { // Read the Adapter Set name, which is supplied by the Server as a parameter String adapterSetId = (String) params.get("adapters_conf.id"); + boolean stop = false; + for (int i = 1; ! stop; i++) { + if (params.containsKey("custom_snapshot_" + i)) { + String msg = (String) params.get("custom_snapshot_" + i); + this.customSnapshot.add(msg); + } else { + stop = true; + } + } if (params.containsKey("flush_snapshot")) { String tmp = (String) params.get("flush_snapshot"); @@ -153,6 +168,8 @@ public void subscribe(String item, Object handle, boolean arg2) subscribed = handle; + sendCustomSnapshot(); + if(this.flushSnapshot) { // Start Thread for periodic flush of the snapshot. myTimer = new Timer(true); @@ -161,6 +178,7 @@ public void subscribe(String item, Object handle, boolean arg2) @Override public void run() { clearHistory(); + sendCustomSnapshot(); } }, new Date(System.currentTimeMillis() + this.flushInterval), this.flushInterval); } @@ -253,6 +271,14 @@ public void run() { return true; } + private void sendCustomSnapshot() { + int i = 1; + for (String snapshotMsg : customSnapshot) { + sendMessage("0.0.0.0", "user" + i, snapshotMsg); + i++; + } + } + // used in case of flush_snapshot set to true. public void clearHistory() { final Object currSubscribed = subscribed;