use of org.apache.camel.spi.EventNotifier in project camel by apache.
the class RoutesCollector method onApplicationEvent.
// Overridden
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
// only listen to context refresh of "my" applicationContext
if (this.applicationContext.equals(applicationContext)) {
CamelContext camelContext = event.getApplicationContext().getBean(CamelContext.class);
// only add and start Camel if its stopped (initial state)
if (camelContext.getStatus().isStopped()) {
LOG.debug("Post-processing CamelContext bean: {}", camelContext.getName());
for (RoutesBuilder routesBuilder : applicationContext.getBeansOfType(RoutesBuilder.class, configurationProperties.isIncludeNonSingletons(), true).values()) {
// filter out abstract classes
boolean abs = Modifier.isAbstract(routesBuilder.getClass().getModifiers());
if (!abs) {
try {
LOG.debug("Injecting following route into the CamelContext: {}", routesBuilder);
camelContext.addRoutes(routesBuilder);
} catch (Exception e) {
throw new CamelSpringBootInitializationException(e);
}
}
}
try {
boolean scan = !configurationProperties.getXmlRoutes().equals("false");
if (scan) {
loadXmlRoutes(applicationContext, camelContext, configurationProperties.getXmlRoutes());
}
boolean scanRests = !configurationProperties.getXmlRests().equals("false");
if (scanRests) {
loadXmlRests(applicationContext, camelContext, configurationProperties.getXmlRests());
}
for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
LOG.debug("CamelContextConfiguration found. Invoking beforeApplicationStart: {}", camelContextConfiguration);
camelContextConfiguration.beforeApplicationStart(camelContext);
}
if (configurationProperties.isMainRunController()) {
CamelMainRunController controller = new CamelMainRunController(applicationContext, camelContext);
if (configurationProperties.getDurationMaxMessages() > 0 || configurationProperties.getDurationMaxIdleSeconds() > 0) {
if (configurationProperties.getDurationMaxMessages() > 0) {
LOG.info("CamelSpringBoot will terminate after processing {} messages", configurationProperties.getDurationMaxMessages());
}
if (configurationProperties.getDurationMaxIdleSeconds() > 0) {
LOG.info("CamelSpringBoot will terminate after being idle for more {} seconds", configurationProperties.getDurationMaxIdleSeconds());
}
// register lifecycle so we can trigger to shutdown the JVM when maximum number of messages has been processed
EventNotifier notifier = new MainDurationEventNotifier(camelContext, configurationProperties.getDurationMaxMessages(), configurationProperties.getDurationMaxIdleSeconds(), controller.getCompleted(), controller.getLatch(), true);
// register our event notifier
ServiceHelper.startService(notifier);
camelContext.getManagementStrategy().addEventNotifier(notifier);
}
if (configurationProperties.getDurationMaxSeconds() > 0) {
LOG.info("CamelSpringBoot will terminate after {} seconds", configurationProperties.getDurationMaxSeconds());
terminateMainControllerAfter(camelContext, configurationProperties.getDurationMaxSeconds(), controller.getCompleted(), controller.getLatch());
}
// controller will start Camel
LOG.info("Starting CamelMainRunController to ensure the main thread keeps running");
controller.start();
} else {
if (applicationContext instanceof ConfigurableApplicationContext) {
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) applicationContext;
if (configurationProperties.getDurationMaxSeconds() > 0) {
LOG.info("CamelSpringBoot will terminate after {} seconds", configurationProperties.getDurationMaxSeconds());
terminateApplicationContext(cac, camelContext, configurationProperties.getDurationMaxSeconds());
}
if (configurationProperties.getDurationMaxMessages() > 0 || configurationProperties.getDurationMaxIdleSeconds() > 0) {
if (configurationProperties.getDurationMaxMessages() > 0) {
LOG.info("CamelSpringBoot will terminate after processing {} messages", configurationProperties.getDurationMaxMessages());
}
if (configurationProperties.getDurationMaxIdleSeconds() > 0) {
LOG.info("CamelSpringBoot will terminate after being idle for more {} seconds", configurationProperties.getDurationMaxIdleSeconds());
}
// needed by MainDurationEventNotifier to signal when we have processed the max messages
final AtomicBoolean completed = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
// register lifecycle so we can trigger to shutdown the JVM when maximum number of messages has been processed
EventNotifier notifier = new MainDurationEventNotifier(camelContext, configurationProperties.getDurationMaxMessages(), configurationProperties.getDurationMaxIdleSeconds(), completed, latch, false);
// register our event notifier
ServiceHelper.startService(notifier);
camelContext.getManagementStrategy().addEventNotifier(notifier);
terminateApplicationContext(cac, camelContext, latch);
}
}
// start camel manually
maybeStart(camelContext);
}
for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
LOG.debug("CamelContextConfiguration found. Invoking afterApplicationStart: {}", camelContextConfiguration);
camelContextConfiguration.afterApplicationStart(camelContext);
}
} catch (Exception e) {
throw new CamelSpringBootInitializationException(e);
}
} else {
LOG.debug("Camel already started, not adding routes.");
}
} else {
LOG.debug("Ignore ContextRefreshedEvent: {}", event);
}
}
use of org.apache.camel.spi.EventNotifier in project camel by apache.
the class CamelAutoConfiguration method afterPropertiesSet.
/**
* Performs additional configuration to lookup beans of Camel types to configure
* advanced configurations.
* <p/>
* Similar code in camel-core-xml module in class org.apache.camel.core.xml.AbstractCamelContextFactoryBean.
*/
void afterPropertiesSet(ApplicationContext applicationContext, CamelContext camelContext) {
Tracer tracer = getSingleBeanOfType(applicationContext, Tracer.class);
if (tracer != null) {
// use formatter if there is a TraceFormatter bean defined
TraceFormatter formatter = getSingleBeanOfType(applicationContext, TraceFormatter.class);
if (formatter != null) {
tracer.setFormatter(formatter);
}
LOG.info("Using custom Tracer: {}", tracer);
camelContext.addInterceptStrategy(tracer);
}
BacklogTracer backlogTracer = getSingleBeanOfType(applicationContext, BacklogTracer.class);
if (backlogTracer != null) {
LOG.info("Using custom BacklogTracer: {}", backlogTracer);
camelContext.addInterceptStrategy(backlogTracer);
}
HandleFault handleFault = getSingleBeanOfType(applicationContext, HandleFault.class);
if (handleFault != null) {
LOG.info("Using custom HandleFault: {}", handleFault);
camelContext.addInterceptStrategy(handleFault);
}
InflightRepository inflightRepository = getSingleBeanOfType(applicationContext, InflightRepository.class);
if (inflightRepository != null) {
LOG.info("Using custom InflightRepository: {}", inflightRepository);
camelContext.setInflightRepository(inflightRepository);
}
AsyncProcessorAwaitManager asyncProcessorAwaitManager = getSingleBeanOfType(applicationContext, AsyncProcessorAwaitManager.class);
if (asyncProcessorAwaitManager != null) {
LOG.info("Using custom AsyncProcessorAwaitManager: {}", asyncProcessorAwaitManager);
camelContext.setAsyncProcessorAwaitManager(asyncProcessorAwaitManager);
}
ManagementStrategy managementStrategy = getSingleBeanOfType(applicationContext, ManagementStrategy.class);
if (managementStrategy != null) {
LOG.info("Using custom ManagementStrategy: {}", managementStrategy);
camelContext.setManagementStrategy(managementStrategy);
}
ManagementNamingStrategy managementNamingStrategy = getSingleBeanOfType(applicationContext, ManagementNamingStrategy.class);
if (managementNamingStrategy != null) {
LOG.info("Using custom ManagementNamingStrategy: {}", managementNamingStrategy);
camelContext.getManagementStrategy().setManagementNamingStrategy(managementNamingStrategy);
}
EventFactory eventFactory = getSingleBeanOfType(applicationContext, EventFactory.class);
if (eventFactory != null) {
LOG.info("Using custom EventFactory: {}", eventFactory);
camelContext.getManagementStrategy().setEventFactory(eventFactory);
}
UnitOfWorkFactory unitOfWorkFactory = getSingleBeanOfType(applicationContext, UnitOfWorkFactory.class);
if (unitOfWorkFactory != null) {
LOG.info("Using custom UnitOfWorkFactory: {}", unitOfWorkFactory);
camelContext.setUnitOfWorkFactory(unitOfWorkFactory);
}
RuntimeEndpointRegistry runtimeEndpointRegistry = getSingleBeanOfType(applicationContext, RuntimeEndpointRegistry.class);
if (runtimeEndpointRegistry != null) {
LOG.info("Using custom RuntimeEndpointRegistry: {}", runtimeEndpointRegistry);
camelContext.setRuntimeEndpointRegistry(runtimeEndpointRegistry);
}
// custom type converters defined as <bean>s
Map<String, TypeConverters> typeConverters = applicationContext.getBeansOfType(TypeConverters.class);
if (typeConverters != null && !typeConverters.isEmpty()) {
for (Map.Entry<String, TypeConverters> entry : typeConverters.entrySet()) {
TypeConverters converter = entry.getValue();
LOG.info("Adding custom TypeConverters with id: {} and implementation: {}", entry.getKey(), converter);
camelContext.getTypeConverterRegistry().addTypeConverters(converter);
}
}
// set the event notifier strategies if defined
Map<String, EventNotifier> eventNotifiers = applicationContext.getBeansOfType(EventNotifier.class);
if (eventNotifiers != null && !eventNotifiers.isEmpty()) {
for (Map.Entry<String, EventNotifier> entry : eventNotifiers.entrySet()) {
EventNotifier notifier = entry.getValue();
// do not add if already added, for instance a tracer that is also an InterceptStrategy class
if (!camelContext.getManagementStrategy().getEventNotifiers().contains(notifier)) {
LOG.info("Using custom EventNotifier with id: {} and implementation: {}", entry.getKey(), notifier);
camelContext.getManagementStrategy().addEventNotifier(notifier);
}
}
}
// set endpoint strategies if defined
Map<String, EndpointStrategy> endpointStrategies = applicationContext.getBeansOfType(EndpointStrategy.class);
if (endpointStrategies != null && !endpointStrategies.isEmpty()) {
for (Map.Entry<String, EndpointStrategy> entry : endpointStrategies.entrySet()) {
EndpointStrategy strategy = entry.getValue();
LOG.info("Using custom EndpointStrategy with id: {} and implementation: {}", entry.getKey(), strategy);
camelContext.addRegisterEndpointCallback(strategy);
}
}
// shutdown
ShutdownStrategy shutdownStrategy = getSingleBeanOfType(applicationContext, ShutdownStrategy.class);
if (shutdownStrategy != null) {
LOG.info("Using custom ShutdownStrategy: " + shutdownStrategy);
camelContext.setShutdownStrategy(shutdownStrategy);
}
// add global interceptors
Map<String, InterceptStrategy> interceptStrategies = applicationContext.getBeansOfType(InterceptStrategy.class);
if (interceptStrategies != null && !interceptStrategies.isEmpty()) {
for (Map.Entry<String, InterceptStrategy> entry : interceptStrategies.entrySet()) {
InterceptStrategy strategy = entry.getValue();
// do not add if already added, for instance a tracer that is also an InterceptStrategy class
if (!camelContext.getInterceptStrategies().contains(strategy)) {
LOG.info("Using custom InterceptStrategy with id: {} and implementation: {}", entry.getKey(), strategy);
camelContext.addInterceptStrategy(strategy);
}
}
}
// set the lifecycle strategy if defined
Map<String, LifecycleStrategy> lifecycleStrategies = applicationContext.getBeansOfType(LifecycleStrategy.class);
if (lifecycleStrategies != null && !lifecycleStrategies.isEmpty()) {
for (Map.Entry<String, LifecycleStrategy> entry : lifecycleStrategies.entrySet()) {
LifecycleStrategy strategy = entry.getValue();
// do not add if already added, for instance a tracer that is also an InterceptStrategy class
if (!camelContext.getLifecycleStrategies().contains(strategy)) {
LOG.info("Using custom LifecycleStrategy with id: {} and implementation: {}", entry.getKey(), strategy);
camelContext.addLifecycleStrategy(strategy);
}
}
}
// add route policy factories
Map<String, RoutePolicyFactory> routePolicyFactories = applicationContext.getBeansOfType(RoutePolicyFactory.class);
if (routePolicyFactories != null && !routePolicyFactories.isEmpty()) {
for (Map.Entry<String, RoutePolicyFactory> entry : routePolicyFactories.entrySet()) {
RoutePolicyFactory factory = entry.getValue();
LOG.info("Using custom RoutePolicyFactory with id: {} and implementation: {}", entry.getKey(), factory);
camelContext.addRoutePolicyFactory(factory);
}
}
// set the default thread pool profile if defined
initThreadPoolProfiles(applicationContext, camelContext);
}
use of org.apache.camel.spi.EventNotifier in project camel by apache.
the class DefaultManagementLifecycleStrategy method getManagedObjectForService.
@SuppressWarnings("unchecked")
private Object getManagedObjectForService(CamelContext context, Service service, Route route) {
// skip channel, UoW and dont double wrap instrumentation
if (service instanceof Channel || service instanceof UnitOfWork || service instanceof InstrumentationProcessor) {
return null;
}
// skip non managed services
if (service instanceof NonManagedService) {
return null;
}
Object answer = null;
if (service instanceof ManagementAware) {
return ((ManagementAware<Service>) service).getManagedObject(service);
} else if (service instanceof Tracer) {
// special for tracer
Tracer tracer = (Tracer) service;
ManagedTracer mt = managedTracers.get(tracer);
if (mt == null) {
mt = new ManagedTracer(context, tracer);
mt.init(getManagementStrategy());
managedTracers.put(tracer, mt);
}
return mt;
} else if (service instanceof BacklogTracer) {
// special for backlog tracer
BacklogTracer backlogTracer = (BacklogTracer) service;
ManagedBacklogTracer mt = managedBacklogTracers.get(backlogTracer);
if (mt == null) {
mt = new ManagedBacklogTracer(context, backlogTracer);
mt.init(getManagementStrategy());
managedBacklogTracers.put(backlogTracer, mt);
}
return mt;
} else if (service instanceof BacklogDebugger) {
// special for backlog debugger
BacklogDebugger backlogDebugger = (BacklogDebugger) service;
ManagedBacklogDebugger md = managedBacklogDebuggers.get(backlogDebugger);
if (md == null) {
md = new ManagedBacklogDebugger(context, backlogDebugger);
md.init(getManagementStrategy());
managedBacklogDebuggers.put(backlogDebugger, md);
}
return md;
} else if (service instanceof DataFormat) {
answer = getManagementObjectStrategy().getManagedObjectForDataFormat(context, (DataFormat) service);
} else if (service instanceof Producer) {
answer = getManagementObjectStrategy().getManagedObjectForProducer(context, (Producer) service);
} else if (service instanceof Consumer) {
answer = getManagementObjectStrategy().getManagedObjectForConsumer(context, (Consumer) service);
} else if (service instanceof Processor) {
// special for processors as we need to do some extra work
return getManagedObjectForProcessor(context, (Processor) service, route);
} else if (service instanceof ThrottlingInflightRoutePolicy) {
answer = new ManagedThrottlingInflightRoutePolicy(context, (ThrottlingInflightRoutePolicy) service);
} else if (service instanceof ThrottlingExceptionRoutePolicy) {
answer = new ManagedThrottlingExceptionRoutePolicy(context, (ThrottlingExceptionRoutePolicy) service);
} else if (service instanceof ConsumerCache) {
answer = new ManagedConsumerCache(context, (ConsumerCache) service);
} else if (service instanceof ProducerCache) {
answer = new ManagedProducerCache(context, (ProducerCache) service);
} else if (service instanceof DefaultEndpointRegistry) {
answer = new ManagedEndpointRegistry(context, (DefaultEndpointRegistry) service);
} else if (service instanceof TypeConverterRegistry) {
answer = new ManagedTypeConverterRegistry(context, (TypeConverterRegistry) service);
} else if (service instanceof RestRegistry) {
answer = new ManagedRestRegistry(context, (RestRegistry) service);
} else if (service instanceof InflightRepository) {
answer = new ManagedInflightRepository(context, (InflightRepository) service);
} else if (service instanceof AsyncProcessorAwaitManager) {
answer = new ManagedAsyncProcessorAwaitManager(context, (AsyncProcessorAwaitManager) service);
} else if (service instanceof RuntimeEndpointRegistry) {
answer = new ManagedRuntimeEndpointRegistry(context, (RuntimeEndpointRegistry) service);
} else if (service instanceof StreamCachingStrategy) {
answer = new ManagedStreamCachingStrategy(context, (StreamCachingStrategy) service);
} else if (service instanceof EventNotifier) {
answer = getManagementObjectStrategy().getManagedObjectForEventNotifier(context, (EventNotifier) service);
} else if (service instanceof TransformerRegistry) {
answer = new ManagedTransformerRegistry(context, (TransformerRegistry) service);
} else if (service instanceof ValidatorRegistry) {
answer = new ManagedValidatorRegistry(context, (ValidatorRegistry) service);
} else if (service instanceof RuntimeCamelCatalog) {
answer = new ManagedRuntimeCamelCatalog(context, (RuntimeCamelCatalog) service);
} else if (service != null) {
// fallback as generic service
answer = getManagementObjectStrategy().getManagedObjectForService(context, service);
}
if (answer != null && answer instanceof ManagedService) {
ManagedService ms = (ManagedService) answer;
ms.setRoute(route);
ms.init(getManagementStrategy());
}
return answer;
}
use of org.apache.camel.spi.EventNotifier in project camel by apache.
the class DefaultManagementStrategy method doStartManagementStrategy.
protected void doStartManagementStrategy() throws Exception {
ObjectHelper.notNull(camelContext, "CamelContext");
if (eventNotifiers != null) {
for (EventNotifier notifier : eventNotifiers) {
// inject CamelContext if the service is aware
if (notifier instanceof CamelContextAware) {
CamelContextAware aware = (CamelContextAware) notifier;
aware.setCamelContext(camelContext);
}
ServiceHelper.startService(notifier);
}
}
if (managementAgent != null) {
ServiceHelper.startService(managementAgent);
// set the naming strategy using the domain name from the agent
if (managementNamingStrategy == null) {
setManagementNamingStrategy(new DefaultManagementNamingStrategy(managementAgent.getMBeanObjectDomainName()));
}
}
if (managementNamingStrategy instanceof CamelContextAware) {
((CamelContextAware) managementNamingStrategy).setCamelContext(getCamelContext());
}
}
use of org.apache.camel.spi.EventNotifier in project camel by apache.
the class EventHelper method notifyCamelContextStartupFailed.
public static void notifyCamelContextStartupFailed(CamelContext context, Throwable cause) {
ManagementStrategy management = context.getManagementStrategy();
if (management == null) {
return;
}
List<EventNotifier> notifiers = management.getEventNotifiers();
if (notifiers == null || notifiers.isEmpty()) {
return;
}
for (EventNotifier notifier : notifiers) {
if (notifier.isIgnoreCamelContextEvents()) {
continue;
}
EventFactory factory = management.getEventFactory();
if (factory == null) {
return;
}
EventObject event = factory.createCamelContextStartupFailureEvent(context, cause);
if (event == null) {
return;
}
doNotifyEvent(notifier, event);
}
}
Aggregations