Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the documentation for ReliableMessageListener [API-2182] #941

Merged
merged 23 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
991e2f1
Added the documentation section to describe the ReliableListener inte…
ihsandemir Dec 25, 2023
4021b23
Update docs/modules/data-structures/pages/reliable-topic.adoc
ihsandemir Dec 26, 2023
a75d42d
Update docs/modules/data-structures/pages/reliable-topic.adoc
ihsandemir Dec 26, 2023
9727726
Update docs/modules/data-structures/pages/reliable-topic.adoc
ihsandemir Dec 26, 2023
0efad8f
Review comment fix. Added github reference links.
ihsandemir Dec 26, 2023
7f940e9
Merge branch 'relaibleListenerDoc' of github.com:ihsandemir/hz-docs i…
ihsandemir Dec 26, 2023
74b4cd0
Review comment fix. Moved the ReliableMessageListener documentation i…
ihsandemir Dec 27, 2023
038cd7c
Update docs/modules/data-structures/pages/reliable-topic.adoc
ihsandemir Jan 3, 2024
133219f
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 3, 2024
78c2210
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 3, 2024
ba39cb8
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 3, 2024
cdfa57b
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 3, 2024
caef364
Review comment fixes.
ihsandemir Jan 3, 2024
dd656a8
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 3, 2024
1309801
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 3, 2024
cacd565
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 4, 2024
e39d5d5
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 4, 2024
a1341c8
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 4, 2024
2be4e9d
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 4, 2024
aa36a7c
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 4, 2024
eebbcfb
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 4, 2024
ef83091
Update docs/modules/events/pages/object-events.adoc
ihsandemir Jan 4, 2024
76aadeb
Review comment fixes.
ihsandemir Jan 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/modules/data-structures/pages/reliable-topic.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,4 @@ messages when they are added or removed.
tries to read in batches. Its default value is 10.
* `topic-overload-policy`: Policy to handle an overloaded topic.
Available values are `DISCARD_OLDEST`, `DISCARD_NEWEST`, `BLOCK` and `ERROR`.
Its default value is `BLOCK`. See <<slow-consumers, Slow Consumers>> for definitions of these policies.
The default value is `BLOCK`. See <<slow-consumers, Slow Consumers>> for definitions of these policies.
1 change: 1 addition & 0 deletions docs/modules/events/pages/distributed-events.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ For distributed object events:
* **Entry Listener** for `IMap` and `MultiMap` entry events
* **Item Listener** for `IQueue`, `ISet` and `IList` item events
* **Message Listener** for `ITopic` message events
* **Reliable Message Listener** for `ReliableTopic` message events

For Hazelcast JCache implementation:

Expand Down
78 changes: 78 additions & 0 deletions docs/modules/events/pages/object-events.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,84 @@ public class ExampleMessageListener implements MessageListener<MyEvent> {
}
----

[[reliable-message-listener]]
== ReliableMessageListener

link:https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/java/com/hazelcast/topic/ReliableMessageListener.java[ReliableMessageListener] is a link:https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/java/com/hazelcast/topic/MessageListener.java[MessageListener] to better integrate with the reliable topic.

If a standard `MessageListener` is registered on a reliable topic, it acts only as an asynchronous event handler for messages; however, a `ReliableMessageListener` allows you to control the behavior of the listener.

If a `ReliableMessageListener` is registered on a normal `ITopic`, only the methods inherited from `MessageListener` are called.

For example, a Reliable Message Listener class can be defined as follows:
[source,java]
----
public class ExampleReliableMessageListener implements ReliableMessageListener<Long> {
private long 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.");
}
}
----

**Durable Subscription**

You can use the `ReliableMessageListener` to control which message to start processing from when the listener is registered. This supports the creation of a durable subscription by storing the sequence of the last message and then re-starting from the specified `sequenceId`.

**Exception Handling**

The `ReliableMessageListener` also copes with exceptions using the `isTerminal(Throwable)` method. This method allows you to control which exceptions can terminate and cancel the listener. If a `MessageListener` is used, it doesn't terminate following an exception and continues to run. In some situations, such as cluster being stopped, it is better to cancel the listener following the exception.

**Global Order**

The `ReliableMessageListener` always gets all events in order (global order). It does not get duplicates, and gaps (loss of messages) only occur when it is too slow. For more information on dealing with message loss, refer to the `isLossTolerant()` method in the https://docs.hazelcast.org/docs/5.4.0-SNAPSHOT/javadoc/com/hazelcast/topic/ReliableMessageListener.html#isLossTolerant()[Java API documentation^].

**Delivery Guarantees**

The `ReliableMessageListener` controls the item to continue from following a restart, providing an at-least-once or at-most-once delivery guarantee. The `storeSequence(long sequence)` is always called before a message is processed; so it can be persisted on non-volatile storage. When the `retrieveInitialSequence()` returns the stored sequence, an at-least-once delivery is implemented as the same item is processed again. To implement an at-most-once delivery guarantee, add 1 to the stored sequence when the `retrieveInitialSequence()` is called.

**Loss Tolerance**

If this `ReliableMessageListener` can deal with message loss, the `boolean isLosstolerant()` method returns `true`. Even though the reliable topic promises to be reliable, a `MessageListener` can be too slow and eventually this causes the message to become unavailable.

If the `ReliableMessageListener` is not loss tolerant and the topic detects that there are missing messages, the `ReliableMessageListener` terminates.

**onCancel Callback**

This method is called by Hazelcast when the `ReliableMessageListener` is canceled. This can happen when the listener is unregistered, or when it has been canceled due to an exception or shutdown.

=== Registering Message Listeners

After you create your class, you can configure your cluster to include
Expand Down
Loading