Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 12 additions & 8 deletions patterns/hello_world_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from solace.messaging.publisher.direct_message_publisher import PublishFailureListener
from solace.messaging.resources.topic_subscription import TopicSubscription
from solace.messaging.receiver.message_receiver import MessageHandler
from solace.messaging.config.solace_properties.message_properties import APPLICATION_MESSAGE_ID
# from solace.messaging.core.solace_message import SolaceMessage
from solace.messaging.resources.topic import Topic

Expand Down Expand Up @@ -90,11 +91,11 @@ def on_failed_publish(self, e: "FailedPublishEvent"):
msgSeqNum = 0
# Prepare outbound message payload and body
message_body = f'Hello from Python Hellow World Sample!'
outbound_msg = messaging_service.message_builder() \
message_builder = messaging_service.message_builder() \
.with_application_message_id("sample_id") \
.with_property("application", "samples") \
.with_property("language", "Python") \
.build(message_body)

try:
print(f"Subscribed to: {topics}")
# Build a Receiver
Expand All @@ -106,13 +107,16 @@ def on_failed_publish(self, e: "FailedPublishEvent"):
print("Connected and Subscribed! Ready to publish\n")
try:
while not SHUTDOWN:
# Direct publish the message
direct_publisher.publish(destination=Topic.of(TOPIC_PREFIX + f"/python/{unique_name}/{msgSeqNum}"), message=outbound_msg)
msgSeqNum += 1
# Modifying the outbond message instead of creating a new one
outbound_msg.solace_message.message_set_binary_attachment_string(f'{message_body} --> {msgSeqNum}')
outbound_msg.solace_message.set_message_application_message_id(f'sample_id {msgSeqNum}')
time.sleep(0.1)
# Check https://docs.solace.com/API-Developer-Online-Ref-Documentation/python/source/rst/solace.messaging.config.solace_properties.html for additional message properties
# Note: additional properties override what is set by the message_builder
additional_properties = {APPLICATION_MESSAGE_ID: f'sample_id {msgSeqNum}'}
# Creating a dynamic outbound message
outbound_message = message_builder.build(f'{message_body} --> {msgSeqNum}', additional_message_properties=additional_properties)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to comment that applciation id is override from the value of set in the builder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that's a good idea 👍 will add it now

# Direct publish the message
direct_publisher.publish(destination=Topic.of(TOPIC_PREFIX + f"/python/{unique_name}/{msgSeqNum}"), message=outbound_message)
# sleep are not necessary when dealing with the default back pressure elastic
# time.sleep(0.1)
except KeyboardInterrupt:
print('\nDisconnecting Messaging Service')
except PubSubPlusClientError as exception:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ certifi==2020.12.5
chardet==4.0.0
idna==2.10
requests==2.25.1
solace-pubsubplus==1.1.0
solace-pubsubplus==1.2.0
urllib3==1.26.4