def direct_message_publish_outbound_with_all_props_on_backpressure_wait( messaging_service: MessagingService, destination, message, buffer_capacity, message_count): """ to publish outbound message using back pressure""" try: direct_publish_service = messaging_service.create_direct_message_publisher_builder() \ .on_back_pressure_wait(buffer_capacity=buffer_capacity) \ .build() direct_publish_service.start() outbound_msg = messaging_service.message_builder() \ .with_property("custom_key", "custom_value") \ .with_expiration(SolaceConstants.MESSAGE_EXPIRATION) \ .with_priority(SolaceConstants.MESSAGE_PRIORITY) \ .with_sequence_number(SolaceConstants.MESSAGE_SEQUENCE_NUMBER) \ .with_application_message_id(constants.APPLICATION_MESSAGE_ID) \ .with_application_message_type("app_msg_type") \ .with_http_content_header("text/html", "utf-8") \ .build(message) for e in range(message_count): direct_publish_service.publish(destination=destination, message=outbound_msg) except PublisherOverflowError: PublisherOverflowError("Queue maximum limit is reached") finally: util.publisher_terminate(direct_publish_service)
def direct_message_publish_outbound_properties( messaging_service: MessagingService, destination, message): """ to publish outbound message with additional properties""" try: direct_publish_service = messaging_service.create_direct_message_publisher_builder( ).build() direct_publish_service.start_async() outbound_msg = messaging_service.message_builder() \ .with_application_message_id(constants.APPLICATION_MESSAGE_ID) \ .from_properties(constants.CUSTOM_PROPS).build(message) direct_publish_service.publish(destination=destination, message=outbound_msg) finally: direct_publish_service.terminate(0)
def direct_message_publish_outbound_business_obj(messaging_service: MessagingService, destination, message_obj, converter): """ to publish outbound message from a custom object supplied with its own converter""" try: direct_publish_service = messaging_service.create_direct_message_publisher_builder().\ on_back_pressure_reject(buffer_capacity=0).build() publish_start = direct_publish_service.start_async() publish_start.result() outbound_msg = messaging_service.message_builder() \ .with_application_message_id(constants.APPLICATION_MESSAGE_ID) \ .build(message_obj, converter=converter) direct_publish_service.publish(destination=destination, message=outbound_msg) finally: direct_publish_service.terminate()
def receive_request_and_send_response_message(service: MessagingService, for_requests: TopicSubscription): """Mimics microservice that performs a response Args: service: connected messaging service for_requests: where to expect requests """ request_receiver: RequestReplyMessageReceiver = service.request_reply() \ .create_request_reply_message_receiver_builder().build(for_requests).start() msg, replier = request_receiver.receive_message(timeout=5000) if replier is not None: outbound_msg = service.message_builder().build("pong reply") replier.reply(outbound_msg)
def direct_message_publish_outbound_with_all_props( messaging_service: MessagingService, destination, message): """ to publish outbound message""" try: direct_publish_service = messaging_service.create_direct_message_publisher_builder( ).build() direct_publish_service.start_async() outbound_msg = messaging_service.message_builder() \ .with_property("custom_key", "custom_value") \ .with_expiration(SolaceConstants.DEFAULT_TIMEOUT_MS) \ .with_priority(1) \ .with_sequence_number(12345) \ .with_application_message_id(constants.APPLICATION_MESSAGE_ID) \ .with_application_message_type("app_msg_type") \ .with_http_content_header("text/html", _sol_constants.ENCODING_TYPE) \ .build(message) direct_publish_service.publish(destination=destination, message=outbound_msg) finally: direct_publish_service.terminate(0)
def notify_on_direct_publisher_failures(messaging_service: MessagingService, destination_name, message, buffer_capacity, message_count): """ configure direct message publisher to receive notifications about publish failures if any occurred """ try: direct_publish_service = messaging_service.create_direct_message_publisher_builder() \ .on_back_pressure_reject(buffer_capacity=buffer_capacity) \ .build() direct_publish_service.start() publish_failure_listener = PublishFailureListenerImpl() direct_publish_service.set_publish_failure_listener(publish_failure_listener) outbound_msg = messaging_service.message_builder() \ .with_application_message_id(constants.APPLICATION_MESSAGE_ID) \ .build(message) direct_publish_service.publish(destination=destination_name, message=outbound_msg) finally: direct_publish_service.terminate_async()
def publish_request_and_process_response_message_async(service: MessagingService, request_destination: Topic, reply_timeout: int): """Mimics microservice that performs a async request Args: service: connected messaging service request_destination: where to send a request (it is same for requests and responses) reply_timeout: the reply timeout """ requester: RequestReplyMessagePublisher = service.request_reply() \ .create_request_reply_message_publisher_builder().build().start() ping_message = service.message_builder().build(payload='Ping', additional_message_properties={SEQUENCE_NUMBER: 123}) publish_request_async = requester.publish(request_message=ping_message, request_destination=request_destination, reply_timeout=reply_timeout) # we can get the reply from the future print(publish_request_async.result())
def publish_request_and_process_response_message_blocking(service: MessagingService, request_destination: Topic, reply_timeout: int): """Mimics microservice that performs a blocking request Args: service: connected messaging service request_destination: where to send a request (it is same for requests and responses) reply_timeout: the reply timeout """ requester: RequestReplyMessagePublisher = service.request_reply() \ .create_request_reply_message_publisher_builder().build().start() ping_message: OutboundMessage = service.message_builder().build(payload='Ping') try: reply = requester.publish_await_response(request_message=ping_message, request_destination=request_destination, reply_timeout=reply_timeout) print(f"reply: {reply}") except TimeoutError as e: print(e)
def request_reply_message_consume2(messaging_service: MessagingService, consumer_subscription: str, reply_timeout: int): """This method will create an receiver instance to receive str or byte array type message""" try: global MAX_SLEEP topic_subscription = TopicSubscription.of(consumer_subscription) group_name = ShareName.of('test') receiver = messaging_service.request_reply(). \ create_request_reply_message_receiver_builder().build(request_topic_subscription=topic_subscription, share_name=group_name) receiver.start() msg, replier = receiver.receive_message(timeout=reply_timeout) print(f"incoming message is {msg.get_payload_as_string()}") if replier is not None: outbound_msg = messaging_service.message_builder().build("pong reply") replier.reply(outbound_msg) print(f"Subscribed to: {consumer_subscription}") time.sleep(MAX_SLEEP) finally: receiver.terminate()
def direct_message_publish_outbound_with_all_props_on_backpressure_elastic( messaging_service: MessagingService, destination, message, message_count): """ to publish outbound message using back pressure""" try: direct_publish_service = messaging_service.create_direct_message_publisher_builder() \ .on_back_pressure_elastic() \ .build() direct_publish_service.start() outbound_msg = messaging_service.message_builder() \ .with_property("custom_key", "custom_value") \ .with_expiration(SolaceConstants.MESSAGE_EXPIRATION) \ .with_priority(SolaceConstants.MESSAGE_PRIORITY) \ .with_sequence_number(SolaceConstants.MESSAGE_SEQUENCE_NUMBER) \ .with_application_message_id(constants.APPLICATION_MESSAGE_ID) \ .with_application_message_type("app_msg_type") \ .with_http_content_header("text/html", "utf-8") \ .build(message) for e in range(message_count): direct_publish_service.publish(destination=destination, message=outbound_msg) finally: direct_publish_service.terminate()