|
| 1 | +# PIP-371: Support to specify message listeners in TableView constructor |
| 2 | + |
| 3 | +# Background knowledge |
| 4 | + |
| 5 | +### TableView |
| 6 | +Ref: https://pulsar.apache.org/docs/next/concepts-clients/#tableview |
| 7 | + |
| 8 | +### TableView Listeners |
| 9 | +TableView runs the registered listeners on each received message. TableView users could utilize TableView Listeners for event-sourcing applications. |
| 10 | + |
| 11 | +### How to Register TableView Listeners |
| 12 | + |
| 13 | +Currently, `TableView#foreachAndListen` and `TableView#listen` interfaces are used to register listeners. |
| 14 | + |
| 15 | +# Motivation |
| 16 | +issue : https://github.com/apache/pulsar/issues/23158 |
| 17 | + |
| 18 | +As reported in the above issue, currently, the TableView listener registration methods, `foreachAndListen` and `listen`, are being called after `TableView#start()`. This timing issue can result in some messages being missed or not handled if the listeners are registered too late. |
| 19 | + |
| 20 | +Additionally, the current implementation of these listeners does not distinguish between existing messages (those present before the TableView started) and tail (future) messages (those received after the TableView started). Some business logic may require different handling for these two types of messages, but the current design does not support this differentiation. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +# Goals |
| 25 | + |
| 26 | +## In Scope |
| 27 | + |
| 28 | +- Support specifying TableView Listeners in TableView builder and constructor, so that TableView can register listeners as part of instance init and accordingly call them for all messages. |
| 29 | +- Support specifying TableView Listeners for existing and tail messages separately. |
| 30 | +- This design is based on Pulsar-Java-Client. |
| 31 | + |
| 32 | +# Detailed Design |
| 33 | + |
| 34 | +## Design & Implementation Details |
| 35 | + |
| 36 | +Add followings in TableViewBuilder.java |
| 37 | +```java |
| 38 | + /** |
| 39 | + * Set the message listeners. |
| 40 | + * If {@link TableViewBuilder#existingMessageListeners} are not specified, |
| 41 | + * these listeners are used for both |
| 42 | + * existing and tailing(future) messages in the topic. |
| 43 | + * @param listeners message listeners |
| 44 | + * @return the {@link TableViewBuilder} builder instance |
| 45 | + */ |
| 46 | + TableViewBuilder<T> messageListeners(BiConsumer<String, T>... listeners); |
| 47 | + |
| 48 | + /** |
| 49 | + * Set the message listeners separately for existing messages in the topic. |
| 50 | + * @param listeners message listeners |
| 51 | + * @return the {@link TableViewBuilder} builder instance |
| 52 | + */ |
| 53 | + TableViewBuilder<T> existingMessageListeners(BiConsumer<String, T>... listeners); |
| 54 | + |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | +Add followings in TableViewConfigurationData.java |
| 59 | +```java |
| 60 | + private BiConsumer[] messageListeners; |
| 61 | + private BiConsumer[] existingMessageListeners; |
| 62 | +``` |
| 63 | + |
| 64 | +Add followings in TableViewImpl.java |
| 65 | +```java |
| 66 | +private final List<BiConsumer<String, T>> existingMessageListeners; |
| 67 | +``` |
| 68 | + |
| 69 | +Update followings in TableViewImpl.java |
| 70 | +```java |
| 71 | + |
| 72 | +// before |
| 73 | +private void handleMessage(Message<T> msg) |
| 74 | +// after |
| 75 | +private void handleMessage(Message<T> msg, boolean handleExistingMessage) |
| 76 | + |
| 77 | +``` |
| 78 | + |
| 79 | +# Backward & Forward Compatibility |
| 80 | + |
| 81 | +Compatible. |
| 82 | + |
| 83 | +# Links |
| 84 | + |
| 85 | +<!-- |
| 86 | +Updated afterwards |
| 87 | +--> |
| 88 | +* Mailing List discussion thread: |
| 89 | +* Mailing List voting thread: |
0 commit comments