use of org.apache.camel.spi.EventNotifier in project camel by apache.
the class MainSupport method postProcessCamelContext.
protected void postProcessCamelContext(CamelContext camelContext) throws Exception {
if (trace) {
camelContext.setTracing(true);
}
if (fileWatchDirectory != null) {
ReloadStrategy reload = new FileWatcherReloadStrategy(fileWatchDirectory);
camelContext.setReloadStrategy(reload);
// ensure reload is added as service and started
camelContext.addService(reload);
// and ensure its register in JMX (which requires manually to be added because CamelContext is already started)
Object managedObject = camelContext.getManagementStrategy().getManagementObjectStrategy().getManagedObjectForService(camelContext, reload);
if (managedObject == null) {
// service should not be managed
return;
}
// skip already managed services, for example if a route has been restarted
if (camelContext.getManagementStrategy().isManaged(managedObject, null)) {
LOG.trace("The service is already managed: {}", reload);
return;
}
try {
camelContext.getManagementStrategy().manageObject(managedObject);
} catch (Exception e) {
LOG.warn("Could not register service: " + reload + " as Service MBean.", e);
}
}
if (durationMaxMessages > 0 || durationIdle > 0) {
// convert to seconds as that is what event notifier uses
long seconds = timeUnit.toSeconds(durationIdle);
// register lifecycle so we can trigger to shutdown the JVM when maximum number of messages has been processed
EventNotifier notifier = new MainDurationEventNotifier(camelContext, durationMaxMessages, seconds, completed, latch, true);
// register our event notifier
ServiceHelper.startService(notifier);
camelContext.getManagementStrategy().addEventNotifier(notifier);
}
// try to load the route builders from the routeBuilderClasses
loadRouteBuilders(camelContext);
for (RouteBuilder routeBuilder : routeBuilders) {
camelContext.addRoutes(routeBuilder);
}
// register lifecycle so we are notified in Camel is stopped from JMX or somewhere else
camelContext.addLifecycleStrategy(new MainLifecycleStrategy(completed, latch));
// allow to do configuration before its started
for (MainListener listener : listeners) {
listener.configure(camelContext);
}
}
use of org.apache.camel.spi.EventNotifier in project camel by apache.
the class DefaultCamelContext method doStop.
protected synchronized void doStop() throws Exception {
stopWatch.restart();
log.info("Apache Camel " + getVersion() + " (CamelContext: " + getName() + ") is shutting down");
EventHelper.notifyCamelContextStopping(this);
// stop route inputs in the same order as they was started so we stop the very first inputs first
try {
// force shutting down routes as they may otherwise cause shutdown to hang
shutdownStrategy.shutdownForced(this, getRouteStartupOrder());
} catch (Throwable e) {
log.warn("Error occurred while shutting down routes. This exception will be ignored.", e);
}
// shutdown await manager to trigger interrupt of blocked threads to attempt to free these threads graceful
shutdownServices(asyncProcessorAwaitManager);
shutdownServices(getRouteStartupOrder().stream().sorted(Comparator.comparing(RouteStartupOrder::getStartupOrder).reversed()).map(DefaultRouteStartupOrder.class::cast).map(DefaultRouteStartupOrder::getRouteService).collect(Collectors.toList()), false);
// do not clear route services or startup listeners as we can start Camel again and get the route back as before
getRouteStartupOrder().clear();
// but clear any suspend routes
suspendedRouteServices.clear();
// which we need to stop after the routes, as a POJO consumer is essentially a route also
for (Service service : servicesToStop) {
if (service instanceof Consumer) {
shutdownServices(service);
}
}
// shutdown default error handler thread pool
if (errorHandlerExecutorService != null) {
// force shutting down the thread pool
getExecutorServiceManager().shutdownNow(errorHandlerExecutorService);
errorHandlerExecutorService = null;
}
// shutdown debugger
ServiceHelper.stopAndShutdownService(getDebugger());
shutdownServices(endpoints.values());
endpoints.clear();
shutdownServices(components.values());
components.clear();
shutdownServices(languages.values());
languages.clear();
try {
for (LifecycleStrategy strategy : lifecycleStrategies) {
strategy.onContextStop(this);
}
} catch (Throwable e) {
log.warn("Error occurred while stopping lifecycle strategies. This exception will be ignored.", e);
}
// shutdown services as late as possible
shutdownServices(servicesToStop);
servicesToStop.clear();
// must notify that we are stopped before stopping the management strategy
EventHelper.notifyCamelContextStopped(this);
// stop the notifier service
for (EventNotifier notifier : getManagementStrategy().getEventNotifiers()) {
shutdownServices(notifier);
}
// shutdown executor service and management as the last one
shutdownServices(executorServiceManager);
shutdownServices(managementStrategy);
shutdownServices(managementMBeanAssembler);
shutdownServices(lifecycleStrategies);
// do not clear lifecycleStrategies as we can start Camel again and get the route back as before
// stop the lazy created so they can be re-created on restart
forceStopLazyInitialization();
// stop to clear introspection cache
IntrospectionSupport.stop();
stopWatch.stop();
if (log.isInfoEnabled()) {
log.info("Apache Camel " + getVersion() + " (CamelContext: " + getName() + ") uptime {}", getUptime());
log.info("Apache Camel " + getVersion() + " (CamelContext: " + getName() + ") is shutdown in " + TimeUtils.printDuration(stopWatch.taken()));
}
// and clear start date
startDate = null;
// [TODO] Remove in 3.0
Container.Instance.unmanage(this);
}
use of org.apache.camel.spi.EventNotifier in project camel by apache.
the class EventHelper method notifyCamelContextSuspending.
public static void notifyCamelContextSuspending(CamelContext context) {
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.createCamelContextSuspendingEvent(context);
if (event == null) {
return;
}
doNotifyEvent(notifier, event);
}
}
use of org.apache.camel.spi.EventNotifier in project camel by apache.
the class EventHelper method notifyRouteStarted.
public static void notifyRouteStarted(CamelContext context, Route route) {
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.isIgnoreRouteEvents()) {
continue;
}
EventFactory factory = management.getEventFactory();
if (factory == null) {
return;
}
EventObject event = factory.createRouteStartedEvent(route);
if (event == null) {
return;
}
doNotifyEvent(notifier, event);
}
}
use of org.apache.camel.spi.EventNotifier in project camel by apache.
the class EventHelper method notifyExchangeSending.
public static void notifyExchangeSending(CamelContext context, Exchange exchange, Endpoint endpoint) {
if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) {
// do not generate events for an notify event
return;
}
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.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeSentEvents()) {
continue;
}
EventFactory factory = management.getEventFactory();
if (factory == null) {
return;
}
EventObject event = factory.createExchangeSendingEvent(exchange, endpoint);
if (event == null) {
return;
}
doNotifyEvent(notifier, event);
}
}
Aggregations