0

I'm using Hazelcast multimap. how to create streaming API for Hazelcast multimap listener based on my key. I need to use that API in any frontend applications. if any changes come into the Hazelcast map that needs to be reflected into the API.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
Code_X
  • 11
  • 2

1 Answers1

0
import logging

import hazelcast

logging.basicConfig(level=logging.INFO)

client = hazelcast.HazelcastClient()

multi_map = client.get_multi_map("test").blocking()


def on_entry_added(entry_event):
    print(entry_event)
    # Do something with the event.
    # Do not perform blocking operations here.
    # If you need to perform blocking operations
    # offload them to another thread.


multi_map.add_entry_listener(
    include_value=True,
    key="your key",
    added_func=on_entry_added,
)


for i in range(1000):
    multi_map.put("your key", i)
mdumandag
  • 256
  • 2
  • 6
  • Thanks for your reply. In my case, I want to create a streaming API in python. hazelcast key is argument and streams all the records as a response. if I update the value in the key (hazelcast) it reflects the API (Web, Mobile). – Code_X Feb 24 '22 at 12:38
  • I have updated my answer with the python code sample. Note that, I was wrong in my initial answer saying that there is no such API today, but it seems there were, and I simply missed it. My updated answer adds the listener to a single key. Of course, if you want to listen to events for all keys, you can omit the `key` parameter in the `add_entry_listener` method – mdumandag Feb 24 '22 at 13:04
  • is it possible to check listener is activated for the key? (Duplicated Hazelcast Listeners) – Code_X Feb 28 '22 at 13:10
  • No, but `add_entry_listener` returns a registration id (a string), and there is a method to remove a listener with that id (`remove_entry_listener`). Maybe you can make use of those – mdumandag Feb 28 '22 at 20:14