public override Task Publish(IEventData @event) { var message = JsonConvert.SerializeObject(@event); var body = Encoding.UTF8.GetBytes(message); MessageProperties messageProperties = new MessageProperties(); IExchange exchange = _ibus.Advanced.ExchangeDeclare(BROKER_NAME, "topic"); _logger.LogDebug($"exchange:{exchange.Name},routekey:{@event.GetType().Name},message:{message}"); return(_ibus.Advanced.PublishAsync(exchange, @event.GetType().Name, true, messageProperties, body)); }
/// <summary> /// 重写以实现异步触发事件的执行 /// </summary> /// <param name="factory">事件处理器工厂</param> /// <param name="eventType">事件类型</param> /// <param name="eventData">事件数据</param> /// <param name="wait">是否等待结果返回</param> /// <returns></returns> protected virtual async Task InvokeHandlerAsync(IEventHandlerFactory factory, Type eventType, IEventData eventData, bool wait = true) { EventHandlerDisposeWrapper handlerWrapper = factory.GetHandler(); IEventHandler handler = handlerWrapper.EventHandler; try { if (handler == null) { Logger.LogWarning($"事件源“{eventData.GetType()}”的事件处理器无法找到"); return; } if (wait) { await RunAsync(factory, handler, eventType, eventData); return; } Task.Run(async() => { await RunAsync(factory, handler, eventType, eventData); }); } finally { handlerWrapper.Dispose(); } }
/// <summary> /// 重写以实现触发事件的执行 /// </summary> /// <param name="factory">事件处理器工厂</param> /// <param name="eventType">事件类型</param> /// <param name="eventData">事件数据</param> /// <param name="wait">是否等待结果返回</param> protected void InvokeHandler(IEventHandlerFactory factory, Type eventType, IEventData eventData, bool wait = true) { IEventHandler handler = factory.GetHandler(); if (handler == null) { _Logger.LogWarning($"事件源“{eventData.GetType()}”的事件处理器无法找到"); return; } if (!handler.CanHandle(eventData)) { return; } if (wait) { Run(factory, handler, eventType, eventData); } else { Task.Run(() => { Run(factory, handler, eventType, eventData); }); } }
/// <summary> /// 重写以实现触发事件的执行 /// </summary> /// <param name="factory">事件处理器工厂</param> /// <param name="eventType">事件类型</param> /// <param name="eventData">事件数据</param> /// <param name="wait">是否等待结果返回</param> protected void InvokeHandler(IEventHandlerFactory factory, Type eventType, IEventData eventData, bool wait = true) { EventHandlerDisposeWrapper handlerWrapper = factory.GetHandler(); IEventHandler handler = handlerWrapper.EventHandler; try { if (handler == null) { Logger.LogWarning($"事件源“{eventData.GetType()}”的事件处理器无法找到"); return; } if (!handler.CanHandle(eventData)) { return; } if (wait) { Run(factory, handler, eventType, eventData); } else { Task.Run(() => { Run(factory, handler, eventType, eventData); }); } } finally { handlerWrapper.Dispose(); } }
/// <summary> /// 重写以实现触发事件的执行,默认使用同步执行 /// </summary> /// <param name="factory">事件处理器工厂</param> /// <param name="eventType">事件类型</param> /// <param name="eventData">事件数据</param> protected void InvokeHandler(IEventHandlerFactory factory, Type eventType, IEventData eventData) { IEventHandler handler = factory.GetHandler(); if (handler == null) { throw new OsharpException($"事件源“{eventData.GetType()}”的事件处理器无法找到"); } if (!handler.CanHandle(eventData)) { return; } try { handler.Handle(eventData); } catch (Exception ex) { string msg = $"执行事件“{eventType.Name}”的处理器“{handler.GetType()}”时引发异常:{ex.Message}"; _Logger.LogError(ex, msg); } finally { factory.ReleaseHandler(handler); } }
public static Event Make(IEventData eventData, Guid aggregateId, int eventSequence) { return new Event { AggregateId = aggregateId, AggregateEventSequence = eventSequence, DateTime = System.DateTime.UtcNow, EventType = eventData.GetType().FullName, Data = JsonConvert.SerializeObject(eventData) }; }
public static Event Make(IEventData eventData, Guid aggregateId, int eventSequence) { return(new Event { AggregateId = aggregateId, AggregateEventSequence = eventSequence, DateTime = System.DateTime.UtcNow, EventType = eventData.GetType().FullName, Data = JsonConvert.SerializeObject(eventData) }); }
/// <summary> /// 有返回值的必然 1 对 1 /// </summary> /// <typeparam name="TResponse"></typeparam> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public Task <TResponse> Send <TResponse>(IEventData <TResponse> request, CancellationToken cancellationToken = default) { if (request == null) { throw new ArgumentNullException(nameof(request)); } var requestType = request.GetType(); //获取映射的EventHandler var handlerTypes = _InMemoryEventStore.GetHandlersForEvent(requestType); //var objInstance = // Activator.CreateInstance( // typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(requestType, typeof(TResponse))); //var handler = (RequestHandlerWrapper<TResponse>)objInstance; return(Task.FromResult(default(TResponse))); }
public async Task HandleAsync(IEventData @event, CancellationToken cancellationToken = default) { var eventName = @event.GetType().Name; if (HasSubscriptionsForEvent(eventName)) { using (var serviceScope = _iServiceProvider.GetService <IServiceScopeFactory>().CreateScope()) { var subscriptions = GetHandlersForEvent(eventName); foreach (var subscription in subscriptions) { var handler = (IEventHandler)serviceScope.ServiceProvider.GetService(subscription); if (handler.CanHandle(@event)) { await handler.HandleAsync(@event, cancellationToken); } } } } }
/// <summary> /// 重写以实现触发事件的执行 /// </summary> /// <param name="factory">事件处理器工厂</param> /// <param name="eventType">事件类型</param> /// <param name="eventData">事件数据</param> protected void InvokeHandler(IEventHandlerFactory factory, Type eventType, IEventData eventData) { EventHandlerDisposeWrapper handlerWrapper = factory.GetHandler(); IEventHandler handler = handlerWrapper.EventHandler; try { if (handler == null) { Logger.LogWarning($"事件源“{eventData.GetType()}”的事件处理器无法找到"); return; } Run(handler, eventType, eventData); } finally { handlerWrapper.Dispose(); } }
/// <summary> /// 重写以实现异步触发事件的执行 /// </summary> /// <param name="factory">事件处理器工厂</param> /// <param name="eventType">事件类型</param> /// <param name="eventData">事件数据</param> /// <param name="wait">是否等待结果返回</param> /// <returns></returns> protected virtual Task InvokeHandlerAsync(IEventHandlerFactory factory, Type eventType, IEventData eventData, bool wait = true) { IEventHandler handler = factory.GetHandler(); if (handler == null) { Logger.LogWarning($"事件源“{eventData.GetType()}”的事件处理器无法找到"); return(Task.FromResult(0)); } if (!handler.CanHandle(eventData)) { return(Task.FromResult(0)); } if (wait) { return(RunAsync(factory, handler, eventType, eventData)); } Task.Run(async() => { await RunAsync(factory, handler, eventType, eventData); }); return(Task.FromResult(0)); }
/// <summary> /// 是否可处理指定事件 /// </summary> /// <param name="eventData">事件源数据</param> /// <returns>是否可处理</returns> public virtual bool CanHandle(IEventData eventData) { return(eventData.GetType() == typeof(TEventData)); }
private static void HandleEventGroupDisbanded(IRpcProtocol protocol, IEventData eventData) { var groupDisbanded = new GroupDisbanded(protocol, eventData); if (!groupDisbanded.IsValid) { _logger.ErrorFormat("[HandleEventGroupDisbanded]: Received an invalid {0}", eventData.GetType()); return; } MmoWorld.Instance.PrimaryFiber.Enqueue(() => DoGroupDisbanded(groupDisbanded)); }
private Event MakeEvent(IEventData eventDataObject) { return new Event {AggregateId = DinnerGuid, AggregateEventSequence = _currentEvent, DateTime = DateTime.UtcNow, EventType = eventDataObject.GetType().FullName, Data = JsonConvert.SerializeObject(eventDataObject)}; }
public bool CanHandle(IEventData @event) => typeof(TEventData).Equals(@event.GetType());