Search in sources :

Example 1 with EventNotifier

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);
    }
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) FileWatcherReloadStrategy(org.apache.camel.impl.FileWatcherReloadStrategy) EventNotifier(org.apache.camel.spi.EventNotifier) ReloadStrategy(org.apache.camel.spi.ReloadStrategy) FileWatcherReloadStrategy(org.apache.camel.impl.FileWatcherReloadStrategy)

Example 2 with EventNotifier

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);
}
Also used : PollingConsumer(org.apache.camel.PollingConsumer) Consumer(org.apache.camel.Consumer) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) EventNotifier(org.apache.camel.spi.EventNotifier) Service(org.apache.camel.Service) StatefulService(org.apache.camel.StatefulService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SuspendableService(org.apache.camel.SuspendableService) RouteStartupOrder(org.apache.camel.spi.RouteStartupOrder)

Example 3 with EventNotifier

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);
    }
}
Also used : ManagementStrategy(org.apache.camel.spi.ManagementStrategy) EventNotifier(org.apache.camel.spi.EventNotifier) EventFactory(org.apache.camel.spi.EventFactory) EventObject(java.util.EventObject)

Example 4 with EventNotifier

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);
    }
}
Also used : ManagementStrategy(org.apache.camel.spi.ManagementStrategy) EventNotifier(org.apache.camel.spi.EventNotifier) EventFactory(org.apache.camel.spi.EventFactory) EventObject(java.util.EventObject)

Example 5 with EventNotifier

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);
    }
}
Also used : ManagementStrategy(org.apache.camel.spi.ManagementStrategy) EventNotifier(org.apache.camel.spi.EventNotifier) EventFactory(org.apache.camel.spi.EventFactory) EventObject(java.util.EventObject)

Aggregations

EventNotifier (org.apache.camel.spi.EventNotifier)34 ManagementStrategy (org.apache.camel.spi.ManagementStrategy)28 EventFactory (org.apache.camel.spi.EventFactory)27 EventObject (java.util.EventObject)25 LifecycleStrategy (org.apache.camel.spi.LifecycleStrategy)4 BacklogTracer (org.apache.camel.processor.interceptor.BacklogTracer)3 HandleFault (org.apache.camel.processor.interceptor.HandleFault)3 Tracer (org.apache.camel.processor.interceptor.Tracer)3 AsyncProcessorAwaitManager (org.apache.camel.spi.AsyncProcessorAwaitManager)3 InflightRepository (org.apache.camel.spi.InflightRepository)3 RuntimeEndpointRegistry (org.apache.camel.spi.RuntimeEndpointRegistry)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CamelContextAware (org.apache.camel.CamelContextAware)2 Consumer (org.apache.camel.Consumer)2 Service (org.apache.camel.Service)2 StatefulService (org.apache.camel.StatefulService)2 SuspendableService (org.apache.camel.SuspendableService)2