This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 555550de499354ae166f66bbb620dd99bfd31969 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue May 14 06:21:12 2019 +0200 CAMEL-13503: Camel main - Allow to configure global and common options ala camel-spring-boot have. --- .../camel/main/MainConfigurationProperties.java | 144 +++++++++++++++++++++ .../java/org/apache/camel/main/MainSupport.java | 87 +++++++------ 2 files changed, 195 insertions(+), 36 deletions(-) diff --git a/core/camel-core/src/main/java/org/apache/camel/main/MainConfigurationProperties.java b/core/camel-core/src/main/java/org/apache/camel/main/MainConfigurationProperties.java index 7c1b54d..56cb2c8 100644 --- a/core/camel-core/src/main/java/org/apache/camel/main/MainConfigurationProperties.java +++ b/core/camel-core/src/main/java/org/apache/camel/main/MainConfigurationProperties.java @@ -17,12 +17,14 @@ package org.apache.camel.main; import org.apache.camel.ManagementStatisticsLevel; +import org.apache.camel.spi.ReloadStrategy; /** * Global configuration for Camel Main to setup context name, stream caching and other global configurations. */ public class MainConfigurationProperties { + private boolean autoConfigurationEnabled = true; private String name; private int shutdownTimeout = 300; private boolean shutdownSuppressLoggingOnTimeout; @@ -33,9 +35,12 @@ public class MainConfigurationProperties { private int producerTemplateCacheSize = 1000; private int consumerTemplateCacheSize = 1000; private String fileConfigurations; + private long duration = -1; private int durationMaxSeconds; private int durationMaxIdleSeconds; private int durationMaxMessages; + private boolean hangupInterceptorEnabled = true; + private int durationHitExitCode; private int logDebugMaxChars; private boolean streamCachingEnabled; private String streamCachingSpoolDirectory; @@ -62,10 +67,41 @@ public class MainConfigurationProperties { private boolean jmxCreateConnector; private boolean useMdcLogging; private String threadNamePattern; + private String fileWatchDirectory; + private boolean fileWatchDirectoryRecursively; + private ReloadStrategy reloadStrategy; // getter and setters // -------------------------------------------------------------- + public boolean isAutoConfigurationEnabled() { + return autoConfigurationEnabled; + } + + /** + * Whether auto configuration of components/dataformats/languages is enabled or not. + * When enabled the configuration parameters are loaded from the properties component + * and configured as defaults (similar to spring-boot auto-configuration). You can prefix + * the parameters in the properties file with: + * - camel.component.name.option1=value1 + * - camel.component.name.option2=value2 + * - camel.dataformat.name.option1=value1 + * - camel.dataformat.name.option2=value2 + * - camel.language.name.option1=value1 + * - camel.language.name.option2=value2 + * Where name is the name of the component, dataformat or language such as seda,direct,jaxb. + * <p/> + * The auto configuration also works for any options on components + * that is a complex type (not standard Java type) and there has been an explicit single + * bean instance registered to the Camel registry via the {@link org.apache.camel.spi.Registry#bind(String, Object)} method + * or by using the {@link org.apache.camel.BindToRegistry} annotation style. + * <p/> + * This option is default enabled. + */ + public void setAutoConfigurationEnabled(boolean autoConfigurationEnabled) { + this.autoConfigurationEnabled = autoConfigurationEnabled; + } + public String getName() { return name; } @@ -574,9 +610,87 @@ public class MainConfigurationProperties { this.threadNamePattern = threadNamePattern; } + public long getDuration() { + return duration; + } + + /** + * Sets the duration (in seconds) to run the application until it + * should be terminated. Defaults to -1. Any value <= 0 will run forever. + */ + public void setDuration(long duration) { + this.duration = duration; + } + + public boolean isHangupInterceptorEnabled() { + return hangupInterceptorEnabled; + } + + /** + * Whether to use graceful hangup when Camel is stopping or when the JVM terminates. + */ + public void setHangupInterceptorEnabled(boolean hangupInterceptorEnabled) { + this.hangupInterceptorEnabled = hangupInterceptorEnabled; + } + + public int getDurationHitExitCode() { + return durationHitExitCode; + } + + /** + * Sets the exit code for the application if duration was hit + */ + public void setDurationHitExitCode(int durationHitExitCode) { + this.durationHitExitCode = durationHitExitCode; + } + + public String getFileWatchDirectory() { + return fileWatchDirectory; + } + + /** + * Sets the directory name to watch XML file changes to trigger live reload of Camel routes. + * <p/> + * Notice you cannot set this value and a custom {@link ReloadStrategy} as well. + */ + public void setFileWatchDirectory(String fileWatchDirectory) { + this.fileWatchDirectory = fileWatchDirectory; + } + + public boolean isFileWatchDirectoryRecursively() { + return fileWatchDirectoryRecursively; + } + + /** + * Sets the flag to watch directory of XML file changes recursively to trigger live reload of Camel routes. + * <p/> + * Notice you cannot set this value and a custom {@link ReloadStrategy} as well. + */ + public void setFileWatchDirectoryRecursively(boolean fileWatchDirectoryRecursively) { + this.fileWatchDirectoryRecursively = fileWatchDirectoryRecursively; + } + + public ReloadStrategy getReloadStrategy() { + return reloadStrategy; + } + + /** + * Sets a custom {@link ReloadStrategy} to be used. + * <p/> + * Notice you cannot set this value and the fileWatchDirectory as well. + */ + public void setReloadStrategy(ReloadStrategy reloadStrategy) { + this.reloadStrategy = reloadStrategy; + } + // fluent builders // -------------------------------------------------------------- + public MainConfigurationProperties withAutoConfigurationEnabled(boolean autoConfigurationEnabled) { + this.autoConfigurationEnabled = autoConfigurationEnabled; + return this; + } + public MainConfigurationProperties withName(String name) { this.name = name; return this; @@ -772,4 +886,34 @@ public class MainConfigurationProperties { return this; } + public MainConfigurationProperties withDuration(long duration) { + this.duration = duration; + return this; + } + + public MainConfigurationProperties withHangupInterceptorEnabled(boolean hangupInterceptorEnabled) { + this.hangupInterceptorEnabled = hangupInterceptorEnabled; + return this; + } + + public MainConfigurationProperties withDurationHitExitCode(int durationHitExitCode) { + this.durationHitExitCode = durationHitExitCode; + return this; + } + + public MainConfigurationProperties withFileWatchDirectory(String fileWatchDirectory) { + this.fileWatchDirectory = fileWatchDirectory; + return this; + } + + public MainConfigurationProperties withFileWatchDirectoryRecursively(boolean fileWatchDirectoryRecursively) { + this.fileWatchDirectoryRecursively = fileWatchDirectoryRecursively; + return this; + } + + public MainConfigurationProperties withReloadStrategy(ReloadStrategy reloadStrategy) { + this.reloadStrategy = reloadStrategy; + return this; + } + } diff --git a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java index 3c0edde..9149e26 100644 --- a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java +++ b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java @@ -110,23 +110,14 @@ public abstract class MainSupport extends ServiceSupport { protected final AtomicBoolean completed = new AtomicBoolean(false); protected final AtomicInteger exitCode = new AtomicInteger(UNINITIALIZED_EXIT_CODE); - // TODO: Move these to mainConfigurationProperties (delegate) - protected long duration = -1; - protected String fileWatchDirectory; - protected boolean fileWatchDirectoryRecursively; - - protected CamelContext camelContext; + protected volatile CamelContext camelContext; + protected volatile ProducerTemplate camelTemplate; protected final MainConfigurationProperties mainConfigurationProperties = new MainConfigurationProperties(); protected List<RouteBuilder> routeBuilders = new ArrayList<>(); protected String routeBuilderClasses; protected List<Object> configurations = new ArrayList<>(); protected String configurationClasses; - protected ProducerTemplate camelTemplate; - protected boolean hangupInterceptorEnabled = true; - protected int durationHitExitCode = DEFAULT_EXIT_CODE; - protected ReloadStrategy reloadStrategy; protected String propertyPlaceholderLocations; - protected boolean autoConfigurationEnabled = true; protected Properties initialProperties; protected Properties overrideProperties; @@ -258,14 +249,14 @@ public abstract class MainSupport extends ServiceSupport { * Hangup signal. */ public void disableHangupSupport() { - hangupInterceptorEnabled = false; + mainConfigurationProperties.setHangupInterceptorEnabled(false); } /** * Hangup support is enabled by default. */ public void enableHangupSupport() { - hangupInterceptorEnabled = true; + mainConfigurationProperties.setHangupInterceptorEnabled(true); } /** @@ -309,7 +300,7 @@ public abstract class MainSupport extends ServiceSupport { } private void internalBeforeStart() { - if (hangupInterceptorEnabled) { + if (mainConfigurationProperties.isHangupInterceptorEnabled()) { String threadName = ThreadHelper.resolveThreadName(null, "CamelHangupInterceptor"); Thread task = new HangupInterceptor(this); @@ -412,18 +403,22 @@ public abstract class MainSupport extends ServiceSupport { return mainConfigurationProperties; } + @Deprecated public long getDuration() { - return duration; + return mainConfigurationProperties.getDuration(); } /** * Sets the duration (in seconds) to run the application until it * should be terminated. Defaults to -1. Any value <= 0 will run forever. + * @deprecated use {@link #configure()} */ + @Deprecated public void setDuration(long duration) { - this.duration = duration; + mainConfigurationProperties.setDuration(duration); } + @Deprecated public int getDurationIdle() { return mainConfigurationProperties.getDurationMaxIdleSeconds(); } @@ -433,11 +428,14 @@ public abstract class MainSupport extends ServiceSupport { * if there has been no message processed after being idle for more than this duration * then the application should be terminated. * Defaults to -1. Any value <= 0 will run forever. + * @deprecated use {@link #configure()} */ + @Deprecated public void setDurationIdle(int durationIdle) { mainConfigurationProperties.setDurationMaxIdleSeconds(durationIdle); } + @Deprecated public int getDurationMaxMessages() { return mainConfigurationProperties.getDurationMaxMessages(); } @@ -445,20 +443,25 @@ public abstract class MainSupport extends ServiceSupport { /** * Sets the duration to run the application to process at most max messages until it * should be terminated. Defaults to -1. Any value <= 0 will run forever. + * @deprecated use {@link #configure()} */ + @Deprecated public void setDurationMaxMessages(int durationMaxMessages) { mainConfigurationProperties.setDurationMaxMessages(durationMaxMessages); } /** * Sets the exit code for the application if duration was hit + * @deprecated use {@link #configure()} */ + @Deprecated public void setDurationHitExitCode(int durationHitExitCode) { - this.durationHitExitCode = durationHitExitCode; + mainConfigurationProperties.setDurationHitExitCode(durationHitExitCode); } + @Deprecated public int getDurationHitExitCode() { - return durationHitExitCode; + return mainConfigurationProperties.getDurationHitExitCode(); } public int getExitCode() { @@ -501,43 +504,52 @@ public abstract class MainSupport extends ServiceSupport { this.routeBuilderClasses = builders; } + @Deprecated public String getFileWatchDirectory() { - return fileWatchDirectory; + return mainConfigurationProperties.getFileWatchDirectory(); } /** * Sets the directory name to watch XML file changes to trigger live reload of Camel routes. * <p/> * Notice you cannot set this value and a custom {@link ReloadStrategy} as well. + * @deprecated use {@link #configure()} */ + @Deprecated public void setFileWatchDirectory(String fileWatchDirectory) { - this.fileWatchDirectory = fileWatchDirectory; + mainConfigurationProperties.setFileWatchDirectory(fileWatchDirectory); } + @Deprecated public boolean isFileWatchDirectoryRecursively() { - return fileWatchDirectoryRecursively; + return mainConfigurationProperties.isFileWatchDirectoryRecursively(); } /** * Sets the flag to watch directory of XML file changes recursively to trigger live reload of Camel routes. * <p/> * Notice you cannot set this value and a custom {@link ReloadStrategy} as well. + * @deprecated use {@link #configure()} */ + @Deprecated public void setFileWatchDirectoryRecursively(boolean fileWatchDirectoryRecursively) { - this.fileWatchDirectoryRecursively = fileWatchDirectoryRecursively; + mainConfigurationProperties.setFileWatchDirectoryRecursively(fileWatchDirectoryRecursively); } + @Deprecated public ReloadStrategy getReloadStrategy() { - return reloadStrategy; + return mainConfigurationProperties.getReloadStrategy(); } /** * Sets a custom {@link ReloadStrategy} to be used. * <p/> * Notice you cannot set this value and the fileWatchDirectory as well. + * @deprecated use {@link #configure()} */ + @Deprecated public void setReloadStrategy(ReloadStrategy reloadStrategy) { - this.reloadStrategy = reloadStrategy; + mainConfigurationProperties.setReloadStrategy(reloadStrategy); } public String getPropertyPlaceholderLocations() { @@ -552,8 +564,9 @@ public abstract class MainSupport extends ServiceSupport { this.propertyPlaceholderLocations = location; } + @Deprecated public boolean isAutoConfigurationEnabled() { - return autoConfigurationEnabled; + return mainConfigurationProperties.isAutoConfigurationEnabled(); } /** @@ -575,9 +588,11 @@ public abstract class MainSupport extends ServiceSupport { * or by using the {@link org.apache.camel.BindToRegistry} annotation style. * <p/> * This option is default enabled. + * @deprecated use {@link #configure()} */ + @Deprecated public void setAutoConfigurationEnabled(boolean autoConfigurationEnabled) { - this.autoConfigurationEnabled = autoConfigurationEnabled; + mainConfigurationProperties.setAutoConfigurationEnabled(autoConfigurationEnabled); } public Properties getInitialProperties() { @@ -623,19 +638,19 @@ public abstract class MainSupport extends ServiceSupport { protected void waitUntilCompleted() { while (!completed.get()) { try { - if (duration > 0) { - LOG.info("Waiting for: {} seconds", duration); - latch.await(duration, TimeUnit.SECONDS); - exitCode.compareAndSet(UNINITIALIZED_EXIT_CODE, durationHitExitCode); + if (mainConfigurationProperties.getDuration() > 0) { + LOG.info("Waiting for: {} seconds", mainConfigurationProperties.getDuration()); + latch.await(mainConfigurationProperties.getDuration(), TimeUnit.SECONDS); + exitCode.compareAndSet(UNINITIALIZED_EXIT_CODE, mainConfigurationProperties.getDurationHitExitCode()); completed.set(true); } else if (mainConfigurationProperties.getDurationMaxIdleSeconds() > 0) { LOG.info("Waiting to be idle for: {} seconds", mainConfigurationProperties.getDurationMaxIdleSeconds()); - exitCode.compareAndSet(UNINITIALIZED_EXIT_CODE, durationHitExitCode); + exitCode.compareAndSet(UNINITIALIZED_EXIT_CODE, mainConfigurationProperties.getDurationHitExitCode()); latch.await(); completed.set(true); } else if (mainConfigurationProperties.getDurationMaxMessages() > 0) { LOG.info("Waiting until: {} messages has been processed", mainConfigurationProperties.getDurationMaxMessages()); - exitCode.compareAndSet(UNINITIALIZED_EXIT_CODE, durationHitExitCode); + exitCode.compareAndSet(UNINITIALIZED_EXIT_CODE, mainConfigurationProperties.getDurationHitExitCode()); latch.await(); completed.set(true); } else { @@ -785,8 +800,8 @@ public abstract class MainSupport extends ServiceSupport { } LOG.info("Using optional properties from classpath:application.properties"); } - if (fileWatchDirectory != null) { - ReloadStrategy reload = new FileWatcherReloadStrategy(fileWatchDirectory, fileWatchDirectoryRecursively); + if (mainConfigurationProperties.getFileWatchDirectory() != null) { + ReloadStrategy reload = new FileWatcherReloadStrategy(mainConfigurationProperties.getFileWatchDirectory(), mainConfigurationProperties.isFileWatchDirectoryRecursively()); camelContext.setReloadStrategy(reload); // ensure reload is added as service and started camelContext.addService(reload); @@ -820,7 +835,7 @@ public abstract class MainSupport extends ServiceSupport { } // need to eager allow to auto configure properties component - if (autoConfigurationEnabled) { + if (mainConfigurationProperties.isAutoConfigurationEnabled()) { autoConfigurationPropertiesComponent(camelContext); autoConfigurationMainConfiguration(camelContext, mainConfigurationProperties); } @@ -833,7 +848,7 @@ public abstract class MainSupport extends ServiceSupport { // conventional configuration via properties to allow configuring options on // component, dataformat, and languages (like spring-boot auto-configuration) - if (autoConfigurationEnabled) { + if (mainConfigurationProperties.isAutoConfigurationEnabled()) { autoConfigurationFromRegistry(camelContext); autoConfigurationFromProperties(camelContext); }