/// <summary> /// implement OnTopicEvent to handle integration events /// </summary> /// <param name="request"></param> /// <param name="context"></param> /// <returns></returns> public override async Task <TopicEventResponse> OnTopicEvent(TopicEventRequest request, ServerCallContext context) { var result = new TopicEventResponse(); result.Status = TopicEventResponse.Types.TopicEventResponseStatus.Success; var data = request.Data.ToStringUtf8(); var input = JsonConvert.DeserializeObject <IntegrationEvent>(data); var eventType = GetEventByName(input.EventType, _logger); if (eventType != null) { try { var @event = JsonConvert.DeserializeObject(data, eventType); await _mediator.Publish(@event); _logger.LogInformation("Dispached integration event {eventName} to found implementation {eventType}", input.EventType, eventType); } catch (Exception ex) { _logger.LogError("Error while dispatching integration event {eventName}: {error}", input.EventType, ex); result.Status = TopicEventResponse.Types.TopicEventResponseStatus.Retry; } } else { result.Status = TopicEventResponse.Types.TopicEventResponseStatus.Drop; } return(await Task.FromResult(result)); }
public override async Task <TopicEventResponse> OnTopicEvent(TopicEventRequest request, ServerCallContext context) { _logger.LogDebug("OnTopicEvent"); if (request.PubsubName == "pubsub") { var incrementRequest = JsonSerializer.Deserialize <IncrementRequest>(request.Data.ToStringUtf8(), this.jsonOptions); if (request.Topic == "points") { _logger.LogDebug("Points received: {0}", incrementRequest.Points); } } var response = new TopicEventResponse(); return(await Task.FromResult(response)); }
public override async Task <TopicEventResponse> OnTopicEvent(TopicEventRequest request, ServerCallContext context) { m_Logger.LogInformation($" ** ServiceB: Topic dispatcher entered. Topic={request.Topic}, PubsubName={request.PubsubName}"); switch (request.Topic) { case NamesOfQueuesNPubSubs.ServiceADemoEvent1Topic: try { string payloadString = request.Data.ToStringUtf8(); m_Logger.LogInformation($" ** ServiceB: {request.Topic} subscr. Dispatching event containing: {payloadString}."); // For display in a running demo. Console.WriteLine($" ** ServiceB: {request.Topic} subscr. Dispatching event containing: {payloadString}"); // Below, ServiceB processes the event locally here in ServiceB. bool isSuccess = await ProcessServiceADemoEvent1(payloadString); // ANOTHER OPTION -- ServiceB could also use Dapr SERVICE INVOCATION to call some other // service within this Dapr-Mesh to process this event, rather than process the event in // THIS ServiceB. This is sketched below but NOT IMPLEMENTED so as to keep the code minimal. //HelloRequest helloRequest = new HelloRequest(); //await m_DaprProxy.InvokeMethodGrpcAsync<HelloRequest>("SomeService", "SomeServiceOp", helloRequest); } catch (Exception ex) { m_Logger.LogError($" ** ServiceB: Exception when dispatching event for Topic={request.Topic}. ex = {ex}"); throw; } break; // TODO If required, add subscription for NamesOfQueuesNPubSubs.ServiceADemoEvents2Topic and // process the events below. case NamesOfQueuesNPubSubs.ServiceADemoEvent2Topic: try { string payloadString = request.Data.ToStringUtf8(); m_Logger.LogInformation($" ** ServiceB: {request.Topic} subscr. Dispatching event containing: {payloadString}."); // For display in a running demo. Console.WriteLine($" ** ServiceB: {request.Topic} subscr. Dispatching event containing: {payloadString}"); } catch (Exception ex) { m_Logger.LogError($" ** ServiceB: Exception when dispatching event for Topic={request.Topic}. ex = {ex}"); throw; } break; default: m_Logger.LogError($" ** ServiceB: ERROR. Received event for unsupported Topic={request.Topic}."); break; } // TODO Determine the circumstances required to return Retry or Drop. TopicEventResponse topicResponse = new TopicEventResponse(); // The other enum members are Retry, Drop. topicResponse.Status = TopicEventResponse.Types.TopicEventResponseStatus.Success; return(await Task.FromResult(topicResponse)); // More Info -- From Dapr SDK appcallback.proto /* * // TopicEventResponse is response from app on published message * message TopicEventResponse { * // TopicEventResponseStatus allows apps to have finer control over handling of the message. * enum TopicEventResponseStatus { * // SUCCESS is the default behavior: message is acknowledged and not retried or logged. * SUCCESS = 0; * // RETRY status signals Dapr to retry the message as part of an expected scenario (no warning is logged). * RETRY = 1; * // DROP status signals Dapr to drop the message as part of an unexpected scenario (warning is logged). * DROP = 2; * // } */ //return base.OnTopicEvent(request, context); }