/// <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="handler">Method the perform when receiving KubeMQ.SDK.csharp.PubSub.EventReceive .</param> /// <param name="errorDelegate">Method the perform when receiving error from KubeMQ.SDK.csharp.PubSub.EventReceive .</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 SubscribeToEvents(SubscribeRequest subscribeRequest, HandleEventDelegate handler, HandleEventErrorDelegate errorDelegate, CancellationToken cancellationToken = default(CancellationToken)) { ValidateSubscribeRequest(subscribeRequest); // throws ArgumentException try { this.Ping(); } catch (Exception pingEx) { logger.LogWarning(pingEx, "An exception occurred while sending ping to kubemq"); throw pingEx; } var grpcListnerTask = Task.Run((Func <Task>)(async() => { while (true) { try { await SubscribeToEvents(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 listening for events"); errorDelegate(ex); } await Task.Delay(1000); } })); // send events to end-user Task evenSenderTask = Task.Run((Func <Task>)(async() => { while (true) { // await for event from queue InnerRecivedEvent innerEvent = await _RecivedEvents.ReceiveAsync(); LogIncomingEvent(innerEvent); // Convert KubeMQ.Grpc.Event to outer Event EventReceive evnt = new EventReceive(innerEvent); try { // Activate end-user event handler Delegate handler(evnt); } catch (Exception ex) { logger.LogError(ex, "An exception occurred while handling the event"); errorDelegate(ex); } } })); }
/// <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="handler">Method the perform when receiving KubeMQ.SDK.csharp.PubSub.EventReceive .</param> /// <returns>A task that represents the Subscribe Request.</returns> public void SubscribeToEvents(SubscribeRequest subscribeRequest, HandleEventDelegate handler) { ValidateSubscribeRequest(subscribeRequest);// throws ArgumentException var grpcListnerTask = Task.Run((Func <Task>)(async() => { while (true) { try { await SubscribeToEvents(subscribeRequest); } catch (Exception ex) { logger.LogWarning(ex, $"An exception occurred while listening for events"); } await Task.Delay(1000); } })); // send events to end-user Task evenSenderTask = Task.Run((Func <Task>)(async() => { while (true) { // await for event from queue InnerRecivedEvent innerEvent = await _RecivedEvents.ReceiveAsync(); LogIncomingEvent(innerEvent); // Convert KubeMQ.Grpc.Event to outer Event EventReceive evnt = new EventReceive(innerEvent); try { // Activate end-user event handler Delegate handler(evnt); } catch (Exception ex) { logger.LogError(ex, "An exception occurred while handling the event"); } } })); }