/// <summary> /// Performs the initialization. /// </summary> /// <param name="startTime">optional start time</param> protected void DoInitialize(long?startTime) { Log.Info("Initializing engine URI '" + _engineURI + "' version " + Version.VERSION); // This setting applies to all engines in a given VM ExecutionPathDebugLog.IsEnabled = _configSnapshot.EngineDefaults.Logging.IsEnableExecutionDebug; ExecutionPathDebugLog.IsTimerDebugEnabled = _configSnapshot.EngineDefaults.Logging.IsEnableTimerDebug; // This setting applies to all engines in a given VM MetricReportingPath.IsMetricsEnabled = _configSnapshot.EngineDefaults.MetricsReporting.IsEnableMetricsReporting; // This setting applies to all engines in a given VM AuditPath.AuditPattern = _configSnapshot.EngineDefaults.Logging.AuditPattern; // This setting applies to all engines in a given VM ThreadingOption.IsThreadingEnabled = ( ThreadingOption.IsThreadingEnabled || _configSnapshot.EngineDefaults.Threading.IsThreadPoolTimerExec || _configSnapshot.EngineDefaults.Threading.IsThreadPoolInbound || _configSnapshot.EngineDefaults.Threading.IsThreadPoolRouteExec || _configSnapshot.EngineDefaults.Threading.IsThreadPoolOutbound ); if (_engine != null) { _engine.Services.TimerService.StopInternalClock(false); // Give the timer thread a little moment to catch up Thread.Sleep(100); if (_configSnapshot.EngineDefaults.MetricsReporting.IsEnableMetricsReporting) { DestroyEngineMetrics(_engine.Services.EngineURI); } _engine.Runtime.Initialize(); _engine.Services.Dispose(); } // Make EP services context factory var epServicesContextFactoryClassName = _configSnapshot.EPServicesContextFactoryClassName; EPServicesContextFactory epServicesContextFactory; if (epServicesContextFactoryClassName == null) { // Check system properties epServicesContextFactoryClassName = Environment.GetEnvironmentVariable("ESPER_EPSERVICE_CONTEXT_FACTORY_CLASS"); } if (epServicesContextFactoryClassName == null) { epServicesContextFactory = new EPServicesContextFactoryDefault(); } else { Type clazz; try { clazz = TransientConfigurationResolver.ResolveClassForNameProvider( _configSnapshot.TransientConfiguration).ClassForName(epServicesContextFactoryClassName); } catch (TypeLoadException) { throw new ConfigurationException( "Type '" + epServicesContextFactoryClassName + "' cannot be loaded"); } Object obj; try { obj = Activator.CreateInstance(clazz); } catch (TypeLoadException) { throw new ConfigurationException("Class '" + clazz + "' cannot be instantiated"); } catch (MissingMethodException e) { throw new ConfigurationException( "Error instantiating class - Default constructor was not found", e); } catch (MethodAccessException e) { throw new ConfigurationException( "Error instantiating class - Caller does not have permission to use constructor", e); } catch (ArgumentException e) { throw new ConfigurationException("Error instantiating class - Type is not a RuntimeType", e); } epServicesContextFactory = (EPServicesContextFactory)obj; } var services = epServicesContextFactory.CreateServicesContext(this, _configSnapshot); // New runtime EPRuntimeSPI runtimeSPI; InternalEventRouteDest routeDest; TimerCallback timerCallback; var runtimeClassName = _configSnapshot.EngineDefaults.AlternativeContext.Runtime; if (runtimeClassName == null) { // Check system properties runtimeClassName = Environment.GetEnvironmentVariable("ESPER_EPRUNTIME_CLASS"); } if (runtimeClassName == null) { var runtimeImpl = new EPRuntimeImpl(services); runtimeSPI = runtimeImpl; routeDest = runtimeImpl; timerCallback = runtimeImpl.TimerCallback; } else { Type clazz; try { clazz = TypeHelper.ResolveType(runtimeClassName, true); } catch (TypeLoadException) { throw new ConfigurationException("Class '" + runtimeClassName + "' cannot be loaded"); } Object obj; try { var c = clazz.GetConstructor(new[] { typeof(EPServicesContext) }); obj = c.Invoke(new object[] { services }); } catch (TypeLoadException) { throw new ConfigurationException("Class '" + clazz + "' cannot be instantiated"); } catch (MissingMethodException) { throw new ConfigurationException( "Class '" + clazz + "' cannot be instantiated, constructor accepting services was not found"); } catch (MethodAccessException) { throw new ConfigurationException("Illegal access instantiating class '" + clazz + "'"); } runtimeSPI = (EPRuntimeSPI)obj; routeDest = (InternalEventRouteDest)obj; timerCallback = (TimerCallback)obj; } routeDest.InternalEventRouter = services.InternalEventRouter; services.InternalEventEngineRouteDest = routeDest; // set current time, if applicable if (startTime != null) { services.SchedulingService.Time = startTime.Value; } // Configure services to use the new runtime services.TimerService.Callback = timerCallback; // Statement lifecycle init services.StatementLifecycleSvc.Init(); // Filter service init services.FilterService.Init(); // Schedule service init services.SchedulingService.Init(); // New admin var configOps = new ConfigurationOperationsImpl( services.EventAdapterService, services.EventTypeIdGenerator, services.EngineImportService, services.VariableService, services.EngineSettingsService, services.ValueAddEventService, services.MetricsReportingService, services.StatementEventTypeRefService, services.StatementVariableRefService, services.PlugInViews, services.FilterService, services.PatternSubexpressionPoolSvc, services.MatchRecognizeStatePoolEngineSvc, services.TableService, _configSnapshot.TransientConfiguration); var defaultStreamSelector = SelectClauseStreamSelectorEnumExtensions.MapFromSODA( _configSnapshot.EngineDefaults.StreamSelection.DefaultStreamSelector); EPAdministratorSPI adminSPI; var adminClassName = _configSnapshot.EngineDefaults.AlternativeContext.Admin; var adminContext = new EPAdministratorContext(runtimeSPI, services, configOps, defaultStreamSelector); if (adminClassName == null) { // Check system properties adminClassName = Environment.GetEnvironmentVariable("ESPER_EPADMIN_CLASS"); } if (adminClassName == null) { adminSPI = new EPAdministratorImpl(adminContext); } else { Type clazz; try { clazz = TypeHelper.ResolveType(adminClassName, true); } catch (TypeLoadException) { throw new ConfigurationException( "Class '" + epServicesContextFactoryClassName + "' cannot be loaded"); } Object obj; try { var c = clazz.GetConstructor(new[] { typeof(EPAdministratorContext) }); obj = c.Invoke(new[] { adminContext }); } catch (TypeLoadException) { throw new ConfigurationException("Class '" + clazz + "' cannot be instantiated"); } catch (MissingMethodException) { throw new ConfigurationException( "Class '" + clazz + "' cannot be instantiated, constructor accepting context was not found"); } catch (MethodAccessException) { throw new ConfigurationException("Illegal access instantiating class '" + clazz + "'"); } adminSPI = (EPAdministratorSPI)obj; } // Start clocking if (_configSnapshot.EngineDefaults.Threading.IsInternalTimerEnabled) { if (_configSnapshot.EngineDefaults.TimeSource.TimeUnit != TimeUnit.MILLISECONDS) { throw new ConfigurationException("Internal timer requires millisecond time resolution"); } services.TimerService.StartInternalClock(); } // Give the timer thread a little moment to start up Thread.Sleep(100); // Save engine instance _engine = new EPServiceEngine(services, runtimeSPI, adminSPI); // Load and initialize adapter loader classes LoadAdapters(services); // Initialize extension services if (services.EngineLevelExtensionServicesContext != null) { services.EngineLevelExtensionServicesContext.Init(services, runtimeSPI, adminSPI); } // Start metrics reporting, if any if (_configSnapshot.EngineDefaults.MetricsReporting.IsEnableMetricsReporting) { services.MetricsReportingService.SetContext(runtimeSPI, services); } // Start engine metrics report if (_configSnapshot.EngineDefaults.MetricsReporting.IsEnableMetricsReporting) { StartEngineMetrics(services, runtimeSPI); } // register with the statement lifecycle service services.StatementLifecycleSvc.LifecycleEvent += HandleLifecycleEvent; // call initialize listeners try { if (ServiceInitialized != null) { ServiceInitialized(this, new ServiceProviderEventArgs(this)); } } catch (Exception ex) { Log.Error("Runtime exception caught during an ServiceInitialized event:" + ex.Message, ex); } }
public void Dispose() { lock (this) { if (_engine != null) { Log.Info("Destroying engine URI '" + _engineURI + "'"); try { if (ServiceDestroyRequested != null) { ServiceDestroyRequested(this, new ServiceProviderEventArgs(this)); } } catch (Exception ex) { Log.Error("Exception caught during an ServiceDestroyRequested event:" + ex.Message, ex); } if (_configSnapshot.EngineDefaults.MetricsReporting.IsEnableMetricsReporting) { DestroyEngineMetrics(_engine.Services.EngineURI); } // assign null value var engineToDestroy = _engine; engineToDestroy.Services.TimerService.StopInternalClock(false); // Give the timer thread a little moment to catch up Thread.Sleep(100); // plugin-loaders - destroy in opposite order var pluginLoaders = engineToDestroy.Services.ConfigSnapshot.PluginLoaders; if (!pluginLoaders.IsEmpty()) { var reversed = new List <ConfigurationPluginLoader>(pluginLoaders); reversed.Reverse(); foreach (var config in reversed) { try { using ((PluginLoader)engineToDestroy.Services.EngineEnvContext.Lookup("plugin-loader/" + config.LoaderName)) { } } catch (Exception e) { Log.Error("Error destroying plug-in loader: " + config.LoaderName, e); } } } engineToDestroy.Services.ThreadingService.Dispose(); // assign null - making EPRuntime and EPAdministrator unobtainable _engine = null; engineToDestroy.Runtime.Dispose(); engineToDestroy.Admin.Dispose(); engineToDestroy.Services.Dispose(); _runtimes.Remove(_engineURI); engineToDestroy.Services.Initialize(); } } }