Search in sources :

Example 31 with EventNotifier

use of org.apache.camel.spi.EventNotifier in project camel by apache.

the class EventHelper method notifyCamelContextStopped.

public static void notifyCamelContextStopped(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.createCamelContextStoppedEvent(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 32 with EventNotifier

use of org.apache.camel.spi.EventNotifier in project camel by apache.

the class EventHelper method notifyCamelContextStopFailed.

public static void notifyCamelContextStopFailed(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.createCamelContextStopFailureEvent(context, cause);
        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 33 with EventNotifier

use of org.apache.camel.spi.EventNotifier in project camel by apache.

the class BlueprintCamelContext method maybeStart.

private void maybeStart() throws Exception {
    LOG.trace("maybeStart: {}", this);
    if (!routeDefinitionValid.get()) {
        LOG.trace("maybeStart: {} is skipping since CamelRoute definition is not correct.", this);
        return;
    }
    // allow to register the BluerintCamelContext eager in the OSGi Service Registry, which ex is needed
    // for unit testing with camel-test-blueprint
    boolean eager = "true".equalsIgnoreCase(System.getProperty("registerBlueprintCamelContextEager"));
    if (eager) {
        for (EventNotifier notifier : getManagementStrategy().getEventNotifiers()) {
            if (notifier instanceof OsgiCamelContextPublisher) {
                OsgiCamelContextPublisher publisher = (OsgiCamelContextPublisher) notifier;
                publisher.registerCamelContext(this);
                break;
            }
        }
    }
    // for example from unit testing we want to start Camel later and not
    // when blueprint loading the bundle
    boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
    if (skip) {
        LOG.trace("maybeStart: {} is skipping as System property skipStartingCamelContext is set", this);
        return;
    }
    if (!isStarted() && !isStarting()) {
        LOG.debug("Starting {}", this);
        start();
    } else {
        // ignore as Camel is already started
        LOG.trace("Ignoring maybeStart() as {} is already started", this);
    }
}
Also used : OsgiCamelContextPublisher(org.apache.camel.core.osgi.OsgiCamelContextPublisher) EventNotifier(org.apache.camel.spi.EventNotifier)

Example 34 with EventNotifier

use of org.apache.camel.spi.EventNotifier in project camel by apache.

the class AbstractCamelContextFactoryBean method afterPropertiesSet.

public void afterPropertiesSet() throws Exception {
    if (ObjectHelper.isEmpty(getId())) {
        throw new IllegalArgumentException("Id must be set");
    }
    // set the package scan resolver as soon as possible
    PackageScanClassResolver packageResolver = getBeanForType(PackageScanClassResolver.class);
    if (packageResolver != null) {
        LOG.info("Using custom PackageScanClassResolver: {}", packageResolver);
        getContext().setPackageScanClassResolver(packageResolver);
    }
    // then set custom properties
    Map<String, String> mergedOptions = new HashMap<>();
    if (getProperties() != null) {
        mergedOptions.putAll(getProperties().asMap());
    }
    if (getGlobalOptions() != null) {
        mergedOptions.putAll(getGlobalOptions().asMap());
    }
    getContext().setGlobalOptions(mergedOptions);
    // and enable lazy loading of type converters if applicable
    initLazyLoadTypeConverters();
    setupCustomServices();
    // set the custom registry if defined
    initCustomRegistry(getContext());
    // setup property placeholder so we got it as early as possible
    initPropertyPlaceholder();
    // setup JMX agent at first
    initJMXAgent();
    Tracer tracer = getBeanForType(Tracer.class);
    if (tracer != null) {
        // use formatter if there is a TraceFormatter bean defined
        TraceFormatter formatter = getBeanForType(TraceFormatter.class);
        if (formatter != null) {
            tracer.setFormatter(formatter);
        }
        LOG.info("Using custom Tracer: {}", tracer);
        getContext().addInterceptStrategy(tracer);
    }
    BacklogTracer backlogTracer = getBeanForType(BacklogTracer.class);
    if (backlogTracer != null) {
        LOG.info("Using custom BacklogTracer: {}", backlogTracer);
        getContext().addInterceptStrategy(backlogTracer);
    }
    HandleFault handleFault = getBeanForType(HandleFault.class);
    if (handleFault != null) {
        LOG.info("Using custom HandleFault: {}", handleFault);
        getContext().addInterceptStrategy(handleFault);
    }
    @SuppressWarnings("deprecation") org.apache.camel.processor.interceptor.Delayer delayer = getBeanForType(org.apache.camel.processor.interceptor.Delayer.class);
    if (delayer != null) {
        LOG.info("Using custom Delayer: {}", delayer);
        getContext().addInterceptStrategy(delayer);
    }
    InflightRepository inflightRepository = getBeanForType(InflightRepository.class);
    if (inflightRepository != null) {
        LOG.info("Using custom InflightRepository: {}", inflightRepository);
        getContext().setInflightRepository(inflightRepository);
    }
    AsyncProcessorAwaitManager asyncProcessorAwaitManager = getBeanForType(AsyncProcessorAwaitManager.class);
    if (asyncProcessorAwaitManager != null) {
        LOG.info("Using custom AsyncProcessorAwaitManager: {}", asyncProcessorAwaitManager);
        getContext().setAsyncProcessorAwaitManager(asyncProcessorAwaitManager);
    }
    ManagementStrategy managementStrategy = getBeanForType(ManagementStrategy.class);
    if (managementStrategy != null) {
        LOG.info("Using custom ManagementStrategy: {}", managementStrategy);
        getContext().setManagementStrategy(managementStrategy);
    }
    ManagementNamingStrategy managementNamingStrategy = getBeanForType(ManagementNamingStrategy.class);
    if (managementNamingStrategy != null) {
        LOG.info("Using custom ManagementNamingStrategy: {}", managementNamingStrategy);
        getContext().getManagementStrategy().setManagementNamingStrategy(managementNamingStrategy);
    }
    EventFactory eventFactory = getBeanForType(EventFactory.class);
    if (eventFactory != null) {
        LOG.info("Using custom EventFactory: {}", eventFactory);
        getContext().getManagementStrategy().setEventFactory(eventFactory);
    }
    UnitOfWorkFactory unitOfWorkFactory = getBeanForType(UnitOfWorkFactory.class);
    if (unitOfWorkFactory != null) {
        LOG.info("Using custom UnitOfWorkFactory: {}", unitOfWorkFactory);
        getContext().setUnitOfWorkFactory(unitOfWorkFactory);
    }
    RuntimeEndpointRegistry runtimeEndpointRegistry = getBeanForType(RuntimeEndpointRegistry.class);
    if (runtimeEndpointRegistry != null) {
        LOG.info("Using custom RuntimeEndpointRegistry: {}", runtimeEndpointRegistry);
        getContext().setRuntimeEndpointRegistry(runtimeEndpointRegistry);
    }
    // custom type converters defined as <bean>s
    Map<String, TypeConverters> typeConverters = getContext().getRegistry().findByTypeWithName(TypeConverters.class);
    if (typeConverters != null && !typeConverters.isEmpty()) {
        for (Entry<String, TypeConverters> entry : typeConverters.entrySet()) {
            TypeConverters converter = entry.getValue();
            LOG.info("Adding custom TypeConverters with id: {} and implementation: {}", entry.getKey(), converter);
            getContext().getTypeConverterRegistry().addTypeConverters(converter);
        }
    }
    // set the event notifier strategies if defined
    Map<String, EventNotifier> eventNotifiers = getContext().getRegistry().findByTypeWithName(EventNotifier.class);
    if (eventNotifiers != null && !eventNotifiers.isEmpty()) {
        for (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 (!getContext().getManagementStrategy().getEventNotifiers().contains(notifier)) {
                LOG.info("Using custom EventNotifier with id: {} and implementation: {}", entry.getKey(), notifier);
                getContext().getManagementStrategy().addEventNotifier(notifier);
            }
        }
    }
    // set endpoint strategies if defined
    Map<String, EndpointStrategy> endpointStrategies = getContext().getRegistry().findByTypeWithName(EndpointStrategy.class);
    if (endpointStrategies != null && !endpointStrategies.isEmpty()) {
        for (Entry<String, EndpointStrategy> entry : endpointStrategies.entrySet()) {
            EndpointStrategy strategy = entry.getValue();
            LOG.info("Using custom EndpointStrategy with id: {} and implementation: {}", entry.getKey(), strategy);
            getContext().addRegisterEndpointCallback(strategy);
        }
    }
    // shutdown
    ShutdownStrategy shutdownStrategy = getBeanForType(ShutdownStrategy.class);
    if (shutdownStrategy != null) {
        LOG.info("Using custom ShutdownStrategy: " + shutdownStrategy);
        getContext().setShutdownStrategy(shutdownStrategy);
    }
    // add global interceptors
    Map<String, InterceptStrategy> interceptStrategies = getContext().getRegistry().findByTypeWithName(InterceptStrategy.class);
    if (interceptStrategies != null && !interceptStrategies.isEmpty()) {
        for (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 (!getContext().getInterceptStrategies().contains(strategy)) {
                LOG.info("Using custom InterceptStrategy with id: {} and implementation: {}", entry.getKey(), strategy);
                getContext().addInterceptStrategy(strategy);
            }
        }
    }
    // set the lifecycle strategy if defined
    Map<String, LifecycleStrategy> lifecycleStrategies = getContext().getRegistry().findByTypeWithName(LifecycleStrategy.class);
    if (lifecycleStrategies != null && !lifecycleStrategies.isEmpty()) {
        for (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 (!getContext().getLifecycleStrategies().contains(strategy)) {
                LOG.info("Using custom LifecycleStrategy with id: {} and implementation: {}", entry.getKey(), strategy);
                getContext().addLifecycleStrategy(strategy);
            }
        }
    }
    // add route policy factories
    Map<String, RoutePolicyFactory> routePolicyFactories = getContext().getRegistry().findByTypeWithName(RoutePolicyFactory.class);
    if (routePolicyFactories != null && !routePolicyFactories.isEmpty()) {
        for (Entry<String, RoutePolicyFactory> entry : routePolicyFactories.entrySet()) {
            RoutePolicyFactory factory = entry.getValue();
            LOG.info("Using custom RoutePolicyFactory with id: {} and implementation: {}", entry.getKey(), factory);
            getContext().addRoutePolicyFactory(factory);
        }
    }
    // set the default thread pool profile if defined
    initThreadPoolProfiles(getContext());
    // Set the application context and camelContext for the beanPostProcessor
    initBeanPostProcessor(getContext());
    // init camel context
    initCamelContext(getContext());
    // init stream caching strategy
    initStreamCachingStrategy();
}
Also used : HashMap(java.util.HashMap) InflightRepository(org.apache.camel.spi.InflightRepository) UnitOfWorkFactory(org.apache.camel.spi.UnitOfWorkFactory) TypeConverters(org.apache.camel.TypeConverters) EndpointStrategy(org.apache.camel.spi.EndpointStrategy) EventNotifier(org.apache.camel.spi.EventNotifier) RoutePolicyFactory(org.apache.camel.spi.RoutePolicyFactory) TraceFormatter(org.apache.camel.processor.interceptor.TraceFormatter) BacklogTracer(org.apache.camel.processor.interceptor.BacklogTracer) ManagedManagementStrategy(org.apache.camel.management.ManagedManagementStrategy) DefaultManagementStrategy(org.apache.camel.management.DefaultManagementStrategy) ManagementStrategy(org.apache.camel.spi.ManagementStrategy) ShutdownStrategy(org.apache.camel.spi.ShutdownStrategy) Tracer(org.apache.camel.processor.interceptor.Tracer) BacklogTracer(org.apache.camel.processor.interceptor.BacklogTracer) EventFactory(org.apache.camel.spi.EventFactory) AsyncProcessorAwaitManager(org.apache.camel.spi.AsyncProcessorAwaitManager) InterceptStrategy(org.apache.camel.spi.InterceptStrategy) PackageScanClassResolver(org.apache.camel.spi.PackageScanClassResolver) ManagementNamingStrategy(org.apache.camel.spi.ManagementNamingStrategy) DefaultManagementLifecycleStrategy(org.apache.camel.management.DefaultManagementLifecycleStrategy) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) RuntimeEndpointRegistry(org.apache.camel.spi.RuntimeEndpointRegistry) HandleFault(org.apache.camel.processor.interceptor.HandleFault)

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