示例#1
0
        /// <summary>
        /// Publish a single request using the KubeMQ , response will return in the passed handler.
        /// </summary>
        /// <param name="handler">Method that will be activated once receiving response.</param>
        /// <param name="request">The KubeMQ.SDK.csharp.RequestReply.LowLevel.request that will be sent to the kubeMQ.</param>
        /// <returns>A task that represents the request that was sent using the SendRequest.</returns>
        public async Task SendRequest(Request request, HandleResponseDelegate handler)
        {
            try
            {
                //LogRequest(request);

                InnerRequest innerRequest = request.Convert();

                // Send request and wait for response
                InnerResponse innerResponse = await GetKubeMQClient().SendRequestAsync(innerRequest, Metadata);

                // convert InnerResponse to Response and return response to end user
                Response response = new Response(innerResponse);

                // send the response to the end-user response handler
                handler(response);
            }
            catch (RpcException ex)
            {
                logger.LogError($"Grpc Exception in SendRequest. Status: {ex.Status}");

                throw new RpcException(ex.Status);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Exception in Initiator.SendRequest");

                throw ex;
            }
        }
示例#2
0
        /// <summary>
        /// Publish a single request using the KubeMQ.
        /// </summary>
        /// <param name="request"></param>
        /// <returns>KubeMQ KubeMQ.SDK.csharp.RequestReply.Response.</returns>
        public Response SendRequest(Request request)
        {
            try
            {
                //LogRequest(request);

                InnerRequest innerRequest = request.Convert();

                // Send request and wait for response
                InnerResponse innerResponse = GetKubeMQClient().SendRequest(innerRequest, Metadata);

                // convert InnerResponse to Response and return response to end user
                return(new Response(innerResponse));
            }
            catch (RpcException ex)
            {
                logger.LogError($"Grpc Exception in SendRequestAsync. Status: {ex.Status}");

                throw new RpcException(ex.Status);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Exception in Initiator.SendRequestAsync");
                return(null);
            }
        }
示例#3
0
        /// <summary>
        /// Register to kubeMQ Channel using KubeMQ.SDK.csharp.Subscription.SubscribeRequest with RespondDelegate .
        /// </summary>
        /// <param name="subscribeRequest">Parameters list represent by KubeMQ.SDK.csharp.Subscription.SubscribeRequest that will determine the subscription configuration.</param>
        /// <param name="handler">Method the perform when receiving KubeMQ.SDK.csharp.RequestReplay.RequestReceive </param>
        /// <returns>A task that represents the Subscribe Request.</returns>
        public void SubscribeToRequests(SubscribeRequest subscribeRequest, RespondDelegate handler)
        {
            ValidateSubscribeRequest(subscribeRequest);// throws ArgumentException

            Task grpcListnerTask = Task.Run((Func <Task>)(async() =>
            {
                while (true)
                {
                    try
                    {
                        await SubscribeToRequests(subscribeRequest);
                    }
                    catch (Exception ex)
                    {
                        logger.LogWarning(ex, $"An exception occurred while listening for request");
                    }
                    await Task.Delay(1000);
                }
            }));


            // send requests to end-user and send his response via GRPC
            Task handelAndRespondTask = Task.Run((Func <Task>)(async() =>
            {
                while (true)
                {
                    // await for Request from queue
                    InnerRequest innerRequest = await _RecivedRequests.ReceiveAsync();

                    // Convert KubeMQ.Grpc.Request to RequestReceive
                    RequestReceive request = new RequestReceive(innerRequest);

                    try
                    {
                        // Activate end-user request handler and receive the response
                        Response response = handler(request);

                        // Convert
                        InnerResponse innerResponse = response.Convert();

                        LogResponse(innerResponse);

                        // Send Response via GRPC
                        GetKubeMQClient().SendResponse(innerResponse, _metadata);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, "An exception occurred while handling the response");
                    }
                }
            }));
        }
