/// <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"); } } })); }
public Response(RequestReceive request) { RequestID = request.RequestID; ReplyChannel = request.ReplyChannel; Timestamp = DateTime.UtcNow; }
/// <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); } } })); }