/
SubscribedMember.java
57 lines (47 loc) · 1.75 KB
/
SubscribedMember.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.topic.ITopic;
import com.hazelcast.topic.Message;
import com.hazelcast.topic.ReliableMessageListener;
public class SubscribedMember {
public static void main(String[] args) {
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
ITopic<Long> topic = hz.getReliableTopic("sometopic");
topic.addMessageListener(new ExampleReliableMessageListener());
}
private static class ExampleReliableMessageListener implements ReliableMessageListener<Long> {
private long lastSequence;
public long getLastSequence() {
return lastSequence;
}
public void onMessage(Message<Long> m) {
System.out.println("Received: " + m.getMessageObject());
}
@Override
public long retrieveInitialSequence() {
// The initial sequence to start reading from.
return 1;
}
@Override
public void storeSequence(long l) {
// The sequence to store somewhere so that it can be retrieved upon restart.
lastSequence = l;
System.out.println("Stored sequence: " + l);
}
@Override
public boolean isLossTolerant() {
System.out.println("isLossTolerant called");
// If true, the listener will not be removed upon an exception.
return false;
}
@Override
public boolean isTerminal(Throwable throwable) {
System.out.println("isTerminal called");
return false;
}
@Override
public void onCancel() {
System.out.println("onCancel called. The listener is being removed.");
}
}
}