-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_nack.py
More file actions
70 lines (61 loc) · 3.03 KB
/
test_nack.py
File metadata and controls
70 lines (61 loc) · 3.03 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
import time
from solace.messaging.messaging_service import MessagingService
from solace.messaging.resources.queue import Queue
from solace.messaging.receiver.persistent_message_receiver import PersistentMessageReceiver
from solace.messaging.config.message_acknowledgement_configuration import Outcome
class HowToConnectMessagingService:
"""class for reconnection strategy sampler"""
@staticmethod
def to_string_api_metrics(service: "MessagingService"):
"""method implies on how to get String representation of all current API metrics using
messaging service instance
Args:
service: service connected instance of a messaging service, ready to be used
"""
metrics: "ApiMetrics" = service.metrics()
print(f"API metrics[ALL]: {metrics}\n")
@staticmethod
def connect_and_process_messages():
"""method to connect and process messages"""
config = {
"solace.messaging.transport.host": "ws://localhost:8008",
"solace.messaging.authentication.scheme.basic.username": "default",
"solace.messaging.authentication.scheme.basic.password": "default",
"solace.messaging.service.vpn-name": "default",
}
messaging_service = MessagingService.builder().from_properties(config).build()
try:
messaging_service.connect()
# HowToConnectMessagingService.to_string_api_metrics(messaging_service)
print("Connected to Solace broker")
queue = Queue.durable_exclusive_queue("test_queue")
# Create receiver with required outcome support
nacking_receiver: PersistentMessageReceiver = (
messaging_service.create_persistent_message_receiver_builder()
.with_required_message_outcome_support(Outcome.FAILED, Outcome.REJECTED)
.build(queue)
)
# # Start the receiver and add subscription
nacking_receiver.start()
try:
# Process messages for a specific duration or message count
end_time = time.time() + 60 # Process for 60 seconds
while time.time() < end_time:
# Receive with timeout (e.g., 1 second)
HowToConnectMessagingService.to_string_api_metrics(
messaging_service
)
msg = nacking_receiver.receive_message(1000)
HowToConnectMessagingService.to_string_api_metrics(
messaging_service
)
if msg:
print("intentionally rejected")
nacking_receiver.settle(msg, Outcome.REJECTED)
else:
print("No message received within timeout")
except Exception as e:
print(f"Error receiving message: {e}")
except Exception as e:
print(f"Error connecting to Solace broker: {e}")
HowToConnectMessagingService.connect_and_process_messages()