def when_tcp_reconnection_happens(): """method to test the listener service event""" attempt_listener = ReconnectionAttemptListenerImpl() listener = ReconnectionListenerImpl() messaging_service = MessagingService.builder().from_properties(boot.broker_properties()) \ .with_reconnection_retry_strategy(RetryStrategy.parametrized_retry(3, 3)).build(). \ add_reconnection_attempt_listener(attempt_listener). \ add_reconnection_listener(listener) messaging_service.connect() # If message disconnect occurs due to any network issue, service will automatically get connects again # Apply some logic to simulate network issue, upon message service disconnect, # this listener will notify once it is automatically connect/disconnect messaging_service.remove_reconnection_listener(listener)
def connect_retry_interval(retry_interval): """ creates a new instance of message service, that is used to configure direct message instances from config Returns: new connection for Direct messaging Raises: PubSubPlusClientError """ try: messaging_service = MessagingService.builder().from_properties(boot.broker_properties()) \ .with_reconnection_retry_strategy(RetryStrategy.forever_retry(retry_interval)).build() messaging_service.connect() return messaging_service except PubSubPlusClientError as exception: raise exception finally: messaging_service.disconnect()
def add_listener_when_reconnection_happens(retries: int, retry_interval: int) -> 'MessagingService': """method adds a reconnection listener when an reconnection happens using the reconnection strategy Args: retries (int): the number of retries count retry_interval (int): the retry interval value Returns: the listener events """ events = list() def on_reconnection(event_p): events.append(event_p) print("current event", events) try: destination_name = Topic.of(constants.TOPIC_ENDPOINT_DEFAULT) message = constants.MESSAGE_TO_SEND number_of_message_to_send = 10 messaging_service = MessagingService.builder().from_properties(boot.broker_properties()) \ .with_reconnection_retry_strategy(RetryStrategy.parametrized_retry(retries, retry_interval)) \ .add_reconnection_listener(on_reconnection).build() messaging_service1_status_code = messaging_service.connect_async() print("messaging service status code: ", messaging_service1_status_code) for _ in range(number_of_message_to_send): publish_service = messaging_service.create_direct_message_publisher_builder().build() publish_service.publish(destination=destination_name, message=message) session_force_failure_status = messaging_service.disconnect_force() print("session force failure status: ", session_force_failure_status) for _ in range(number_of_message_to_send): publish_service = messaging_service.create_direct_message_publisher_builder().build() publish_service.publish(destination=destination_name, message=message) messaging_service.disconnect() finally: timeout = time.time() + 60 * 1 while True: if 13 in events or time.time() > timeout: break return events # MessagingService got list
def add_listener_when_reconnection_happens( retries: int, retry_interval: int) -> 'MessagingService': """method adds a reconnection listener when an reconnection happens using the reconnection strategy Args: retries (int): the number of retries count retry_interval (int): the retry interval value Returns: the listener events """ events = list() class SampleServiceReconnectionHandler(ReconnectionAttemptListener, ReconnectionListener): def __init__(self): self.events = list() def on_reconnecting(event): self.events.append(event) print( 'Got reconnection attempt event, current reconnection events {self.events}' ) def on_reconnected(self, event): self.events.append(event) print( 'Got reconnected event, current reconnection events {self.events}' ) messaging_service = MessagingService.builder().from_properties(boot.broker_properties()) \ .with_reconnection_retry_strategy(RetryStrategy.parametrized_retry(retries, retry_interval)) \ .build() event_handler = SampleServiceReconnectionHandler() try: messaging_service.add_reconnection_listener(event_handler) messaging_service.connect() # wait for reconnection here # for now ommitting this code as it requires external connection administration finally: messaging_service.disconnect() return event_handler.events # MessagingService got list
def connect_parametrized_retry(retries, retry_interval): """ creates a new instance of message service, that is used to configure direct message instances from config Returns: new connection for Direct messaging Raises: PubSubPlusClientError """ try: message_service = MessagingService.builder() \ .from_properties(boot.broker_properties()) \ .with_reconnection_retry_strategy(RetryStrategy.parametrized_retry(retries, retry_interval)) \ .build(SamplerUtil.get_new_application_id()) return message_service.connect() except PubSubPlusClientError as exception: print(f'Exception: {exception}') raise exception finally: message_service.disconnect()
print("\non_service_interrupted") print(f"Error cause: {e.get_cause()}") print(f"Message: {e.get_message()}") # Broker Config. Note: Could pass other properties Look into broker_props = { "solace.messaging.transport.host": os.environ.get('SOLACE_HOST') or "localhost", "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 "default" } # 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'Messaging 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) # Queue name. # NOTE: This assumes that a persistent queue already exists on the broker with the right topic subscription queue_name = "sample-queue"