Search in sources :

Example 1 with ReloadStrategy

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

the class CamelAutoConfiguration method camelContext.

/**
     * Spring-aware Camel context for the application. Auto-detects and loads all routes available in the Spring context.
     */
@Bean
@ConditionalOnMissingBean(CamelContext.class)
CamelContext camelContext(ApplicationContext applicationContext, CamelConfigurationProperties config) {
    if (ObjectHelper.isNotEmpty(config.getFileConfigurations())) {
        Environment env = applicationContext.getEnvironment();
        if (env instanceof ConfigurableEnvironment) {
            MutablePropertySources sources = ((ConfigurableEnvironment) env).getPropertySources();
            if (sources != null) {
                if (!sources.contains("camel-file-configuration")) {
                    sources.addFirst(new FilePropertySource("camel-file-configuration", applicationContext, config.getFileConfigurations()));
                }
            }
        }
    }
    CamelContext camelContext = new SpringCamelContext(applicationContext);
    SpringCamelContext.setNoStart(true);
    if (!config.isJmxEnabled()) {
        camelContext.disableJMX();
    }
    if (config.getName() != null) {
        ((SpringCamelContext) camelContext).setName(config.getName());
    }
    if (config.getShutdownTimeout() > 0) {
        camelContext.getShutdownStrategy().setTimeout(config.getShutdownTimeout());
    }
    camelContext.getShutdownStrategy().setSuppressLoggingOnTimeout(config.isShutdownSuppressLoggingOnTimeout());
    camelContext.getShutdownStrategy().setShutdownNowOnTimeout(config.isShutdownNowOnTimeout());
    camelContext.getShutdownStrategy().setShutdownRoutesInReverseOrder(config.isShutdownRoutesInReverseOrder());
    camelContext.getShutdownStrategy().setLogInflightExchangesOnTimeout(config.isShutdownLogInflightExchangesOnTimeout());
    if (config.getLogDebugMaxChars() > 0) {
        camelContext.getGlobalOptions().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "" + config.getLogDebugMaxChars());
    }
    // stream caching
    camelContext.setStreamCaching(config.isStreamCachingEnabled());
    camelContext.getStreamCachingStrategy().setAnySpoolRules(config.isStreamCachingAnySpoolRules());
    camelContext.getStreamCachingStrategy().setBufferSize(config.getStreamCachingBufferSize());
    camelContext.getStreamCachingStrategy().setRemoveSpoolDirectoryWhenStopping(config.isStreamCachingRemoveSpoolDirectoryWhenStopping());
    camelContext.getStreamCachingStrategy().setSpoolChiper(config.getStreamCachingSpoolChiper());
    if (config.getStreamCachingSpoolDirectory() != null) {
        camelContext.getStreamCachingStrategy().setSpoolDirectory(config.getStreamCachingSpoolDirectory());
    }
    if (config.getStreamCachingSpoolThreshold() != 0) {
        camelContext.getStreamCachingStrategy().setSpoolThreshold(config.getStreamCachingSpoolThreshold());
    }
    if (config.getStreamCachingSpoolUsedHeapMemoryLimit() != null) {
        StreamCachingStrategy.SpoolUsedHeapMemoryLimit limit;
        if ("Committed".equalsIgnoreCase(config.getStreamCachingSpoolUsedHeapMemoryLimit())) {
            limit = StreamCachingStrategy.SpoolUsedHeapMemoryLimit.Committed;
        } else if ("Max".equalsIgnoreCase(config.getStreamCachingSpoolUsedHeapMemoryLimit())) {
            limit = StreamCachingStrategy.SpoolUsedHeapMemoryLimit.Max;
        } else {
            throw new IllegalArgumentException("Invalid option " + config.getStreamCachingSpoolUsedHeapMemoryLimit() + " must either be Committed or Max");
        }
        camelContext.getStreamCachingStrategy().setSpoolUsedHeapMemoryLimit(limit);
    }
    if (config.getStreamCachingSpoolUsedHeapMemoryThreshold() != 0) {
        camelContext.getStreamCachingStrategy().setSpoolUsedHeapMemoryThreshold(config.getStreamCachingSpoolUsedHeapMemoryThreshold());
    }
    camelContext.setMessageHistory(config.isMessageHistory());
    camelContext.setLogExhaustedMessageBody(config.isLogExhaustedMessageBody());
    camelContext.setHandleFault(config.isHandleFault());
    camelContext.setAutoStartup(config.isAutoStartup());
    camelContext.setAllowUseOriginalMessage(config.isAllowUseOriginalMessage());
    if (camelContext.getManagementStrategy().getManagementAgent() != null) {
        camelContext.getManagementStrategy().getManagementAgent().setEndpointRuntimeStatisticsEnabled(config.isEndpointRuntimeStatisticsEnabled());
        camelContext.getManagementStrategy().getManagementAgent().setStatisticsLevel(config.getJmxManagementStatisticsLevel());
        camelContext.getManagementStrategy().getManagementAgent().setManagementNamePattern(config.getJmxManagementNamePattern());
        camelContext.getManagementStrategy().getManagementAgent().setCreateConnector(config.isJmxCreateConnector());
    }
    camelContext.setPackageScanClassResolver(new FatJarPackageScanClassResolver());
    // tracing
    camelContext.setTracing(config.isTracing());
    if (camelContext.getDefaultTracer() instanceof Tracer) {
        Tracer tracer = (Tracer) camelContext.getDefaultTracer();
        if (tracer.getDefaultTraceFormatter() != null) {
            DefaultTraceFormatter formatter = tracer.getDefaultTraceFormatter();
            if (config.getTracerFormatterBreadCrumbLength() != null) {
                formatter.setBreadCrumbLength(config.getTracerFormatterBreadCrumbLength());
            }
            if (config.getTracerFormatterMaxChars() != null) {
                formatter.setMaxChars(config.getTracerFormatterMaxChars());
            }
            if (config.getTracerFormatterNodeLength() != null) {
                formatter.setNodeLength(config.getTracerFormatterNodeLength());
            }
            formatter.setShowBody(config.isTraceFormatterShowBody());
            formatter.setShowBodyType(config.isTracerFormatterShowBodyType());
            formatter.setShowBreadCrumb(config.isTraceFormatterShowBreadCrumb());
            formatter.setShowException(config.isTraceFormatterShowException());
            formatter.setShowExchangeId(config.isTraceFormatterShowExchangeId());
            formatter.setShowExchangePattern(config.isTraceFormatterShowExchangePattern());
            formatter.setShowHeaders(config.isTraceFormatterShowHeaders());
            formatter.setShowNode(config.isTraceFormatterShowNode());
            formatter.setShowProperties(config.isTraceFormatterShowProperties());
            formatter.setShowRouteId(config.isTraceFormatterShowRouteId());
            formatter.setShowShortExchangeId(config.isTraceFormatterShowShortExchangeId());
        }
    }
    if (config.getXmlRoutesReloadDirectory() != null) {
        ReloadStrategy reload = new FileWatcherReloadStrategy(config.getXmlRoutesReloadDirectory());
        camelContext.setReloadStrategy(reload);
    }
    // additional advanced configuration which is not configured using CamelConfigurationProperties
    afterPropertiesSet(applicationContext, camelContext);
    return camelContext;
}
Also used : SpringCamelContext(org.apache.camel.spring.SpringCamelContext) CamelContext(org.apache.camel.CamelContext) DefaultTraceFormatter(org.apache.camel.processor.interceptor.DefaultTraceFormatter) FileWatcherReloadStrategy(org.apache.camel.impl.FileWatcherReloadStrategy) Tracer(org.apache.camel.processor.interceptor.Tracer) BacklogTracer(org.apache.camel.processor.interceptor.BacklogTracer) SpringCamelContext(org.apache.camel.spring.SpringCamelContext) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Environment(org.springframework.core.env.Environment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) StreamCachingStrategy(org.apache.camel.spi.StreamCachingStrategy) ReloadStrategy(org.apache.camel.spi.ReloadStrategy) FileWatcherReloadStrategy(org.apache.camel.impl.FileWatcherReloadStrategy) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with ReloadStrategy

use of org.apache.camel.spi.ReloadStrategy 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)

Aggregations

FileWatcherReloadStrategy (org.apache.camel.impl.FileWatcherReloadStrategy)2 ReloadStrategy (org.apache.camel.spi.ReloadStrategy)2 CamelContext (org.apache.camel.CamelContext)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 BacklogTracer (org.apache.camel.processor.interceptor.BacklogTracer)1 DefaultTraceFormatter (org.apache.camel.processor.interceptor.DefaultTraceFormatter)1 Tracer (org.apache.camel.processor.interceptor.Tracer)1 EventNotifier (org.apache.camel.spi.EventNotifier)1 StreamCachingStrategy (org.apache.camel.spi.StreamCachingStrategy)1 SpringCamelContext (org.apache.camel.spring.SpringCamelContext)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 Environment (org.springframework.core.env.Environment)1 MutablePropertySources (org.springframework.core.env.MutablePropertySources)1