in core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java [2267:2509]
public void doInit() throws Exception {
final StopWatch watch = new StopWatch();
vetoed = null;
final StartupStepRecorder startupStepRecorder = camelContextExtension.getStartupStepRecorder();
StartupStep step = startupStepRecorder.beginStep(CamelContext.class, null, "Init CamelContext");
// init the route controller
final RouteController routeController = getRouteController();
if (startupSummaryLevel == StartupSummaryLevel.Verbose) {
// verbose startup should let route controller do the route startup logging
if (routeController.getLoggingLevel().ordinal() < LoggingLevel.INFO.ordinal()) {
routeController.setLoggingLevel(LoggingLevel.INFO);
}
}
// init the shutdown strategy
final ShutdownStrategy shutdownStrategy = getShutdownStrategy();
if (startupSummaryLevel == StartupSummaryLevel.Verbose) {
// verbose startup should let route controller do the route shutdown logging
if (shutdownStrategy != null && shutdownStrategy.getLoggingLevel().ordinal() < LoggingLevel.INFO.ordinal()) {
shutdownStrategy.setLoggingLevel(LoggingLevel.INFO);
}
}
// optimize - before starting routes lets check if event notifications are possible
camelContextExtension.setEventNotificationApplicable(EventHelper.eventsApplicable(this));
// ensure additional type converters is loaded (either if enabled or we should use package scanning from the base)
boolean load = loadTypeConverters || camelContextExtension.getBasePackageScan() != null;
final TypeConverter typeConverter = camelContextExtension.getTypeConverter();
if (load && typeConverter instanceof AnnotationScanTypeConverters annotationScanTypeConverters) {
StartupStep step2 = startupStepRecorder.beginStep(CamelContext.class, null, "Scan TypeConverters");
annotationScanTypeConverters.scanTypeConverters();
startupStepRecorder.endStep(step2);
}
// ensure additional health checks is loaded
if (loadHealthChecks) {
StartupStep step3 = startupStepRecorder.beginStep(CamelContext.class, null, "Scan HealthChecks");
HealthCheckRegistry hcr = getCamelContextExtension().getContextPlugin(HealthCheckRegistry.class);
if (hcr != null) {
hcr.loadHealthChecks();
}
startupStepRecorder.endStep(step3);
}
// ensure additional dev consoles is loaded
if (devConsole) {
StartupStep step4 = startupStepRecorder.beginStep(CamelContext.class, null, "Scan DevConsoles (phase 1)");
DevConsoleRegistry dcr = getCamelContextExtension().getContextPlugin(DevConsoleRegistry.class);
if (dcr != null) {
dcr.loadDevConsoles();
}
startupStepRecorder.endStep(step4);
}
// custom properties may use property placeholders so resolve those
// early on
if (globalOptions != null && !globalOptions.isEmpty()) {
for (Map.Entry<String, String> entry : globalOptions.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (value != null) {
String replaced = resolvePropertyPlaceholders(value);
if (!value.equals(replaced)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Camel property with key {} replaced value from {} -> {}", key, value, replaced);
}
entry.setValue(replaced);
}
}
}
}
forceLazyInitialization();
// setup cli-connector if not already done
if (hasService(CliConnector.class) == null) {
CliConnectorFactory ccf = getCamelContextExtension().getContextPlugin(CliConnectorFactory.class);
if (ccf != null && ccf.isEnabled()) {
CliConnector connector = ccf.createConnector();
addService(connector, true);
// force start cli connector early as otherwise it will be deferred until context is started
// but, we want status available during startup phase
ServiceHelper.startService(connector);
}
}
// auto-detect camel-debug on classpath (if debugger has not been explicit added)
boolean debuggerDetected = false;
if (getDebugger() == null && hasService(BacklogDebugger.class) == null) {
// detect if camel-debug is on classpath that enables debugging
DebuggerFactory df = getCamelContextExtension().getBootstrapFactoryFinder()
.newInstance(Debugger.FACTORY, DebuggerFactory.class).orElse(null);
if (df != null) {
debuggerDetected = true;
LOG.info("Detected: {} JAR (Enabling Camel Debugging)", df);
setDebugging(true);
Debugger newDebugger = df.createDebugger(this);
if (newDebugger != null) {
setDebugger(newDebugger);
}
}
}
if (!debuggerDetected && (isDebugging() || isDebugStandby())) {
if (hasService(BacklogDebugger.class) == null) {
// debugging enabled but camel-debug was not auto-detected from classpath
// so install default debugger
BacklogDebugger backlog = DefaultBacklogDebugger.createDebugger(this);
addService(backlog, true, true);
}
}
addService(getManagementStrategy(), false);
// check startup conditions before we can continue
StartupConditionStrategy scs = getCamelContextExtension().getContextPlugin(StartupConditionStrategy.class);
scs.checkStartupConditions();
lifecycleStrategies.sort(OrderedComparator.get());
ServiceHelper.initService(lifecycleStrategies);
for (LifecycleStrategy strategy : lifecycleStrategies) {
try {
strategy.onContextInitializing(this);
} catch (VetoCamelContextStartException e) {
// okay we should not start Camel since it was vetoed
LOG.warn("Lifecycle strategy {} vetoed initializing CamelContext ({}) due to: {}", strategy,
camelContextExtension.getName(),
e.getMessage());
throw e;
} catch (Exception e) {
LOG.warn("Lifecycle strategy {} failed initializing CamelContext ({}) due to: {}", strategy,
camelContextExtension.getName(),
e.getMessage());
throw e;
}
}
// optimize - before starting routes lets check if event notifications are possible
camelContextExtension.setEventNotificationApplicable(EventHelper.eventsApplicable(this));
// start notifiers as services
for (EventNotifier notifier : getManagementStrategy().getEventNotifiers()) {
if (notifier instanceof Service service) {
for (LifecycleStrategy strategy : lifecycleStrategies) {
strategy.onServiceAdd(getCamelContextReference(), service, null);
}
}
ServiceHelper.initService(notifier);
}
// the event notifiers must be initialized before we can emit this event
EventHelper.notifyCamelContextInitializing(this);
// re-create endpoint registry as the cache size limit may be set after the constructor of this instance was called.
// and we needed to create endpoints up-front as it may be accessed before this context is started
endpoints = internalServiceManager.addService(this, createEndpointRegistry(endpoints));
// optimised to not include runtimeEndpointRegistry unless startServices
// is enabled or JMX statistics is in extended mode
if (runtimeEndpointRegistry == null && getManagementStrategy() != null
&& getManagementStrategy().getManagementAgent() != null) {
Boolean isEnabled = getManagementStrategy().getManagementAgent().getEndpointRuntimeStatisticsEnabled();
boolean isExtended = getManagementStrategy().getManagementAgent().getStatisticsLevel().isExtended();
// extended mode is either if we use Extended statistics level or
// the option is explicit enabled
boolean extended = isExtended || isEnabled != null && isEnabled;
if (extended) {
runtimeEndpointRegistry = new DefaultRuntimeEndpointRegistry();
}
}
if (runtimeEndpointRegistry != null) {
if (runtimeEndpointRegistry instanceof EventNotifier && getManagementStrategy() != null) {
getManagementStrategy().addEventNotifier((EventNotifier) runtimeEndpointRegistry);
}
addService(runtimeEndpointRegistry, true, true);
}
bindDataFormats();
// init components
ServiceHelper.initService(components.values());
// create route definitions from route templates if we have any sources
for (RouteTemplateParameterSource source : camelContextExtension.getRegistry()
.findByType(RouteTemplateParameterSource.class)) {
for (String routeId : source.routeIds()) {
// do a defensive copy of the parameters
Map<String, Object> map = new HashMap<>(source.parameters(routeId));
Object templateId = map.remove(RouteTemplateParameterSource.TEMPLATE_ID);
if (templateId == null) {
// use alternative style as well
templateId = map.remove("template-id");
}
final String id = templateId != null ? templateId.toString() : null;
if (id == null) {
throw new IllegalArgumentException(
"RouteTemplateParameterSource with routeId: " + routeId + " has no templateId defined");
}
addRouteFromTemplate(routeId, id, map);
}
}
// init the route definitions before the routes is started
StartupStep subStep = startupStepRecorder.beginStep(CamelContext.class, camelContextExtension.getName(), "Init Routes");
// the method is called start but at this point it will only initialize (as context is starting up)
startRouteDefinitions();
// this will init route definitions and populate as route services which we can then initialize now
internalRouteStartupManager.doInitRoutes(this, routeServices);
startupStepRecorder.endStep(subStep);
if (!lifecycleStrategies.isEmpty()) {
subStep = startupStepRecorder.beginStep(CamelContext.class, camelContextExtension.getName(),
"LifecycleStrategy onContextInitialized");
for (LifecycleStrategy strategy : lifecycleStrategies) {
try {
strategy.onContextInitialized(this);
} catch (VetoCamelContextStartException e) {
// okay we should not start Camel since it was vetoed
LOG.warn("Lifecycle strategy {} vetoed initializing CamelContext ({}) due to: {}", strategy,
camelContextExtension.getName(),
e.getMessage());
throw e;
} catch (Exception e) {
LOG.warn("Lifecycle strategy {} failed initializing CamelContext ({}) due to: {}", strategy,
camelContextExtension.getName(),
e.getMessage());
throw e;
}
}
startupStepRecorder.endStep(subStep);
}
EventHelper.notifyCamelContextInitialized(this);
startupStepRecorder.endStep(step);
if (LOG.isDebugEnabled()) {
initTaken = watch.taken();
LOG.debug("Apache Camel {} ({}) initialized in {}", getVersion(), camelContextExtension.getName(),
TimeUtils.printDuration(initTaken, true));
}
}