Topic

class Topic(service_name, name, context)[source]

Bases: PartitionSpecificProxy[BlockingTopic], Generic[MessageType]

Hazelcast provides distribution mechanism for publishing messages that are delivered to multiple subscribers, which is also known as a publish/subscribe (pub/sub) messaging model.

Publish and subscriptions are cluster-wide. When a member subscribes for a topic, it is actually registering for messages published by any member in the cluster, including the new members joined after you added the listener.

Messages are ordered, meaning that listeners(subscribers) will process the messages in the order they are actually published.

add_listener(on_message: Optional[Callable[[TopicMessage[MessageType]], None]] = None) Future[str][source]

Subscribes to this topic.

When someone publishes a message on this topic, on_message function is called if provided.

Parameters

on_message – Function to be called when a message is published.

Returns

A registration id which is used as a key to remove the listener.

publish(message: MessageType) Future[None][source]

Publishes the message to all subscribers of this topic

Parameters

message – The message to be published.

remove_listener(registration_id: str) Future[bool][source]

Stops receiving messages for the given message listener.

If the given listener already removed, this method does nothing.

Parameters

registration_id – Registration id of the listener to be removed.

Returns

True if the listener is removed, False otherwise.

blocking() BlockingTopic[MessageType][source]

Returns a version of this proxy with only blocking method calls.

class BlockingTopic(wrapped: Topic[MessageType])[source]

Bases: Topic[MessageType]

name
service_name
add_listener(on_message: Optional[Callable[[TopicMessage[MessageType]], None]] = None) str[source]

Subscribes to this topic.

When someone publishes a message on this topic, on_message function is called if provided.

Parameters

on_message – Function to be called when a message is published.

Returns

A registration id which is used as a key to remove the listener.

publish(message: MessageType) None[source]

Publishes the message to all subscribers of this topic

Parameters

message – The message to be published.

remove_listener(registration_id: str) bool[source]

Stops receiving messages for the given message listener.

If the given listener already removed, this method does nothing.

Parameters

registration_id – Registration id of the listener to be removed.

Returns

True if the listener is removed, False otherwise.

destroy() bool[source]

Destroys this proxy.

Returns

True if this proxy is destroyed successfully, False otherwise.

blocking() BlockingTopic[MessageType][source]

Returns a version of this proxy with only blocking method calls.