示例#4
0
        /// <summary>
        /// Register to kubeMQ Channel using KubeMQ.SDK.csharp.Subscription.SubscribeRequest.
        /// </summary>
        /// <param name="subscribeRequest">Parameters list represent by KubeMQ.SDK.csharp.Subscription.SubscribeRequest that will determine the subscription configuration.</param>
        /// <param name="cancellationToken">Optional param if needed to cancel the subscriber ,will receive RPC exception with status canceled through the error Delegate is called.</param>
        /// <returns>A task that represents the Subscribe Request.</returns>
        private async Task SubscribeToRequests(SubscribeRequest subscribeRequest, CancellationToken cancellationToken)
        {
            KubeMQGrpc.Subscribe innerSubscribeRequest = subscribeRequest.ToInnerSubscribeRequest();

            using (var call = GetKubeMQClient().SubscribeToRequests(innerSubscribeRequest, Metadata, null, cancellationToken)) {
                // await for requests form GRPC stream.
                while (await call.ResponseStream.MoveNext())
                {
                    // Received requests form GRPC stream.
                    InnerRequest request = call.ResponseStream.Current;

                    LogRequest(request);

                    // Add (Post) request to queue
                    _RecivedRequests.Post(request);
                }
            }
        }
示例#5
0
 private void LogRequest(InnerRequest request)
 {
     logger.LogTrace($"Responder InnerRequest. ID:'{request.RequestID}', Channel:'{request.Channel}', ReplyChannel:'{request.ReplyChannel}'");
 }
示例#6
0
        /// <summary>
        /// Register to kubeMQ Channel using KubeMQ.SDK.csharp.Subscription.SubscribeRequest with RespondDelegate .
        /// </summary>
        /// <param name="subscribeRequest">Parameters list represent by KubeMQ.SDK.csharp.Subscription.SubscribeRequest that will determine the subscription configuration.</param>
        /// <param name="handler">Method the perform when receiving KubeMQ.SDK.csharp.RequestReplay.RequestReceive </param>
        /// <param name="errorDelegate">Method the perform when receiving error from KubeMQ.SDK.csharp.CommandQuery .</param>
        /// <param name="cancellationToken">Optional param if needed to cancel the subscriber ,will receive RPC exception with status canceled through the error Delegate is called.</param>
        /// <returns>A task that represents the Subscribe Request. Possible Exception: fail on ping to kubemq.</returns>
        public void SubscribeToRequests(SubscribeRequest subscribeRequest, RespondDelegate handler, HandleCommandQueryErrorDelegate errorDelegate, CancellationToken cancellationToken = default(CancellationToken))
        {
            ValidateSubscribeRequest(subscribeRequest);// throws ArgumentException

            try
            {
                this.Ping();
            }
            catch (Exception pingEx)
            {
                logger.LogWarning(pingEx, "n exception occurred while sending ping to kubemq");
                throw pingEx;
            }

            Task grpcListnerTask = Task.Run((Func <Task>)(async() =>
            {
                while (true)
                {
                    try
                    {
                        await SubscribeToRequests(subscribeRequest, cancellationToken);
                    }
                    catch (RpcException rpcx)
                    {
                        if (rpcx.StatusCode == StatusCode.Cancelled)
                        {
                            logger.LogWarning(rpcx, $"Cancellation was called ");

                            errorDelegate(rpcx);
                            break;
                        }
                        else
                        {
                            logger.LogWarning(rpcx, $"An RPC exception occurred while listening for events");

                            errorDelegate(rpcx);
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.LogWarning(ex, $"An exception occurred while receiving request");
                        errorDelegate(ex);
                    }
                    await Task.Delay(1000);
                }
            }));


            // send requests to end-user and send his response via GRPC
            Task handelAndRespondTask = Task.Run((Func <Task>)(async() =>
            {
                while (true)
                {
                    // await for Request from queue
                    InnerRequest innerRequest = await _RecivedRequests.ReceiveAsync();

                    // Convert KubeMQ.Grpc.Request to RequestReceive
                    RequestReceive request = new RequestReceive(innerRequest);

                    try
                    {
                        // Activate end-user request handler and receive the response
                        Response response = handler(request);

                        // Convert
                        InnerResponse innerResponse = response.Convert();

                        LogResponse(innerResponse);

                        // Send Response via GRPC
                        GetKubeMQClient().SendResponse(innerResponse, _metadata);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, "An exception occurred while handling the response");
                        errorDelegate(ex);
                    }
                }
            }));
        }