def subscribe_to_requests(channel_name, p_client_id, kube_add): responder = Responder(kube_add) cancel_token = ListenerCancellationToken() sub_req = SubscribeRequest(channel=channel_name, client_id=p_client_id, events_store_type=EventsStoreType.Undefined, events_store_type_value=0, group="", subscribe_type=SubscribeType.Queries) responder.subscribe_to_requests(sub_req, handle_incoming_request, handle_incoming_error, cancel_token) print("sub for 10 seconds") time.sleep(10.0) print("Canceled token") cancel_token.cancel()
def event_subscriber(queue_name, client_id, kube_add): subscriber = Subscriber(kube_add) cancel_token = ListenerCancellationToken() sub_req = SubscribeRequest(channel="MyTestChannelName", client_id=str(randint(9, 19999)), events_store_type=EventsStoreType.Undefined, events_store_type_value=0, group="", subscribe_type=SubscribeType.Events) subscriber.subscribe_to_events(sub_req, handle_incoming_events, handle_incoming_error, cancel_token) print("sub for 2 seconds") time.sleep(2.0) print("Canceled token") cancel_token.cancel()
def subcribe_to_event_store(channel_name, p_client_id, kube_add): subscriber = Subscriber(kube_add) cancel_token = ListenerCancellationToken() sub_req = SubscribeRequest( channel=channel_name, client_id=p_client_id, events_store_type=EventsStoreType.StartFromFirst, events_store_type_value=0, group="", subscribe_type=SubscribeType.EventsStore) subscriber.subscribe_to_events(sub_req, handle_incoming_events, handle_incoming_error, cancel_token) print("sub for 2 seconds") time.sleep(2.0) print("Canceled token") cancel_token.cancel()
if event: print("Subscriber Received Event: Metadata:'%s', Channel:'%s', Body:'%s tags:%s'" % ( event.metadata, event.channel, event.body, event.tags )) def handle_incoming_error(error_msg): print("received error:%s'" % ( error_msg )) if __name__ == "__main__": print("Subscribing to event on channel example") cancel_token=ListenerCancellationToken() # Subscribe to events with store subscriber = Subscriber("localhost:50000") subscribe_request = create_subscribe_request(SubscribeType.EventsStore, EventsStoreType.StartAtSequence, 2) subscriber.subscribe_to_events(subscribe_request, handle_incoming_events,handle_incoming_error,cancel_token) # Subscribe to events without store subscriber = Subscriber("localhost:50000") subscribe_request = create_subscribe_request(SubscribeType.Events) subscriber.subscribe_to_events(subscribe_request, handle_incoming_events,handle_incoming_error,cancel_token) input("Press 'Enter' to stop Listen...\n") cancel_token.cancel() input("Press 'Enter' to stop the application...\n")
def subscribe_to_requests(self, subscribe_request, handler, error_handler, listener_cancellation_token=ListenerCancellationToken()): """ Register to kubeMQ Channel using handler. :param SubscribeRequest subscribe_request: represent by that will determine the subscription configuration. :param handler: Method the perform when receiving RequestReceive :param error_handler: Method the perform when receiving error from kubemq :param listener_cancellation_token: cancellation token, once cancel is called will cancel the subscribe to kubemq :return: A thread running the Subscribe Request. """ if not subscribe_request.channel: raise ValueError("Channel parameter is mandatory.") if not subscribe_request.is_valid_type("CommandQuery"): raise ValueError("Invalid Subscribe Type for this Class.") inner_subscribe_request = subscribe_request.to_inner_subscribe_request() call = self.get_kubemq_client().SubscribeToRequests(inner_subscribe_request, metadata=self._metadata) def subscribe_task(listener_cancellation_token): while True: try: event_receive = call.next() logging.debug("Responder InnerRequest. ID:'%s', Channel:'%s', ReplyChannel:'%s tags:'%s''" % ( event_receive.RequestID, event_receive.Channel, event_receive.ReplyChannel, event_receive.Tags )) if handler: try: response = handler(RequestReceive(event_receive)) logging.debug("Responder InnerResponse. ID:'%s', ReplyChannel:'%s'" % ( response.request_id, response.reply_channel )) self.get_kubemq_client().SendResponse(response.convert(), None, self._metadata) except grpc.RpcError as error: if (listener_cancellation_token.is_cancelled): logging.info("Sub closed by listener request") error_handler(str(error)) else: logging.exception("Subscriber Received Error: Error:'%s'" % (error)) error_handler(str(error)) except Exception as e: logging.exception("Subscriber Received Error: Error:'%s'" % (e)) error_handler(str(e)) except Exception as e: logging.exception("An exception occurred while listening for request:'%s'" % (e)) error_handler(str(e)) raise # re-raise the original exception, keeping full stack trace def check_sub_to_valid(listener_cancellation_token): while True: if (listener_cancellation_token.is_cancelled): logging.info("Sub closed by listener request") call.cancel() return thread = threading.Thread(target=subscribe_task, args=(listener_cancellation_token,)) thread.daemon = True thread.start() listener_thread = threading.Thread(target=check_sub_to_valid, args=(listener_cancellation_token,)) listener_thread.daemon = True listener_thread.start() return thread
def subscribe_to_events( self, subscribe_request, handler, error_handler, listener_cancellation_token=ListenerCancellationToken()): """ Register to kubeMQ Channel using handler. :param SubscribeRequest subscribe_request: represent by that will determine the subscription configuration. :param handler: Method the perform when receiving EventReceive :param error_handler: Method the perform when receiving error from kubemq :param listener_cancellation_token: cancellation token, once cancel is called will cancel the subscribe to kubemq """ if not subscribe_request.channel: raise ValueError("channel parameter is mandatory.") if not subscribe_request.is_valid_type("Events"): raise ValueError("Invalid Subscribe Type for this Class.") if subscribe_request.subscribe_type == SubscribeType.EventsStore: if not subscribe_request.client_id: raise ValueError("client_id parameter is mandatory.") if subscribe_request.events_store_type == EventsStoreType.Undefined: raise ValueError("events_store_type parameter is mandatory.") inner_subscribe_request = subscribe_request.to_inner_subscribe_request( ) call = self.get_kubemq_client().SubscribeToEvents( inner_subscribe_request, metadata=self._metadata) def subscribe_to_event(listener_cancellation_token): try: while True: event_receive = call.next() logging.info( "Subscriber Received Event: EventID:'%s', Channel:'%s', Body:'%s Tags:%s'" % (event_receive.EventID, event_receive.Channel, event_receive.Body, event_receive.Tags)) handler(EventReceive(event_receive)) except grpc.RpcError as error: if (listener_cancellation_token.is_cancelled): logging.info("Sub closed by listener request") else: logging.info("Subscriber Received Error: Error:'%s'" % (str(error))) error_handler(error) except Exception as e: logging.info("Subscriber Received Error: Error:'%s'" % (str(e))) error_handler(str(e)) def check_sub_to_valid(listener_cancellation_token): while True: if (listener_cancellation_token.is_cancelled): logging.info("Sub closed by listener request") call.cancel() return thread = threading.Thread(target=subscribe_to_event, args=(listener_cancellation_token, )) thread.daemon = True thread.start() listener_thread = threading.Thread( target=check_sub_to_valid, args=(listener_cancellation_token, )) listener_thread.daemon = True listener_thread.start()