def direct_message_consume_adding_subscriptions( messaging_service: MessagingService, consumer_subscription: str, listener_topics: list): """ to publish str or byte array type message""" try: topics = [TopicSubscription.of(consumer_subscription)] direct_receive_service = messaging_service.create_direct_message_receiver_builder( ) direct_receive_service = direct_receive_service.with_subscriptions( topics).build() direct_receive_service.start() direct_receive_service.receive_async(MessageHandlerImpl1()) for topic in listener_topics: direct_receive_service.add_subscription( TopicSubscription.of(topic)) print(f"Subscribed to: {consumer_subscription}") while True: global MAX_SLEEP if MAX_SLEEP <= 0: break else: MAX_SLEEP -= 1 time.sleep(1) finally: direct_receive_service.terminate(0)
def direct_message_consume(messaging_service: MessagingService, consumer_subscription: str): """ to publish str or byte array type message""" try: global MAX_SLEEP topics = [TopicSubscription.of(consumer_subscription)] direct_receive_service = messaging_service.create_direct_message_receiver_builder( ) direct_receive_service = direct_receive_service.with_subscriptions( topics).build() direct_receive_service.start() direct_receive_service.receive_async(MessageHandlerImpl()) remaining_time = MAX_SLEEP print(f"Subscribed to: {consumer_subscription}") time.sleep(MAX_SLEEP) finally: api_metrics = HowToAccessApiMetrics() api_metrics.access_individual_api_metrics( messaging_service, Metric.TOTAL_MESSAGES_RECEIVED) api_metrics.to_string_api_metrics(messaging_service) api_metrics.reset_api_metrics(messaging_service) direct_receive_service.terminate()
def consume_direct_message_published_from_rest_client( service: MessagingService, consumer_subscription: str): """To consume direct message payload with content type and content encoding using receive_message()""" try: topics = [TopicSubscription.of(consumer_subscription)] receiver: DirectMessageReceiver = service.create_direct_message_receiver_builder()\ .with_subscriptions(topics).build() receiver.start() with ThreadPoolExecutor(max_workers=1) as e: e.submit(HowToDirectPublishMessage. direct_message_publish_outbound_with_all_props, messaging_service=service, destination=Topic.of(consumer_subscription), message=constants.MESSAGE_TO_SEND) message_payload: 'InboundMessage' = receiver.receive_message() rest_specific_fields = message_payload.get_rest_interoperability_support( ) content_type = rest_specific_fields.get_http_content_type() content_encoding = rest_specific_fields.get_http_content_encoding() print( f"received message content type is :{content_type} \n content encoding is : {content_encoding}" ) finally: receiver.terminate()
def blocking_consume_direct_messages_in_loop_with_time_out( service: MessagingService, consumer_subscription: str, receive_timeout): try: topics = [TopicSubscription.of(consumer_subscription)] receiver: DirectMessageReceiver = service.create_direct_message_receiver_builder()\ .with_subscriptions(topics).build() receiver.start() count = 0 for count in range(100): try: with ThreadPoolExecutor(max_workers=1) as e: e.submit( HowToDirectPublishMessage.direct_message_publish, messaging_service=service, destination=Topic.of(consumer_subscription), message=constants.MESSAGE_TO_SEND) message_payload: 'InboundMessage' = receiver.receive_message( receive_timeout) print( f"message_payload in string: {message_payload.get_payload_as_string()}, msg_count: {count}" ) except PubSubPlusClientError as exception: raise exception finally: receiver.terminate()
def direct_message_consume_adding_subscriptions( messaging_service: MessagingService, consumer_subscription: str, listener_topics: list): """ to publish str or byte array type message Args: messaging_service: connected messaging service consumer_subscription: Each topic subscribed listener_topics: list of topics subscribed to """ try: global MAX_SLEEP topics = [TopicSubscription.of(consumer_subscription)] direct_receive_service = messaging_service.create_direct_message_receiver_builder( ) direct_receive_service = direct_receive_service.with_subscriptions( topics).build() direct_receive_service.start() direct_receive_service.receive_async(MessageHandlerImpl1()) for topic in listener_topics: direct_receive_service.add_subscription( TopicSubscription.of(topic)) print(f"Subscribed to: {consumer_subscription}") time.sleep(MAX_SLEEP) finally: direct_receive_service.terminate()
def consume_direct_message_byte_payload(service: MessagingService, consumer_subscription: str): """To consume direct message payload in bytes using receive_message()""" try: topics = [TopicSubscription.of(consumer_subscription)] receiver: DirectMessageReceiver = service.create_direct_message_receiver_builder()\ .with_subscriptions(topics).build() receiver.start() with ThreadPoolExecutor(max_workers=1) as e: e.submit(HowToDirectPublishMessage.direct_message_publish, messaging_service=service, destination=Topic.of(consumer_subscription), message=constants.MESSAGE_TO_SEND) message_payload = receiver.receive_message().get_payload_as_bytes() print(f"received message payload in bytes is : {message_payload}") finally: receiver.terminate()
def direct_message_consume_for_business_obj( messaging_service: MessagingService, consumer_subscription: str): """ to publish str or byte array type message""" try: global MAX_SLEEP topics = [TopicSubscription.of(consumer_subscription)] direct_receive_service = messaging_service.create_direct_message_receiver_builder( ) direct_receive_service = direct_receive_service.with_subscriptions( topics).build() direct_receive_service.start() direct_receive_service.receive_async(MessageHandlerImpl()) print(f"Subscribed to: {consumer_subscription}") time.sleep(MAX_SLEEP) finally: direct_receive_service.terminate() messaging_service.disconnect()
def direct_message_consume2(messaging_service: MessagingService, consumer_subscription: str): """This method will create an receiver instance to receive str or byte array type message""" try: global MAX_SLEEP topics = [TopicSubscription.of(consumer_subscription)] group_name = ShareName.of('test') direct_receive_service = messaging_service.create_direct_message_receiver_builder( ) direct_receive_service = direct_receive_service.with_subscriptions( topics).build(shared_subscription_group=group_name) direct_receive_service.start() direct_receive_service.receive_async(MessageHandlerImpl2()) print(f"Subscribed to: {consumer_subscription}") time.sleep(MAX_SLEEP) finally: direct_receive_service.terminate() messaging_service.disconnect()
def consume_direct_detailed_message(service: MessagingService, consumer_subscription: str): try: topics = [TopicSubscription.of(consumer_subscription)] receiver: DirectMessageReceiver = service.create_direct_message_receiver_builder()\ .with_subscriptions(topics).build() receiver.start() with ThreadPoolExecutor(max_workers=1) as e: e.submit(HowToDirectPublishMessage. direct_message_publish_outbound_with_all_props, messaging_service=service, destination=Topic.of(consumer_subscription), message=constants.MESSAGE_TO_SEND) message_payload: 'InboundMessage' = receiver.receive_message() expiration = message_payload.get_expiration() print( f"received message payload is :{message_payload.get_payload_as_string()} " f"\n expiration is : {expiration}") finally: receiver.terminate()
def direct_message_consume(messaging_service: MessagingService, topic_subscription: str): try: trace.set_tracer_provider(TracerProvider()) jaeger_exporter = jaeger.JaegerSpanExporter( service_name="<Solace> REST Messaging call to Security Co", agent_host_name="localhost", agent_port=6831, ) trace.get_tracer_provider().add_span_processor( BatchExportSpanProcessor(jaeger_exporter)) tracer = trace.get_tracer(__name__) topics = [TopicSubscription.of(topic_subscription)] # Create a direct message consumer service with the topic subscription and start it direct_receive_service = messaging_service.create_direct_message_receiver_builder( ) direct_receive_service = direct_receive_service.with_subscriptions( topics).build() direct_receive_service.start() # Register a callback message handler direct_receive_service.receive_async(MessageHandlerImpl()) print(f"Subscribed to: {topic_subscription}") # Infinite loop until Keyboard interrupt is received try: while True: time.sleep(1) except KeyboardInterrupt: print('\nDisconnecting Messaging Service') finally: messaging_service.disconnect() direct_receive_service.terminate()