public static MicroserviceFrameworkBuilder RegisterEventHandlers(this MicroserviceFrameworkBuilder builder) { MicroserviceFrameworkLoaderContext.Get(builder.Services).ResolveType += type => { var interfaces = type.GetInterfaces(); var handlerInterfaceTypes = interfaces .Where(@interface => @interface.IsGenericType && EventHandlerBaseType == @interface.GetGenericTypeDefinition()) .ToList(); if (handlerInterfaceTypes.Count == 0) { return; } foreach (var handlerInterfaceType in handlerInterfaceTypes) { var eventType = handlerInterfaceType.GenericTypeArguments[0]; if (!eventType.IsEvent()) { throw new MicroserviceFrameworkException($"{eventType} 不是合法的事件类型"); } // 消息队列,得知道系统实现了哪些 EventHandler 才去监听对应的 Topic,所以必须先注册监听。 EventSubscriptionManager.Register(eventType, handlerInterfaceType); // 每次收到的消息都是独立的 Scope ServiceCollectionUtilities.TryAdd(builder.Services, new ServiceDescriptor(handlerInterfaceType, type, ServiceLifetime.Scoped)); } }; return(builder); }
public void Publish_ExecutesOnlyHandlesWhoseConditionIsTrue() { var agg = CreateAggregator(); var handlerConditionTrue = new TestHandler(); var handlerConditionFalse = new TestHandler(); TestPayload actualPayload = null; var sm = new EventSubscriptionManager(agg, b => { b.On(TestEvents.FirstEvent).When(p => false).Execute(handlerConditionFalse.HandleEvent); b.On(TestEvents.FirstEvent) .When(p => { actualPayload = p; return(true); }) .Execute(handlerConditionTrue.HandleEvent); }); TestPayload expectedPayload = new TestPayload(); agg.Publish(TestEvents.FirstEvent, expectedPayload); Assert.AreEqual(0, handlerConditionFalse.Invocations); Assert.AreEqual(1, handlerConditionTrue.Invocations); Assert.AreEqual(expectedPayload, actualPayload); GC.KeepAlive(sm); }
public MicrophoneVolumeControlViewModel( SystemConfig config, IEventAggregator eventAggregator, EventSubscriptionManager eventSubscriptionManager, MMDeviceEnumerator mMDeviceEnumerator) { _config = config ?? throw new ArgumentNullException("config"); _eventAggregator = eventAggregator ?? throw new ArgumentNullException("_eventAggregator"); _eventSubscriptionManager = eventSubscriptionManager ?? throw new ArgumentNullException("eventSubscriptionManager"); _mMDeviceEnumerator = mMDeviceEnumerator ?? throw new ArgumentNullException("mMDeviceEnumerator"); MicrophoneMuteCommand = new DelegateCommand(() => { _microphoneVolume = MicrophoneVolume; MicrophoneVolume = 0; }); PlayMicrophoneSoundCommand = new DelegateCommand(() => { MicrophoneVolume = _microphoneVolume = _microphoneVolume < 5 ? 5 : _microphoneVolume; }); microphpneVolumeChangeEventSubscriptionToken = eventSubscriptionManager.Subscribe <MicrophoneVolumeChangeEvent, int>(null, MicrophoneVolumeChangeEventHandler, null); MicrophoneVolume = _config.MicrophoneVolume; }
public virtual Void execute(CommandContext commandContext) { ensureNotNull("executionId", executionId); EventSubscriptionManager eventSubscriptionManager = commandContext.EventSubscriptionManager; IList <EventSubscriptionEntity> eventSubscriptions = null; if (!string.ReferenceEquals(messageName, null)) { eventSubscriptions = eventSubscriptionManager.findEventSubscriptionsByNameAndExecution(EventType.MESSAGE.name(), messageName, executionId, exclusive); } else { eventSubscriptions = eventSubscriptionManager.findEventSubscriptionsByExecutionAndType(executionId, EventType.MESSAGE.name(), exclusive); } ensureNotEmpty("Execution with id '" + executionId + "' does not have a subscription to a message event with name '" + messageName + "'", "eventSubscriptions", eventSubscriptions); ensureNumberOfElements("More than one matching message subscription found for execution " + executionId, "eventSubscriptions", eventSubscriptions, 1); // there can be only one: EventSubscriptionEntity eventSubscriptionEntity = eventSubscriptions[0]; // check authorization string processInstanceId = eventSubscriptionEntity.ProcessInstanceId; foreach (CommandChecker checker in commandContext.ProcessEngineConfiguration.CommandCheckers) { checker.checkUpdateProcessInstanceById(processInstanceId); } eventSubscriptionEntity.eventReceived(processVariables, processVariablesLocal, null, false); return(null); }
public void Publish_OfAnEvent_ExecutesCorrectHandlers() { var agg = CreateAggregator(); var firstEventHandler = new TestHandler(); var secondEventHandler = new TestHandler(); var sm = new EventSubscriptionManager(agg); sm.Subscribe(b => { b.On(TestEvents.FirstEvent).Execute(firstEventHandler.HandleEvent); b.On(TestEvents.SecondEvent).Execute(secondEventHandler.HandleEvent); }); var expectedPayload = new TestPayload(); agg.Publish(TestEvents.FirstEvent, expectedPayload); Assert.AreEqual(1, firstEventHandler.Invocations); Assert.AreEqual(0, secondEventHandler.Invocations); Assert.AreEqual(expectedPayload, firstEventHandler.LastPaylaod); GC.KeepAlive(sm); }
private void UserControl_Loaded(object sender, RoutedEventArgs e) { var vm = this.DataContext as SpeechViewModel; if (vm != null) { PPTViewer.SetEventAggregator(vm.EventAggregator); _eventSubscriptionManager = vm.EventSubscriptionManager; _desktopWindowCollector = vm.DesktopWindowCollector; _setupVideoLiveAndRecordingDevices = vm.SetupVideoLiveAndRecordingDevices; _switchDemonstrationSceneEventSubscriptionToken = _eventSubscriptionManager.Subscribe <SwitchDemonstrationSceneEvent, SwitchDemonstrationSceneContext>(null, SwitchDemonstrationSceneEventHandler, null); _systemCloseEventSubscriptionToken = _eventSubscriptionManager.Subscribe <ShutDownEvent, bool>(null, SystemShutdownHandler, null); _pptClosedEventSubscriptionToken = _eventSubscriptionManager.Subscribe <PPTClosedEvent, bool>(null, PPTClosedEventHandler, null); _eventSubscriptionManager.Subscribe <SelectedDemonstrationWindowEvent, PreviewWindowInfo>(null, SwitchPreviewWindowSceneHandler, null); _eventSubscriptionManager.Subscribe <PlayVolumeChangeEvent, int>(null, (volume) => { _player.Volume = (double)volume / 100; }, null); } DefaultScene.Visibility = Visibility.Visible; _desktopWindowCollector.SetWindowHandle(DefaultScene.Handle); _setupVideoLiveAndRecordingDevices?.SetVideoDevice(_desktopWindowCollector); }
public async Task ExecuteAsync(string eventName, string eventData) { var subscriptions = EventSubscriptionManager.GetOrDefault(eventName); if (subscriptions == null) { _logger.LogWarning($"There is no event handler for {eventName}"); return; } foreach (var subscription in subscriptions) { // 每次执行是独立的 scope await using var scope = _serviceProvider.CreateAsyncScope(); var handlers = scope.ServiceProvider.GetServices(subscription.EventHandlerType) .Select(x => x as IDisposable); foreach (var handler in handlers) { if (handler == null) { continue; } var @event = Default.JsonHelper.Deserialize(eventData, subscription.EventType); if (subscription.MethodInfo.Invoke(handler, new[] { @event }) is Task task) { await task.ConfigureAwait(false); } handler.Dispose(); } } }
public void RemoveSubscriptionTo_OnlyHandlerOfSpecifiedEventIsNotExecutedAnymore() { var agg = CreateAggregator(); var firstEventHandler = new TestHandler(); var secondEventHandler1 = new TestHandler(); var secondEventHandler2 = new TestHandler(); var sm = new EventSubscriptionManager(agg, b => { b.On(TestEvents.FirstEvent).Execute(firstEventHandler.HandleEvent); b.On(TestEvents.SecondEvent).Execute(secondEventHandler1.HandleEvent); b.On(TestEvents.SecondEvent).Execute(secondEventHandler2.HandleEvent); }); sm.RemoveSubscriptionsTo(TestEvents.SecondEvent); agg.Publish(TestEvents.FirstEvent, new TestPayload()); agg.Publish(TestEvents.SecondEvent, new TestPayload()); Assert.AreEqual(1, firstEventHandler.Invocations); Assert.AreEqual(0, secondEventHandler1.Invocations); Assert.AreEqual(0, secondEventHandler2.Invocations); GC.KeepAlive(sm); }
protected internal virtual bool IsSameMessageEventSubscriptionAlreadyPresent(EventSubscriptionDeclaration eventSubscription, string tenantId) { // look for subscriptions for the same name in db: IList <EventSubscriptionEntity> subscriptionsForSameMessageName = EventSubscriptionManager.FindEventSubscriptionsByNameAndTenantId(EventType.Message.Name, eventSubscription.UnresolvedEventName, tenantId); //TODO 缓存差异 // also look for subscriptions created in the session: 源码有值 IList <EventSubscriptionEntity> cachedSubscriptions = DbEntityCache.GetEntitiesByType <EventSubscriptionEntity>(typeof(EventSubscriptionEntity)); foreach (EventSubscriptionEntity cachedSubscription in cachedSubscriptions) { //TODO HasTenantId subscriptions if (eventSubscription.UnresolvedEventName.Equals(cachedSubscription.EventName) && HasTenantId(cachedSubscription, tenantId) && !subscriptionsForSameMessageName.Contains(cachedSubscription)) { subscriptionsForSameMessageName.Add(cachedSubscription); } } // remove subscriptions deleted in the same command //subscriptionsForSameMessageName = DbEntityManager.PruneDeletedEntities(subscriptionsForSameMessageName); subscriptionsForSameMessageName = Context.CommandContext.PruneDeletedEntities <EventSubscriptionEntity>(subscriptionsForSameMessageName); // remove subscriptions for different type of event (i.e. remove intermediate message event subscriptions) subscriptionsForSameMessageName = FilterSubscriptionsOfDifferentType(eventSubscription, subscriptionsForSameMessageName); return(subscriptionsForSameMessageName.Count > 0); }
public TimeRecorderViewModel(EventSubscriptionManager eventSubscriptionManager) { _eventSubscriptionManager = eventSubscriptionManager; _timer = new Timer((state) => TimerHandler(), null, Timeout.Infinite, Timeout.Infinite); DurationText = "00:00:00"; _eventSubscriptionManager.Subscribe <LiveAndRecordingOperateEvent, LiveAndRecordingOperateEventContext>(null, LiveOrRecordingOperateEventHandler, EventFilter); }
public void SubscribeAllEventTypes() { foreach (var eventType in EventSubscriptionManager.GetEventTypes()) { var eventName = eventType.GetEventName(); Subscribe(eventName); } StartConsume(); }
protected override void Before_all_tests() { base.Before_all_tests(); EventSubscriptionManager.AddListenerAction <InitializationOfUnitTestHarnessClientEvent>(e => _initializationOfUnitTestHarnessClientEvent = e); EventSubscriptionManager.AddListenerAction <TestExecutionClassCompletedClientEvent>(e => _testExecutionClassCompletedClientEvent.Add(e)); EventSubscriptionManager.AddListenerAction <TestExecutionMethodIgnoredClientEvent>(e => _testExecutionMethodIgnoredClientEvent.Add(e)); EventSubscriptionManager.AddListenerAction <TestExecutionMethodFailedClientEvent>(e => _testExecutionMethodFailedClientEvent.Add(e)); EventSubscriptionManager.AddListenerAction <TestExecutionMethodPassedClientEvent>(e => _testExecutionMethodPassedClientEvent.Add(e)); }
public LiveStatusReporting( ILoggerFacade logger, IServiceClient serviceClient, WebPlatformApiFactory webPlatformApiFactory, EventSubscriptionManager eventSubscriptionManager) { _logger = logger; _serviceClient = serviceClient; _webPlatformApiFactory = webPlatformApiFactory; eventSubscriptionManager.Subscribe <LiveAndRecordingOperateEvent, LiveAndRecordingOperateEventContext>(null, Handler, EventFilter); }
public RuntimeState(EventSubscriptionManager eventSubscriptionManager) { HttpRequestHandleManager.Instance.AddHandler("GetLiveAndRecordState", new Func <IDictionary <string, string>, string>((dic) => { return(HttpRequestHandleResultWarpper.WriteResult(true, null, this)); })); eventSubscriptionManager.Subscribe <LiveAndRecordingOperateEvent, LiveAndRecordingOperateEventContext>(null, LiveAndRecordingOperateEventHandler, null); eventSubscriptionManager.Subscribe <LoadPresentationCompletedEvent, List <PresentationInfo> >(null, LoadPresentationCompletedEventHandler, null); eventSubscriptionManager.Subscribe <LoadWarmVideoCompletedEvent, List <PresentationInfo> >(null, LoadWarmVideoCompletedEventHandler, null); eventSubscriptionManager.Subscribe <LoadCameraDeviceCompletedEvent, List <IVideoDevice> >(null, LoadCameraDeviceCompletedEventHandler, null); eventSubscriptionManager.Subscribe <SwitchDemonstrationSceneEvent, SwitchDemonstrationSceneContext>(null, SwitchDemonstrationSceneEventHandler, null); }
private ScreenOpenedEventArgs ExpectScreenOpenedEvent(Action triggerAction) { ScreenOpenedEventArgs args = null; var sm = new EventSubscriptionManager(Aggregator, b => { b.On(ScreenConductor.ScreenOpenedEvent).Execute(a => args = a); }); triggerAction(); sm.RemoveAllSubscriptions(); return(args); }
protected internal virtual IList <EventSubscriptionEntity> findSignalEventSubscriptions(CommandContext commandContext, string signalName) { EventSubscriptionManager eventSubscriptionManager = commandContext.EventSubscriptionManager; if (builder.TenantIdSet) { return(eventSubscriptionManager.findSignalEventSubscriptionsByEventNameAndTenantId(signalName, builder.TenantId)); } else { return(eventSubscriptionManager.findSignalEventSubscriptionsByEventName(signalName)); } }
public PlayVolumeControlViewModel(SystemConfig config, IEventAggregator eventAggregator, EventSubscriptionManager eventSubscriptionManager) { _config = config ?? throw new ArgumentNullException("config"); _eventAggregator = eventAggregator ?? throw new ArgumentNullException("eventAggregator"); _eventSubscriptionManager = eventSubscriptionManager ?? throw new ArgumentNullException("eventSubscriptionManager"); SoundMuteCommand = new DelegateCommand(new Action(Mute)); PlaySoundCommand = new DelegateCommand(new Action(PlaySound)); playVolumeChangeEventSubscriptionToken = eventSubscriptionManager.Subscribe <PlayVolumeChangeEvent, int>(null, PlayVolumeChangeHandler, null); SoundVolume = _config.VideoVolume; }
protected internal virtual IList <EventSubscriptionEntity> findConditionalStartEventSubscriptions(CommandContext commandContext, ConditionSet conditionSet) { EventSubscriptionManager eventSubscriptionManager = commandContext.EventSubscriptionManager; if (conditionSet.isTenantIdSet) { return(eventSubscriptionManager.findConditionalStartEventSubscriptionByTenantId(conditionSet.TenantId)); } else { return(eventSubscriptionManager.findConditionalStartEventSubscription()); } }
protected override void Before_all_tests() { base.Before_all_tests(); PathToIntegrationTestXap = TestXapFileLocations.MSTestSL3; _clientTestRunConfiguration = new IntegrationTestClientTestRunConfiguration(); EventSubscriptionManager .AddListener <InitializationOfUnitTestHarnessClientEvent>(e => _initializationOfUnitTestHarnessClientEvent = e) .AddListener <TestExecutionClassCompletedClientEvent>(e => _testExecutionClassCompletedClientEvent.Add(e)) .AddListener <TestExecutionMethodIgnoredClientEvent>(e => _testExecutionMethodIgnoredClientEvent.Add(e)) .AddListener <TestExecutionMethodFailedClientEvent>(e => _testExecutionMethodFailedClientEvent.Add(e)) .AddListener <TestExecutionMethodPassedClientEvent>(e => _testExecutionMethodPassedClientEvent.Add(e)) ; }
protected internal virtual IList <EventSubscriptionEntity> findSignalEventSubscriptions(string signalName, string tenantId) { EventSubscriptionManager eventSubscriptionManager = Context.CommandContext.EventSubscriptionManager; if (!string.ReferenceEquals(tenantId, null)) { return(eventSubscriptionManager.findSignalEventSubscriptionsByEventNameAndTenantIdIncludeWithoutTenantId(signalName, tenantId)); } else { // find event subscriptions without tenant id return(eventSubscriptionManager.findSignalEventSubscriptionsByEventNameAndTenantId(signalName, null)); } }
public CourseContentsViewModel(CameraDeviceViewModel cameraDeviceViewModel, IEventAggregator eventAggregator, EventSubscriptionManager eventSubscriptionManager) : this() { CameraDeviceViewModel = cameraDeviceViewModel; CameraDeviceViewModel.SetSelectCameraDevice(0); _eventAggregator = eventAggregator; _eventSubscriptionManager = eventSubscriptionManager; SwitchDemonstrationSceneCommand = new DelegateCommand <string>(new Action <string>(SwitchScene)); OpenPreviewWindow = new DelegateCommand(() => { _eventAggregator.GetEvent <OpenPrevireWindowEvent>().Publish(true); }); _eventSubscriptionManager.Subscribe <SelectedDemonstrationWindowEvent, PreviewWindowInfo>(null, SelectedDemonstrationWindowEventHandler, null); }
private void SubscribeTo( TestEvent @event, TestSubscriber target, Action <TestEventArgs> handler, ExecutionOrder order ) { var sm = new EventSubscriptionManager(Aggregator); sm.Subscribe(b => { b.On(@event, target) .When(args => true) .Execute(handler, order); }); }
protected internal virtual void sendSignalToExecution(CommandContext commandContext, string signalName, string executionId) { ExecutionManager executionManager = commandContext.ExecutionManager; ExecutionEntity execution = executionManager.findExecutionById(executionId); ensureNotNull("Cannot find execution with id '" + executionId + "'", "execution", execution); EventSubscriptionManager eventSubscriptionManager = commandContext.EventSubscriptionManager; IList <EventSubscriptionEntity> signalEvents = eventSubscriptionManager.findSignalEventSubscriptionsByNameAndExecution(signalName, executionId); ensureNotEmpty("Execution '" + executionId + "' has not subscribed to a signal event with name '" + signalName + "'.", signalEvents); checkAuthorizationOfCatchSignals(commandContext, signalEvents); notifyExecutions(signalEvents); }
public ScreenLifecycle(EventAggregator aggregator, IScreenBase parent) { _parent = parent; _subscriptionManager = new EventSubscriptionManager(aggregator); _sm = new LifecycleStateMachine(_parent); DefineTransitions(); _subscriptionManager.Subscribe(b => b.AddSubscription(_sm) ); RegisterHandlerForINeedsInitializationImplementations(); _parent.Children.Add(this); }
private void UserControl_Loaded(object sender, RoutedEventArgs e) { var vm = this.DataContext as PowerCreatorPlayerViewModel; if (vm != null) { _eventSubscriptionManager = vm.EventSubscriptionManager; _desktopWindowCollector = vm.DesktopWindowCollector; _setupVideoLiveAndRecordingDevices = vm.SetupVideoLiveAndRecordingDevices; switchingVideoDeviceEventSubscriptionToken = _eventSubscriptionManager.Subscribe <SwitchingVideoDeviceEvent, VideoDeviceEventContext>(null, SwitchingVideoDeviceEventHandler, EventFilter); systemCloseEventSubscriptionToken = _eventSubscriptionManager.Subscribe <ShutDownEvent, bool>(null, SystemShutdown, null); } DefaultScene.Visibility = Visibility.Visible; _desktopWindowCollector?.SetWindowHandle(DefaultScene.Handle); _setupVideoLiveAndRecordingDevices?.SetVideoDevice(_desktopWindowCollector); }
protected internal virtual IList <EventSubscriptionEntity> findMessageStartEventSubscriptions(CommandContext commandContext, string messageName, CorrelationSet correlationSet) { EventSubscriptionManager eventSubscriptionManager = commandContext.EventSubscriptionManager; if (correlationSet.isTenantIdSet) { EventSubscriptionEntity eventSubscription = eventSubscriptionManager.findMessageStartEventSubscriptionByNameAndTenantId(messageName, correlationSet.TenantId); if (eventSubscription != null) { return(Collections.singletonList(eventSubscription)); } else { return(Collections.emptyList()); } } else { return(eventSubscriptionManager.findMessageStartEventSubscriptionByName(messageName)); } }
public RecordingControlViewModel( SystemConfig config, IEventAggregator eventAggregator, IUnityContainer container, EventSubscriptionManager eventSubscriptionManager) : this() { _config = config; _eventAggregator = eventAggregator; _eventSubscriptionManager = eventSubscriptionManager; _liveInfo = container.Resolve <LiveInfo>(); _speechVideoLiveAndRecordProvider = container.Resolve <SpeechVideoLiveAndRecordProvider>(); _teacherVideoLiveAndRecordProvider = container.Resolve <TeacherVideoLiveAndRecordProvider>(); StartRecordingBtnIsEnable = true; StartRecordingCommand = new DelegateCommand(new Action(StartRecording)); StopRecordingCommand = new DelegateCommand(new Action(StopRecording)); PauseRecordingCommand = new DelegateCommand(new Action(PauseRecording)); _eventSubscriptionManager.Subscribe <LiveAndRecordingOperateEvent, LiveAndRecordingOperateEventContext>(null, LiveOperateEventHandler, EventFilter); _eventSubscriptionManager.Subscribe <ShutDownEvent, bool>(null, SystemShutDownEventHandler, null); }
public void RemoveAllSubscriptions_HandlerAreNotExecutedAnymore() { var agg = CreateAggregator(); var firstEventHandler = new TestHandler(); var secondEventHandler = new TestHandler(); var sm = new EventSubscriptionManager(agg, b => { b.On(TestEvents.FirstEvent).Execute(firstEventHandler.HandleEvent); b.On(TestEvents.SecondEvent).Execute(secondEventHandler.HandleEvent); }); sm.RemoveAllSubscriptions(); agg.Publish(TestEvents.FirstEvent, new TestPayload()); agg.Publish(TestEvents.SecondEvent, new TestPayload()); Assert.AreEqual(0, firstEventHandler.Invocations); Assert.AreEqual(0, secondEventHandler.Invocations); GC.KeepAlive(sm); }
public void Publish_ExecutesHandlersInSpecifiedOrder() { var agg = CreateAggregator(); var log = new StringBuilder(); var firstHandler = new TestHandler(p => log.Append("First ")); var beforeDefaultHandler = new TestHandler(p => log.Append("BeforeDefault ")); var defaultHandler = new TestHandler(p => log.Append("Default ")); var afterDefaultHandler = new TestHandler(p => log.Append("AfterDefault ")); var lastHandler = new TestHandler(p => log.Append("Last ")); var sm1 = new EventSubscriptionManager(agg, b => { b.On(TestEvents.FirstEvent).Execute(firstHandler.HandleEvent, ExecutionOrder.First); }); sm1.Subscribe(b => { b.On(TestEvents.FirstEvent).Execute(defaultHandler.HandleEvent, ExecutionOrder.Default); b.On(TestEvents.FirstEvent).Execute(lastHandler.HandleEvent, ExecutionOrder.Last); }); var sm2 = new EventSubscriptionManager(agg, b => { b.On(TestEvents.FirstEvent).Execute(beforeDefaultHandler.HandleEvent, ExecutionOrder.BeforeDefault); b.On(TestEvents.FirstEvent).Execute(afterDefaultHandler.HandleEvent, ExecutionOrder.AfterDefault); }); agg.Publish(TestEvents.FirstEvent, new TestPayload()); Assert.AreEqual( "First BeforeDefault Default AfterDefault Last ", log.ToString() ); GC.KeepAlive(sm1); GC.KeepAlive(sm2); }
public LivingTimeViewModel(EventSubscriptionManager eventSubscriptionManager) : base(eventSubscriptionManager) { }