Search in sources :

Example 16 with ManagementStrategy

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

the class CamelServletContextListener method initJmx.

/**
     * Initializes JMX on {@link ServletCamelContext} with the configuration from the given init parameters.
     */
private void initJmx(ServletCamelContext camelContext, Map<String, Object> parameters) throws Exception {
    // setup jmx
    Map<String, Object> properties = IntrospectionSupport.extractProperties(parameters, "jmx.");
    if (properties != null && !properties.isEmpty()) {
        String disabled = (String) properties.remove("disabled");
        boolean disableJmx = CamelContextHelper.parseBoolean(camelContext, disabled != null ? disabled : "false");
        if (disableJmx) {
            // disable JMX which is a bit special to do
            LOG.info("JMXAgent disabled");
            // clear the existing lifecycle strategies define by the DefaultCamelContext constructor
            camelContext.getLifecycleStrategies().clear();
            // no need to add a lifecycle strategy as we do not need one as JMX is disabled
            camelContext.setManagementStrategy(new DefaultManagementStrategy());
        } else {
            LOG.info("JMXAgent enabled");
            DefaultManagementAgent agent = new DefaultManagementAgent(camelContext);
            IntrospectionSupport.setProperties(agent, properties);
            ManagementStrategy managementStrategy = new ManagedManagementStrategy(camelContext, agent);
            camelContext.setManagementStrategy(managementStrategy);
            // clear the existing lifecycle strategies defined by the DefaultCamelContext constructor
            camelContext.getLifecycleStrategies().clear();
            camelContext.addLifecycleStrategy(new DefaultManagementLifecycleStrategy(camelContext));
            // set additional configuration from agent
            boolean onlyId = agent.getOnlyRegisterProcessorWithCustomId() != null && agent.getOnlyRegisterProcessorWithCustomId();
            camelContext.getManagementStrategy().onlyManageProcessorWithCustomId(onlyId);
            String statisticsLevel = (String) properties.remove("statisticsLevel");
            if (statisticsLevel != null) {
                camelContext.getManagementStrategy().setStatisticsLevel(ManagementStatisticsLevel.valueOf(statisticsLevel));
            }
            String loadStatisticsEnabled = (String) properties.remove("loadStatisticsEnabled");
            Boolean statisticsEnabled = CamelContextHelper.parseBoolean(camelContext, loadStatisticsEnabled != null ? loadStatisticsEnabled : "true");
            if (statisticsEnabled != null) {
                camelContext.getManagementStrategy().setLoadStatisticsEnabled(statisticsEnabled);
            }
        }
        // validate we could set all parameters
        if (!properties.isEmpty()) {
            throw new IllegalArgumentException("Error setting jmx parameters on CamelContext." + " There are " + properties.size() + " unknown parameters. [" + properties + "]");
        }
    }
}
Also used : DefaultManagementStrategy(org.apache.camel.management.DefaultManagementStrategy) ManagementStrategy(org.apache.camel.spi.ManagementStrategy) ManagedManagementStrategy(org.apache.camel.management.ManagedManagementStrategy) ManagedManagementStrategy(org.apache.camel.management.ManagedManagementStrategy) DefaultManagementLifecycleStrategy(org.apache.camel.management.DefaultManagementLifecycleStrategy) DefaultManagementAgent(org.apache.camel.management.DefaultManagementAgent) DefaultManagementStrategy(org.apache.camel.management.DefaultManagementStrategy)

Example 17 with ManagementStrategy

use of org.apache.camel.spi.ManagementStrategy 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);
}
Also used : 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) DefaultTraceFormatter(org.apache.camel.processor.interceptor.DefaultTraceFormatter) BacklogTracer(org.apache.camel.processor.interceptor.BacklogTracer) 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) ManagementNamingStrategy(org.apache.camel.spi.ManagementNamingStrategy) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) RuntimeEndpointRegistry(org.apache.camel.spi.RuntimeEndpointRegistry) HandleFault(org.apache.camel.processor.interceptor.HandleFault) Map(java.util.Map)

Example 18 with ManagementStrategy

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

the class ManagementStrategyFactory method create.

public ManagementStrategy create(CamelContext context, boolean disableJMX) {
    ManagementStrategy answer;
    if (disableJMX || Boolean.getBoolean(JmxSystemPropertyKeys.DISABLED)) {
        answer = new DefaultManagementStrategy(context);
    } else {
        try {
            answer = new ManagedManagementStrategy(context, new DefaultManagementAgent(context));
            // must add management lifecycle strategy
            context.getLifecycleStrategies().add(0, new DefaultManagementLifecycleStrategy(context));
        } catch (Exception e) {
            log.warn("Cannot create JMX lifecycle strategy. Will fallback and disable JMX.", e);
            answer = new DefaultManagementStrategy(context);
        }
    }
    return answer;
}
Also used : ManagementStrategy(org.apache.camel.spi.ManagementStrategy)

Example 19 with ManagementStrategy

use of org.apache.camel.spi.ManagementStrategy 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);
    }
}
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 20 with ManagementStrategy

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

the class EventHelper method notifyCamelContextStarted.

public static void notifyCamelContextStarted(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.createCamelContextStartedEvent(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)

Aggregations

ManagementStrategy (org.apache.camel.spi.ManagementStrategy)33 EventNotifier (org.apache.camel.spi.EventNotifier)28 EventFactory (org.apache.camel.spi.EventFactory)27 EventObject (java.util.EventObject)25 DefaultManagementStrategy (org.apache.camel.management.DefaultManagementStrategy)5 DefaultManagementLifecycleStrategy (org.apache.camel.management.DefaultManagementLifecycleStrategy)3 ManagedManagementStrategy (org.apache.camel.management.ManagedManagementStrategy)3 HandleFault (org.apache.camel.processor.interceptor.HandleFault)3 LifecycleStrategy (org.apache.camel.spi.LifecycleStrategy)3 ShutdownStrategy (org.apache.camel.spi.ShutdownStrategy)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TypeConverters (org.apache.camel.TypeConverters)2 DefaultManagementAgent (org.apache.camel.management.DefaultManagementAgent)2 BacklogTracer (org.apache.camel.processor.interceptor.BacklogTracer)2 TraceFormatter (org.apache.camel.processor.interceptor.TraceFormatter)2 Tracer (org.apache.camel.processor.interceptor.Tracer)2 AsyncProcessorAwaitManager (org.apache.camel.spi.AsyncProcessorAwaitManager)2 EndpointStrategy (org.apache.camel.spi.EndpointStrategy)2 InflightRepository (org.apache.camel.spi.InflightRepository)2