use of org.apache.camel.VetoCamelContextStartException in project camel by apache.
the class DefaultCamelContext method doStartCamel.
private void doStartCamel() throws Exception {
// 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 {} -> {}", new Object[] { key, value, replaced });
}
entry.setValue(replaced);
}
}
}
}
if (classResolver instanceof CamelContextAware) {
((CamelContextAware) classResolver).setCamelContext(this);
}
if (log.isDebugEnabled()) {
log.debug("Using ClassResolver={}, PackageScanClassResolver={}, ApplicationContextClassLoader={}", new Object[] { getClassResolver(), getPackageScanClassResolver(), getApplicationContextClassLoader() });
}
if (isStreamCaching()) {
log.info("StreamCaching is enabled on CamelContext: {}", getName());
}
if (isTracing()) {
// tracing is added in the DefaultChannel so we can enable it on the fly
log.info("Tracing is enabled on CamelContext: {}", getName());
}
if (isUseMDCLogging()) {
// log if MDC has been enabled
log.info("MDC logging is enabled on CamelContext: {}", getName());
}
if (isHandleFault()) {
// only add a new handle fault if not already configured
if (HandleFault.getHandleFault(this) == null) {
log.info("HandleFault is enabled on CamelContext: {}", getName());
addInterceptStrategy(new HandleFault());
}
}
if (getDelayer() != null && getDelayer() > 0) {
log.info("Delayer is enabled with: {} ms. on CamelContext: {}", getDelayer(), getName());
}
// register debugger
if (getDebugger() != null) {
log.info("Debugger: {} is enabled on CamelContext: {}", getDebugger(), getName());
// register this camel context on the debugger
getDebugger().setCamelContext(this);
startService(getDebugger());
addInterceptStrategy(new Debug(getDebugger()));
}
// start management strategy before lifecycles are started
ManagementStrategy managementStrategy = getManagementStrategy();
// inject CamelContext if aware
if (managementStrategy instanceof CamelContextAware) {
((CamelContextAware) managementStrategy).setCamelContext(this);
}
ServiceHelper.startService(managementStrategy);
// start lifecycle strategies
ServiceHelper.startServices(lifecycleStrategies);
Iterator<LifecycleStrategy> it = lifecycleStrategies.iterator();
while (it.hasNext()) {
LifecycleStrategy strategy = it.next();
try {
strategy.onContextStart(this);
} catch (VetoCamelContextStartException e) {
// okay we should not start Camel since it was vetoed
log.warn("Lifecycle strategy vetoed starting CamelContext ({}) due: {}", getName(), e.getMessage());
throw e;
} catch (Exception e) {
log.warn("Lifecycle strategy " + strategy + " failed starting CamelContext ({}) due: {}", getName(), e.getMessage());
throw e;
}
}
// start notifiers as services
for (EventNotifier notifier : getManagementStrategy().getEventNotifiers()) {
if (notifier instanceof Service) {
Service service = (Service) notifier;
for (LifecycleStrategy strategy : lifecycleStrategies) {
strategy.onServiceAdd(this, service, null);
}
}
if (notifier instanceof Service) {
startService((Service) notifier);
}
}
// must let some bootstrap service be started before we can notify the starting event
EventHelper.notifyCamelContextStarting(this);
forceLazyInitialization();
// 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 = new DefaultEndpointRegistry(this, endpoints);
// add this as service and force pre-start them
addService(endpoints, true, true);
// special for executorServiceManager as want to stop it manually so false in stopOnShutdown
addService(executorServiceManager, false, true);
addService(producerServicePool, true, true);
addService(pollingConsumerServicePool, true, true);
addService(inflightRepository, true, true);
addService(asyncProcessorAwaitManager, true, true);
addService(shutdownStrategy, true, true);
addService(packageScanClassResolver, true, true);
addService(restRegistry, true, true);
addService(messageHistoryFactory, true, true);
addService(runtimeCamelCatalog, true, true);
if (reloadStrategy != null) {
log.info("Using ReloadStrategy: {}", reloadStrategy);
addService(reloadStrategy, true, true);
}
// Initialize declarative transformer/validator registry
transformerRegistry = new DefaultTransformerRegistry(this, transformers);
addService(transformerRegistry, true, true);
validatorRegistry = new DefaultValidatorRegistry(this, validators);
addService(validatorRegistry, true, true);
if (runtimeEndpointRegistry != null) {
if (runtimeEndpointRegistry instanceof EventNotifier) {
getManagementStrategy().addEventNotifier((EventNotifier) runtimeEndpointRegistry);
}
addService(runtimeEndpointRegistry, true, true);
}
// eager lookup any configured properties component to avoid subsequent lookup attempts which may impact performance
// due we use properties component for property placeholder resolution at runtime
Component existing = CamelContextHelper.lookupPropertiesComponent(this, false);
if (existing != null) {
// store reference to the existing properties component
if (existing instanceof PropertiesComponent) {
propertiesComponent = (PropertiesComponent) existing;
} else {
// properties component must be expected type
throw new IllegalArgumentException("Found properties component of type: " + existing.getClass() + " instead of expected: " + PropertiesComponent.class);
}
}
// start components
startServices(components.values());
// start the route definitions before the routes is started
startRouteDefinitions(routeDefinitions);
// is there any stream caching enabled then log an info about this and its limit of spooling to disk, so people is aware of this
boolean streamCachingInUse = isStreamCaching();
if (!streamCachingInUse) {
for (RouteDefinition route : routeDefinitions) {
Boolean routeCache = CamelContextHelper.parseBoolean(this, route.getStreamCache());
if (routeCache != null && routeCache) {
streamCachingInUse = true;
break;
}
}
}
if (streamCachingInUse) {
// stream caching is in use so enable the strategy
getStreamCachingStrategy().setEnabled(true);
addService(getStreamCachingStrategy(), true, true);
} else {
// log if stream caching is not in use as this can help people to enable it if they use streams
log.info("StreamCaching is not in use. If using streams then its recommended to enable stream caching." + " See more details at http://camel.apache.org/stream-caching.html");
}
if (isAllowUseOriginalMessage()) {
log.debug("AllowUseOriginalMessage enabled because UseOriginalMessage is in use");
}
// start routes
if (doNotStartRoutesOnFirstStart) {
log.debug("Skip starting of routes as CamelContext has been configured with autoStartup=false");
}
// invoke this logic to warmup the routes and if possible also start the routes
doStartOrResumeRoutes(routeServices, true, !doNotStartRoutesOnFirstStart, false, true);
// starting will continue in the start method
}
use of org.apache.camel.VetoCamelContextStartException in project camel by apache.
the class DefaultManagementLifecycleStrategy method onContextStart.
public void onContextStart(CamelContext context) throws VetoCamelContextStartException {
Object mc = getManagementObjectStrategy().getManagedObjectForCamelContext(context);
String name = context.getName();
String managementName = context.getManagementNameStrategy().getName();
try {
boolean done = false;
while (!done) {
ObjectName on = getManagementStrategy().getManagementNamingStrategy().getObjectNameForCamelContext(managementName, name);
boolean exists = getManagementStrategy().isManaged(mc, on);
if (!exists) {
done = true;
} else {
// okay there exists already a CamelContext with this name, we can try to fix it by finding a free name
boolean fixed = false;
// if we use the default name strategy we can find a free name to use
String newName = findFreeName(mc, context.getManagementNameStrategy(), name);
if (newName != null) {
// use this as the fixed name
fixed = true;
done = true;
managementName = newName;
}
// we could not fix it so veto starting camel
if (!fixed) {
throw new VetoCamelContextStartException("CamelContext (" + context.getName() + ") with ObjectName[" + on + "] is already registered." + " Make sure to use unique names on CamelContext when using multiple CamelContexts in the same MBeanServer.", context);
} else {
LOG.warn("This CamelContext(" + context.getName() + ") will be registered using the name: " + managementName + " due to clash with an existing name already registered in MBeanServer.");
}
}
}
} catch (VetoCamelContextStartException e) {
// rethrow veto
throw e;
} catch (Exception e) {
// Camel to continue to run
throw ObjectHelper.wrapRuntimeCamelException(e);
}
// set the name we are going to use
if (context instanceof DefaultCamelContext) {
((DefaultCamelContext) context).setManagementName(managementName);
}
try {
manageObject(mc);
} catch (Exception e) {
// Camel to continue to run
throw ObjectHelper.wrapRuntimeCamelException(e);
}
// yes we made it and are initialized
initialized = true;
if (mc instanceof ManagedCamelContext) {
camelContextMBean = (ManagedCamelContext) mc;
}
// register any pre registered now that we are initialized
enlistPreRegisteredServices();
}
use of org.apache.camel.VetoCamelContextStartException in project camel by apache.
the class TwoManagedCamelContextClashTest method testTwoManagedCamelContextClash.
public void testTwoManagedCamelContextClash() throws Exception {
// JMX tests dont work well on AIX CI servers (hangs them)
if (isPlatform("aix")) {
return;
}
camel1 = createCamelContext("foo", "myFoo");
camel2 = createCamelContext("foo", "myFoo");
camel1.start();
assertTrue("Should be started", camel1.getStatus().isStarted());
MBeanServer mbeanServer = camel1.getManagementStrategy().getManagementAgent().getMBeanServer();
ObjectName on = ObjectName.getInstance("org.apache.camel:context=" + camel1.getManagementName() + ",type=context,name=\"foo\"");
assertTrue("Should be registered", mbeanServer.isRegistered(on));
// we use fixed names, so we will get a clash
try {
camel2.start();
fail("Should have thrown an exception");
} catch (VetoCamelContextStartException e) {
assertTrue(e.getMessage().contains("is already registered"));
}
}
use of org.apache.camel.VetoCamelContextStartException in project camel by apache.
the class DefaultCamelContext method start.
@Override
public void start() throws Exception {
try (MDCHelper mdcHelper = new MDCHelper()) {
vetoStated.set(false);
startDate = new Date();
stopWatch.restart();
log.info("Apache Camel " + getVersion() + " (CamelContext: " + getName() + ") is starting");
// Note: This is done on context start as we want to avoid doing it during object construction
// where we could be dealing with CDI proxied camel contexts which may never be started (CAMEL-9657)
// [TODO] Remove in 3.0
Container.Instance.manage(this);
doNotStartRoutesOnFirstStart = !firstStartDone && !isAutoStartup();
// then we may need to start the routes on the 2nd start call
if (firstStartDone && !isAutoStartup() && isStarted()) {
// invoke this logic to warm up the routes and if possible also start the routes
doStartOrResumeRoutes(routeServices, true, true, false, true);
}
// super will invoke doStart which will prepare internal services and start routes etc.
try {
firstStartDone = true;
super.start();
} catch (VetoCamelContextStartException e) {
// mark we veto against starting Camel
vetoStated.set(true);
if (e.isRethrowException()) {
throw e;
} else {
log.info("CamelContext ({}) vetoed to not start due {}", getName(), e.getMessage());
// swallow exception and change state of this camel context to stopped
stop();
return;
}
}
stopWatch.stop();
if (log.isInfoEnabled()) {
// count how many routes are actually started
int started = 0;
for (Route route : getRoutes()) {
if (getRouteStatus(route.getId()).isStarted()) {
started++;
}
}
log.info("Total " + getRoutes().size() + " routes, of which " + started + " are started.");
log.info("Apache Camel " + getVersion() + " (CamelContext: " + getName() + ") started in " + TimeUtils.printDuration(stopWatch.taken()));
}
EventHelper.notifyCamelContextStarted(this);
}
}
Aggregations