Repository navigation
Expand file tree
/
Copy pathdirect_processor.py
More file actions
150 lines (124 loc) · 6.92 KB
/
Copy pathdirect_processor.py
File metadata and controls
150 lines (124 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
## Goal is to demonstrate receive-process-reply (a processor) pattern.
# Processor subscribes to a topic and upon receipt of the message, the payload is processed
# (e.g. uppercased) and published to another designated topic
import os
import platform
import time
# Import Solace Python API modules from the solace package
from solace.messaging.messaging_service import MessagingService, ReconnectionListener, ReconnectionAttemptListener, \
ServiceInterruptionListener, RetryStrategy, ServiceEvent
from solace.messaging.resources.topic import Topic
from solace.messaging.publisher.direct_message_publisher import PublishFailureListener, FailedPublishEvent
from solace.messaging.resources.topic_subscription import TopicSubscription
from solace.messaging.receiver.message_receiver import MessageHandler, InboundMessage
from solace.messaging.errors.pubsubplus_client_error import PubSubPlusClientError
if platform.uname().system == 'Windows': os.environ["PYTHONUNBUFFERED"] = "1" # Disable stdout buffer
TOPIC_PREFIX = "solace/samples/python"
# Inner class for connection error handling
class ServiceEventHandler(ReconnectionListener, ReconnectionAttemptListener, ServiceInterruptionListener):
def on_reconnected(self, e: ServiceEvent):
print("\non_reconnected")
print(f"Error cause: {e.get_cause()}")
print(f"Message: {e.get_message()}")
def on_reconnecting(self, e: "ServiceEvent"):
print("\non_reconnecting")
print(f"Error cause: {e.get_cause()}")
print(f"Message: {e.get_message()}")
def on_service_interrupted(self, e: "ServiceEvent"):
print("\non_service_interrupted")
print(f"Error cause: {e.get_cause()}")
print(f"Message: {e.get_message()}")
# Inner class for publish error handling
class PublisherErrorHandling(PublishFailureListener):
def on_failed_publish(self, e: "FailedPublishEvent"):
print("on_failed_publish")
# Inner class to handle received messages
class ProcessorImpl(MessageHandler):
def __init__(self, publisher, messaging_service):
self.publisher = publisher
self.msg_builder = messaging_service.message_builder() \
.with_application_message_id("sample_id") \
.with_property("application", "samples") \
.with_property("payload", "processed") \
.with_property("language", "Python") \
def on_message(self, message: InboundMessage):
try:
publish_topic = Topic.of(TOPIC_PREFIX + f'/direct/processor/output')
subscribe_topic = message.get_destination_name()
# Check if the payload is a String or Byte, decode if its the later
payload = message.get_payload_as_string() if message.get_payload_as_string() is not None else message.get_payload_as_bytes()
if isinstance(payload, bytearray):
print(f"Received a message of type: {type(payload)}. Decoding to string")
payload = payload.decode()
# Process the message. For simplicity, we will be uppercasing the payload.
processed_payload = payload.upper()
if message.get_application_message_id() is not None:
self.msg_builder = self.msg_builder \
.with_application_message_id(message.get_application_message_id())
output_msg = self.msg_builder.build(f'{processed_payload}')
self.publisher.publish(destination=publish_topic, message=output_msg)
print(f'<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
print(f'Received input message on {subscribe_topic}')
print(f'Received input message (body): {payload}')
# print(f'Received input message dump (body):{message}')
print(f'----------------------------')
print(f'>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
print(f'Published output message to: {publish_topic}')
print(f'Published output message (body): {processed_payload}')
# print(f'Published output message dump (body):{output_msg}')
print(f'----------------------------')
except Exception as e:
print(f'Error processing message: {e.__traceback__}')
# Broker Config. Note: Could pass other properties Look into
broker_props = {
"solace.messaging.transport.host": os.environ.get('SOLACE_HOST') or "tcp://localhost:55555,tcp://localhost:55554",
"solace.messaging.service.vpn-name": os.environ.get('SOLACE_VPN') or "default",
"solace.messaging.authentication.scheme.basic.username": os.environ.get('SOLACE_USERNAME') or "default",
"solace.messaging.authentication.scheme.basic.password": os.environ.get('SOLACE_PASSWORD') or ""
}
# Build A messaging service with a reconnection strategy of 20 retries over an interval of 3 seconds
# Note: The reconnections strategy could also be configured using the broker properties object
messaging_service = MessagingService.builder().from_properties(broker_props)\
.with_reconnection_retry_strategy(RetryStrategy.parametrized_retry(20,3))\
.build()
# Blocking connect thread
messaging_service.connect()
print(f'\nMessaging Service connected? {messaging_service.is_connected}')
# Event Handling for the messaging service
service_handler = ServiceEventHandler()
messaging_service.add_reconnection_listener(service_handler)
messaging_service.add_reconnection_attempt_listener(service_handler)
messaging_service.add_service_interruption_listener(service_handler)
# Create a direct message publisher and start it
publisher = messaging_service.create_direct_message_publisher_builder().build()
publisher.set_publish_failure_listener(PublisherErrorHandling())
# Blocking Start thread
publisher.start()
print(f'Direct Publisher ready? {publisher.is_ready()}')
# Define a Topic subscriptions
subscribe_topic_name = TOPIC_PREFIX + "/direct/processor/input"
subscribe_topic = [TopicSubscription.of(subscribe_topic_name)]
print(f'\nSubscribed to topic: {subscribe_topic_name}')
# Build a Receiver with the given topics and start it
direct_receiver = messaging_service.create_direct_message_receiver_builder()\
.with_subscriptions(subscribe_topic)\
.build()
direct_receiver.start()
print(f'Direct Receiver is running? {direct_receiver.is_running()}')
print("\nSend a KeyboardInterrupt to stop processor\n")
try:
# Callback for received messages
direct_receiver.receive_async(ProcessorImpl(publisher, messaging_service))
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print('\nDisconnecting Messaging Service')
# Handle API exception
except PubSubPlusClientError as exception:
print(f'\nError occurred: {exception}')
finally:
print('\nTerminating receiver')
direct_receiver.terminate()
print('\nDisconnecting Messaging Service')
messaging_service.disconnect()