Skip to content

Commit 90f8229

Browse files
committed
Add support for configurable initial snapshot.
1 parent 0b22d92 commit 90f8229

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ This project includes the implementation of the [SmartDataProvider](https://ligh
1414
### Data Adapter
1515
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.
1616

17-
It's possible to flush chat history based on optional parameters provided in the `adapters.xml` file.
17+
It's possible to configure a custom initial chat history (i.e. snapshot) based on optional parameters provided in the `adapters.xml` file.
18+
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.
1819

1920
### Metadata Adapter
2021
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:
6162

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

65+
<!-- Optional for ChatDataAdapter.
66+
Configuration of a custom initial snapshot at subscription time.
67+
Use progressive numbers after "custom_snapshot_", starting from 1.
68+
Default: empty snapshot. -->
69+
<param name="custom_snapshot_1">Hello</param>
70+
<param name="custom_snapshot_2">How're you doing?</param>
71+
6472
<!-- Optional for ChatDataAdapter.
6573
Configuration flag for periodic flush of the snapshot.
6674
Default: false. -->

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.lightstreamer.examples</groupId>
66
<artifactId>chat-adapter-java</artifactId>
7-
<version>1.1.0</version>
7+
<version>1.1.1</version>
88
<packaging>jar</packaging>
99

1010
<name>Lightstreamer Chat Demo Java Adapter</name>

src/main/java/com/lightstreamer/examples/chat_demo/adapters/ChatDataAdapter.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.text.SimpleDateFormat;
2121
import java.util.Date;
2222
import java.util.HashMap;
23+
import java.util.LinkedList;
2324
import java.util.Map;
2425
import java.util.Timer;
2526
import java.util.TimerTask;
@@ -74,6 +75,11 @@ public class ChatDataAdapter implements SmartDataProvider {
7475
*/
7576
private volatile Object subscribed;
7677

78+
/**
79+
* A configurable list of messages for a custom initial snapshot.
80+
*/
81+
private final LinkedList<String> customSnapshot = new LinkedList<>();
82+
7783
/**
7884
* Boolean flag for periodic flush of snapshot (call clearSnaphot).
7985
*/
@@ -115,6 +121,15 @@ public void init(Map params, File configDir) throws DataProviderException {
115121
// Read the Adapter Set name, which is supplied by the Server as a parameter
116122
String adapterSetId = (String) params.get("adapters_conf.id");
117123

124+
boolean stop = false;
125+
for (int i = 1; ! stop; i++) {
126+
if (params.containsKey("custom_snapshot_" + i)) {
127+
String msg = (String) params.get("custom_snapshot_" + i);
128+
this.customSnapshot.add(msg);
129+
} else {
130+
stop = true;
131+
}
132+
}
118133

119134
if (params.containsKey("flush_snapshot")) {
120135
String tmp = (String) params.get("flush_snapshot");
@@ -153,6 +168,8 @@ public void subscribe(String item, Object handle, boolean arg2)
153168

154169
subscribed = handle;
155170

171+
sendCustomSnapshot();
172+
156173
if(this.flushSnapshot) {
157174
// Start Thread for periodic flush of the snapshot.
158175
myTimer = new Timer(true);
@@ -161,6 +178,7 @@ public void subscribe(String item, Object handle, boolean arg2)
161178
@Override
162179
public void run() {
163180
clearHistory();
181+
sendCustomSnapshot();
164182
}
165183
}, new Date(System.currentTimeMillis() + this.flushInterval), this.flushInterval);
166184
}
@@ -253,6 +271,14 @@ public void run() {
253271
return true;
254272
}
255273

274+
private void sendCustomSnapshot() {
275+
int i = 1;
276+
for (String snapshotMsg : customSnapshot) {
277+
sendMessage("0.0.0.0", "user" + i, snapshotMsg);
278+
i++;
279+
}
280+
}
281+
256282
// used in case of flush_snapshot set to true.
257283
public void clearHistory() {
258284
final Object currSubscribed = subscribed;

0 commit comments

Comments
 (0)