Skip to content

Commit

Permalink
Add support for configurable initial snapshot.
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-weswit committed Nov 6, 2024
1 parent 0b22d92 commit 90f8229
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -61,6 +62,13 @@ The `adapters.xml` file for the Basic Chat Demo, should look like:

<adapter_class>com.lightstreamer.examples.chat_demo.adapters.ChatDataAdapter</adapter_class>

<!-- Optional for ChatDataAdapter.
Configuration of a custom initial snapshot at subscription time.
Use progressive numbers after "custom_snapshot_", starting from 1.
Default: empty snapshot. -->
<param name="custom_snapshot_1">Hello</param>
<param name="custom_snapshot_2">How're you doing?</param>

<!-- Optional for ChatDataAdapter.
Configuration flag for periodic flush of the snapshot.
Default: false. -->
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.lightstreamer.examples</groupId>
<artifactId>chat-adapter-java</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<packaging>jar</packaging>

<name>Lightstreamer Chat Demo Java Adapter</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> customSnapshot = new LinkedList<>();

/**
* Boolean flag for periodic flush of snapshot (call clearSnaphot).
*/
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 90f8229

Please sign in to comment.