Repository: camel Updated Branches: refs/heads/master f84963b2b -> 93f5952e4
http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java deleted file mode 100644 index ba88c8c..0000000 --- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java +++ /dev/null @@ -1,387 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.hystrix; - -import com.netflix.hystrix.HystrixCommand; -import com.netflix.hystrix.HystrixCommandGroupKey; -import com.netflix.hystrix.HystrixCommandKey; -import com.netflix.hystrix.HystrixCommandMetrics; -import com.netflix.hystrix.HystrixCommandProperties; -import com.netflix.hystrix.HystrixRequestCache; -import com.netflix.hystrix.HystrixThreadPoolKey; -import com.netflix.hystrix.HystrixThreadPoolMetrics; -import com.netflix.hystrix.HystrixThreadPoolProperties; -import com.netflix.hystrix.strategy.HystrixPlugins; -import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext; -import org.apache.camel.Endpoint; -import org.apache.camel.Exchange; -import org.apache.camel.Expression; -import org.apache.camel.impl.DefaultProducer; -import org.apache.camel.impl.ProducerCache; -import org.apache.camel.util.CamelContextHelper; -import org.apache.camel.util.EndpointHelper; -import org.apache.camel.util.ServiceHelper; - -/** - * The Hystrix producer. - */ -public class HystrixProducer extends DefaultProducer { - private HystrixConfiguration configuration; - private HystrixRequestContext requestContext; - private ProducerCache producerCache; - - public HystrixProducer(HystrixEndpoint endpoint, HystrixConfiguration configuration) { - super(endpoint); - this.configuration = configuration; - } - - public void process(final Exchange exchange) throws Exception { - HystrixCommand.Setter setter = HystrixCommand.Setter.withGroupKey( - HystrixCommandGroupKey.Factory.asKey(exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_GROUP_KEY, configuration.getGroupKey(), String.class))); - - setCommandPropertiesDefaults(setter, exchange); - setThreadPoolPropertiesDefaults(setter, exchange); - - // lookup the endpoints to use, which can be overridden from headers - String run = exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_RUN_ENDPOINT, configuration.getRunEndpoint(), String.class); - String fallback = exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_FALLBACK_ENDPOINT, configuration.getFallbackEndpoint(), String.class); - Endpoint runEndpoint = exchange.getContext().getEndpoint(run); - Endpoint fallbackEndpoint = fallback != null ? exchange.getContext().getEndpoint(fallback) : null; - - if (log.isDebugEnabled()) { - log.debug("Run endpoint: {}", runEndpoint); - log.debug("Fallback endpoint: {}", fallbackEndpoint); - } - - CamelHystrixCommand camelHystrixCommand = new CamelHystrixCommand(setter, exchange, getCacheKey(exchange), producerCache, runEndpoint, fallbackEndpoint); - - checkRequestContextPresent(exchange); - clearCache(camelHystrixCommand.getCommandKey(), exchange); - camelHystrixCommand.execute(); - - if (configuration.isMetricsEnabled()) { - populateWithMetrics(exchange, camelHystrixCommand); - } - } - - private void setCommandPropertiesDefaults(HystrixCommand.Setter setter, Exchange exchange) { - HystrixCommandProperties.Setter commandDefaults = HystrixCommandProperties.Setter(); - setter.andCommandPropertiesDefaults(commandDefaults); - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_COMMAND_KEY, configuration.getCommandKey(), String.class) != null) { - setter.andCommandKey(HystrixCommandKey.Factory.asKey( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_COMMAND_KEY, configuration.getCommandKey(), String.class))); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CIRCUIT_BREAKER_ENABLED, configuration.getCircuitBreakerEnabled(), Boolean.class) != null) { - commandDefaults.withCircuitBreakerEnabled( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CIRCUIT_BREAKER_ENABLED, configuration.getCircuitBreakerEnabled(), Boolean.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, - configuration.getCircuitBreakerErrorThresholdPercentage(), Integer.class) != null) { - commandDefaults.withCircuitBreakerErrorThresholdPercentage( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, - configuration.getCircuitBreakerErrorThresholdPercentage(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CIRCUIT_BREAKER_FORCE_CLOSED, - configuration.getCircuitBreakerForceClosed(), Boolean.class) != null) { - commandDefaults.withCircuitBreakerForceClosed( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CIRCUIT_BREAKER_FORCE_CLOSED, - configuration.getCircuitBreakerForceClosed(), Boolean.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CIRCUIT_BREAKER_FORCE_OPEN, - configuration.getCircuitBreakerForceOpen(), Boolean.class) != null) { - commandDefaults.withCircuitBreakerForceOpen( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CIRCUIT_BREAKER_FORCE_OPEN, - configuration.getCircuitBreakerForceOpen(), Boolean.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, - configuration.getCircuitBreakerRequestVolumeThreshold(), Integer.class) != null) { - commandDefaults.withCircuitBreakerRequestVolumeThreshold( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, - configuration.getCircuitBreakerRequestVolumeThreshold(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, - configuration.getCircuitBreakerSleepWindowInMilliseconds(), Integer.class) != null) { - commandDefaults.withCircuitBreakerSleepWindowInMilliseconds( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, - configuration.getCircuitBreakerSleepWindowInMilliseconds(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_EXECUTION_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, - configuration.getExecutionIsolationSemaphoreMaxConcurrentRequests(), Integer.class) != null) { - commandDefaults.withExecutionIsolationSemaphoreMaxConcurrentRequests( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_EXECUTION_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, - configuration.getExecutionIsolationSemaphoreMaxConcurrentRequests(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_EXECUTION_ISOLATION_STRATEGY, - configuration.getExecutionIsolationStrategy(), String.class) != null) { - commandDefaults.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.valueOf( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_EXECUTION_ISOLATION_STRATEGY, - configuration.getExecutionIsolationStrategy(), String.class))); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_EXECUTION_ISOLATION_THREAD_INTERRUPTION_ON_TIMEOUT, - configuration.getExecutionIsolationThreadInterruptOnTimeout(), Boolean.class) != null) { - commandDefaults.withExecutionIsolationThreadInterruptOnTimeout( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_EXECUTION_ISOLATION_THREAD_INTERRUPTION_ON_TIMEOUT, - configuration.getExecutionIsolationThreadInterruptOnTimeout(), Boolean.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_EXECUTION_TIMEOUT_IN_MILLISECONDS, - configuration.getExecutionTimeoutInMilliseconds(), Integer.class) != null) { - commandDefaults.withExecutionTimeoutInMilliseconds( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_EXECUTION_TIMEOUT_IN_MILLISECONDS, - configuration.getExecutionTimeoutInMilliseconds(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_EXECUTION_TIMEOUT_ENABLED, - configuration.getExecutionTimeoutEnabled(), Boolean.class) != null) { - commandDefaults.withExecutionTimeoutEnabled( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_EXECUTION_TIMEOUT_ENABLED, - configuration.getExecutionTimeoutEnabled(), Boolean.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_FALLBACK_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, - configuration.getFallbackIsolationSemaphoreMaxConcurrentRequests(), Integer.class) != null) { - commandDefaults.withFallbackIsolationSemaphoreMaxConcurrentRequests( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_FALLBACK_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, - configuration.getFallbackIsolationSemaphoreMaxConcurrentRequests(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_FALLBACK_ENABLED, - configuration.getFallbackEnabled(), Boolean.class) != null) { - commandDefaults.withFallbackEnabled( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_FALLBACK_ENABLED, - configuration.getFallbackEnabled(), Boolean.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_HEALTH_SNAPSHOT_INTERVAL_IN_MILLISECONDS, - configuration.getMetricsHealthSnapshotIntervalInMilliseconds(), Integer.class) != null) { - commandDefaults.withMetricsHealthSnapshotIntervalInMilliseconds( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_HEALTH_SNAPSHOT_INTERVAL_IN_MILLISECONDS, - configuration.getMetricsHealthSnapshotIntervalInMilliseconds(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_ROLLING_PERCENTILE_BUCKET_SIZE, - configuration.getMetricsRollingPercentileBucketSize(), Integer.class) != null) { - commandDefaults.withMetricsRollingPercentileBucketSize( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_ROLLING_PERCENTILE_BUCKET_SIZE, - configuration.getMetricsRollingPercentileBucketSize(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_ROLLING_PERCENTILE_ENABLED, - configuration.getMetricsRollingPercentileEnabled(), Boolean.class) != null) { - commandDefaults.withMetricsRollingPercentileEnabled( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_ROLLING_PERCENTILE_ENABLED, - configuration.getMetricsRollingPercentileEnabled(), Boolean.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_ROLLING_PERCENTILE_WINDOW_IN_MILLISECONDS, - configuration.getMetricsRollingPercentileWindowInMilliseconds(), Integer.class) != null) { - commandDefaults.withMetricsRollingPercentileWindowInMilliseconds( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_ROLLING_PERCENTILE_WINDOW_IN_MILLISECONDS, - configuration.getMetricsRollingPercentileWindowInMilliseconds(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_ROLLING_PERCENTILE_WINDOW_BUCKETS, - configuration.getMetricsRollingPercentileWindowBuckets(), Integer.class) != null) { - commandDefaults.withMetricsRollingPercentileWindowBuckets( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_ROLLING_PERCENTILE_WINDOW_BUCKETS, - configuration.getMetricsRollingPercentileWindowBuckets(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_ROLLING_STATISTICAL_WINDOW_IN_MILLISECONDS, - configuration.getMetricsRollingStatisticalWindowInMilliseconds(), Integer.class) != null) { - commandDefaults.withMetricsRollingStatisticalWindowInMilliseconds( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_ROLLING_STATISTICAL_WINDOW_IN_MILLISECONDS, - configuration.getMetricsRollingStatisticalWindowInMilliseconds(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_ROLLING_STATISTICAL_WINDOW_BUCKETS, - configuration.getMetricsRollingStatisticalWindowBuckets(), Integer.class) != null) { - commandDefaults.withMetricsRollingStatisticalWindowBuckets( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_METRICS_ROLLING_STATISTICAL_WINDOW_BUCKETS, - configuration.getMetricsRollingStatisticalWindowBuckets(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_REQUEST_CACHE_ENABLED, configuration.getRequestCacheEnabled(), Boolean.class) != null) { - commandDefaults.withRequestCacheEnabled( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_REQUEST_CACHE_ENABLED, configuration.getRequestCacheEnabled(), Boolean.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_REQUEST_LOG_ENABLED, configuration.getRequestLogEnabled(), Boolean.class) != null) { - commandDefaults.withRequestLogEnabled( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_REQUEST_LOG_ENABLED, configuration.getRequestLogEnabled(), Boolean.class)); - } - } - - private void setThreadPoolPropertiesDefaults(HystrixCommand.Setter setter, Exchange exchange) { - HystrixThreadPoolProperties.Setter threadPoolProperties = HystrixThreadPoolProperties.Setter(); - setter.andThreadPoolPropertiesDefaults(threadPoolProperties); - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_KEY, configuration.getThreadPoolKey(), String.class) != null) { - setter.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_KEY, configuration.getThreadPoolKey(), String.class))); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CORE_SIZE, configuration.getCoreSize(), Integer.class) != null) { - threadPoolProperties.withCoreSize( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CORE_SIZE, configuration.getCoreSize(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_KEEP_ALIVE_TIME, configuration.getKeepAliveTime(), Integer.class) != null) { - threadPoolProperties.withKeepAliveTimeMinutes( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_KEEP_ALIVE_TIME, configuration.getKeepAliveTime(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_MAX_QUEUE_SIZE, configuration.getMaxQueueSize(), Integer.class) != null) { - threadPoolProperties.withMaxQueueSize( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_MAX_QUEUE_SIZE, configuration.getMaxQueueSize(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_QUEUE_SIZE_REJECTION_THRESHOLD, - configuration.getQueueSizeRejectionThreshold(), Integer.class) != null) { - threadPoolProperties.withQueueSizeRejectionThreshold( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_QUEUE_SIZE_REJECTION_THRESHOLD, - configuration.getQueueSizeRejectionThreshold(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_ROLLING_STATISTICAL_WINDOW_IN_MILLISECONDS, - configuration.getThreadPoolMetricsRollingStatisticalWindowInMilliseconds(), Integer.class) != null) { - threadPoolProperties.withMetricsRollingStatisticalWindowInMilliseconds( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_ROLLING_STATISTICAL_WINDOW_IN_MILLISECONDS, - configuration.getThreadPoolMetricsRollingStatisticalWindowInMilliseconds(), Integer.class)); - } - - if (exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_ROLLING_NUMBER_STATISTICAL_WINDOW_BUCKETS, - configuration.getThreadPoolMetricsRollingStatisticalWindowBuckets(), Integer.class) != null) { - threadPoolProperties.withMetricsRollingStatisticalWindowBuckets( - exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_ROLLING_NUMBER_STATISTICAL_WINDOW_BUCKETS, - configuration.getThreadPoolMetricsRollingStatisticalWindowBuckets(), Integer.class)); - } - } - - private String getCacheKey(Exchange exchange) { - Object cacheKey = exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CACHE_KEY, configuration.getCacheKey(), Object.class); - if (cacheKey == null) { - return null; - } - - String answer; - Expression expression; - if (cacheKey instanceof Expression) { - // it may be an expression already - expression = (Expression) cacheKey; - } else { - // otherwise its a string that either refer to an expression to lookup or use the simple languagae - String key = cacheKey.toString(); - if (EndpointHelper.isReferenceParameter(key)) { - expression = CamelContextHelper.mandatoryLookup(exchange.getContext(), key.substring(1), Expression.class); - } else { - // use simple language as default for the expression - expression = exchange.getContext().resolveLanguage("simple").createExpression(key); - } - } - - answer = expression.evaluate(exchange, String.class); - return answer; - } - - private synchronized void checkRequestContextPresent(Exchange exchange) { - if (!HystrixRequestContext.isCurrentThreadInitialized()) { - HystrixRequestContext customRequestContext = exchange.getIn() - .getHeader(HystrixConstants.CAMEL_HYSTRIX_REQUEST_CONTEXT, HystrixRequestContext.class); - - if (customRequestContext != null) { - HystrixRequestContext.setContextOnCurrentThread(customRequestContext); - } else if (requestContext != null) { - HystrixRequestContext.setContextOnCurrentThread(requestContext); - exchange.getIn().setHeader(HystrixConstants.CAMEL_HYSTRIX_REQUEST_CONTEXT, requestContext); - } - } - } - - private void clearCache(HystrixCommandKey camelHystrixCommand, Exchange exchange) { - Boolean clearCache = exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CLEAR_CACHE_FIRST, Boolean.class); - if (clearCache != null && clearCache) { - HystrixRequestCache.getInstance(camelHystrixCommand, - HystrixPlugins.getInstance().getConcurrencyStrategy()).clear(String.valueOf(getCacheKey(exchange))); - } - } - - private static void populateWithMetrics(Exchange exchange, CamelHystrixCommand camelHystrixCommand) { - HystrixCommandMetrics commandMetrics = HystrixCommandMetrics.getInstance(camelHystrixCommand.getCommandKey()); - HystrixThreadPoolMetrics threadPoolMetrics = HystrixThreadPoolMetrics.getInstance(camelHystrixCommand.getThreadPoolKey()); - - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_COMMAND_METRICS_TOTAL_REQUESTS, commandMetrics.getHealthCounts().getTotalRequests()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_COMMAND_METRICS_ERROR_COUNT, commandMetrics.getHealthCounts().getErrorCount()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_COMMAND_METRICS_ERROR_PERCENTAGE, commandMetrics.getHealthCounts().getErrorPercentage()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_COMMAND_METRICS_CURRENT_CONCURRENT_EXECUTION_COUNT, commandMetrics.getCurrentConcurrentExecutionCount()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_COMMAND_METRICS_EXECUTION_TIME_MEAN, commandMetrics.getExecutionTimeMean()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_COMMAND_METRICS_ROLLING_MAX_CONCURRENT_EXECUTIONS, commandMetrics.getRollingMaxConcurrentExecutions()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_COMMAND_METRICS_TOTAL_TIME_MEAN, commandMetrics.getTotalTimeMean()); - - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_CURRENT_ACTIVE_COUNT, threadPoolMetrics.getCurrentActiveCount()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_CUMULATIVE_COUNT_THREADS_EXECUTED, threadPoolMetrics.getCumulativeCountThreadsExecuted()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_CURRENT_COMPLETED_TASK_COUNT, threadPoolMetrics.getCurrentCompletedTaskCount()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_CURRENT_CORE_POOL_SIZE, threadPoolMetrics.getCurrentCorePoolSize()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_CURRENT_LARGEST_POOL_SIZE, threadPoolMetrics.getCurrentLargestPoolSize()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_CURRENT_MAXIMUM_POOL_SIZE, threadPoolMetrics.getCurrentMaximumPoolSize()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_CURRENT_POOL_SIZE, threadPoolMetrics.getCurrentPoolSize()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_CURRENT_QUEUE_SIZE, threadPoolMetrics.getCurrentQueueSize()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_CURRENT_TASK_COUNT, threadPoolMetrics.getCurrentTaskCount()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_ROLLING_COUNT_THREADS_EXECUTED, threadPoolMetrics.getRollingCountThreadsExecuted()); - setHeader(exchange, HystrixConstants.CAMEL_HYSTRIX_THREAD_POOL_METRICS_ROLLING_MAX_ACTIVE_THREADS, threadPoolMetrics.getRollingMaxActiveThreads()); - } - - private static void setHeader(Exchange exchange, String key, Object value) { - if (exchange.hasOut()) { - exchange.getOut().setHeader(key, value); - } else { - exchange.getIn().setHeader(key, value); - } - } - - @Override - protected void doStart() throws Exception { - // setup the producer cache - producerCache = new ProducerCache(this, getEndpoint().getCamelContext()); - ServiceHelper.startService(producerCache); - - if (configuration.getInitializeRequestContext() != null && configuration.getInitializeRequestContext()) { - requestContext = HystrixRequestContext.initializeContext(); - } - super.doStart(); - } - - @Override - protected void doStop() throws Exception { - if (requestContext != null) { - requestContext.shutdown(); - } - ServiceHelper.stopService(producerCache); - super.doStop(); - } - -} http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixConstants.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixConstants.java new file mode 100644 index 0000000..18ac8c0 --- /dev/null +++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixConstants.java @@ -0,0 +1,28 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.hystrix.processor; + +public interface HystrixConstants { + + // Hystrix EIP response properties + String HYSTRIX_RESPONSE_SUCCESSFUL_EXECUTION = "CamelHystrixSuccessfulExecution"; + String HYSTRIX_RESPONSE_FROM_FALLBACK = "CamelHystrixResponseFromFallback"; + String HYSTRIX_RESPONSE_SHORT_CIRCUITED = "CamelHystrixResponseShortCircuited"; + String HYSTRIX_RESPONSE_TIMED_OUT = "CamelHystrixResponseTimedOut"; + String HYSTRIX_RESPONSE_REJECTED = "CamelHystrixResponseRejected"; + +} http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessor.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessor.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessor.java index 85f3f10..3993a264 100644 --- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessor.java +++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessor.java @@ -30,7 +30,6 @@ import org.apache.camel.Navigate; import org.apache.camel.Processor; import org.apache.camel.api.management.ManagedAttribute; import org.apache.camel.api.management.ManagedResource; -import org.apache.camel.component.hystrix.HystrixConstants; import org.apache.camel.spi.IdAware; import org.apache.camel.support.ServiceSupport; import org.apache.camel.util.AsyncProcessorHelper; http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/main/resources/META-INF/services/org/apache/camel/component/hystrix ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/main/resources/META-INF/services/org/apache/camel/component/hystrix b/components/camel-hystrix/src/main/resources/META-INF/services/org/apache/camel/component/hystrix deleted file mode 100644 index 8fc3ece..0000000 --- a/components/camel-hystrix/src/main/resources/META-INF/services/org/apache/camel/component/hystrix +++ /dev/null @@ -1,18 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -class=org.apache.camel.component.hystrix.HystrixComponent http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentBase.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentBase.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentBase.java deleted file mode 100644 index acd8240..0000000 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentBase.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.hystrix; - -import org.apache.camel.EndpointInject; -import org.apache.camel.Produce; -import org.apache.camel.ProducerTemplate; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.test.junit4.CamelTestSupport; - -public class HystrixComponentBase extends CamelTestSupport { - - @Produce(uri = "direct:start") - protected ProducerTemplate template; - - @EndpointInject(uri = "mock:result") - protected MockEndpoint resultEndpoint; - - @EndpointInject(uri = "mock:error") - protected MockEndpoint errorEndpoint; - -} - http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCacheTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCacheTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCacheTest.java deleted file mode 100644 index 4a84c18..0000000 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCacheTest.java +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.hystrix; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import org.apache.camel.Exchange; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.junit.Test; - -public class HystrixComponentCacheTest extends HystrixComponentBase { - - @Test - public void invokesCachedEndpoint() throws Exception { - resultEndpoint.expectedMessageCount(1); - errorEndpoint.expectedMessageCount(0); - - template.sendBodyAndHeader("body", "key", "cachedKey"); - template.sendBodyAndHeader("body", "key", "cachedKey"); - - assertMockEndpointsSatisfied(); - - resultEndpoint.expectedMessageCount(2); - template.sendBodyAndHeader("body", "key", "cachedKey"); - template.sendBodyAndHeader("body", "key", "differentCachedKey"); - assertMockEndpointsSatisfied(); - } - - @Test - public void invokesCachedEndpointFromDifferentThread() throws Exception { - resultEndpoint.expectedMessageCount(1); - errorEndpoint.expectedMessageCount(0); - - template.sendBodyAndHeader("body", "key", "cachedKey"); - - final CountDownLatch latch = new CountDownLatch(1); - new Thread(new Runnable() { - @Override - public void run() { - template.sendBodyAndHeader("body", "key", "cachedKey"); - latch.countDown(); - } - }).start(); - - latch.await(2, TimeUnit.SECONDS); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - - public void configure() { - - from("direct:fallback") - .to("mock:error"); - - from("direct:run") - .to("mock:result"); - - from("direct:start") - .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&cacheKey=header.key&initializeRequestContext=true"); - } - }; - } -} - http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCircuitBreakerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCircuitBreakerTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCircuitBreakerTest.java deleted file mode 100644 index d0e33ec..0000000 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCircuitBreakerTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.hystrix; - -import org.apache.camel.Exchange; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.junit.Test; - -public class HystrixComponentCircuitBreakerTest extends HystrixComponentBase { - - @Test - public void circuitBreakerRejectsWhenTresholdReached() throws Exception { - final int requestCount = 5; - resultEndpoint.expectedMessageCount(3); - errorEndpoint.expectedMessageCount(requestCount); - resultEndpoint.whenAnyExchangeReceived(new Processor() { - @Override - public void process(Exchange exchange) throws Exception { - throw new RuntimeException("blow"); - } - }); - - for (int i = 0; i < requestCount; i++) { - try { - template.sendBody("test" + i); - } catch (Exception e) { - // ignore - } - } - assertMockEndpointsSatisfied(); - } - - @Override - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - - public void configure() { - - from("direct:fallback") - .to("log:fallback") - .to("mock:error"); - - from("direct:run") - .process(new Processor() { - @Override - public void process(Exchange exchange) throws Exception { - Thread.sleep(500); - } - }) - .to("log:result") - .to("mock:result"); - - from("direct:start") - .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&circuitBreakerRequestVolumeThreshold=2"); - } - }; - } -} - http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentFallbackTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentFallbackTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentFallbackTest.java deleted file mode 100644 index 0091510..0000000 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentFallbackTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.hystrix; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import org.apache.camel.CamelContext; -import org.apache.camel.EndpointInject; -import org.apache.camel.Exchange; -import org.apache.camel.Processor; -import org.apache.camel.Produce; -import org.apache.camel.ProducerTemplate; -import org.apache.camel.builder.ExpressionBuilder; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.impl.DefaultCamelContext; -import org.apache.camel.impl.SimpleRegistry; -import org.apache.camel.test.junit4.CamelTestSupport; -import org.junit.Test; - -public class HystrixComponentFallbackTest extends HystrixComponentBase { - - @Test - public void invokesTargetEndpoint() throws Exception { - resultEndpoint.expectedMessageCount(1); - errorEndpoint.expectedMessageCount(0); - - template.sendBody("test"); - - assertMockEndpointsSatisfied(); - } - - @Test - public void invokesFallbackEndpointExceptionThrown() throws Exception { - resultEndpoint.expectedMessageCount(1); - errorEndpoint.expectedMessageCount(1); - resultEndpoint.whenAnyExchangeReceived(new Processor() { - @Override - public void process(Exchange exchange) throws Exception { - throw new RuntimeException("blow"); - } - }); - - template.sendBody("test"); - - assertMockEndpointsSatisfied(); - } - - @Test - public void invokesFallbackEndpointExceptionSet() throws Exception { - resultEndpoint.expectedMessageCount(1); - errorEndpoint.expectedMessageCount(1); - resultEndpoint.whenAnyExchangeReceived(new Processor() { - @Override - public void process(Exchange exchange) throws Exception { - exchange.setException(new RuntimeException("blow")); - } - }); - - template.sendBody("test"); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - - public void configure() { - - from("direct:fallback") - .to("mock:error"); - - from("direct:run") - .to("mock:result"); - - from("direct:start") - .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&initializeRequestContext=true"); - } - }; - } -} - http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentOverrideRunTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentOverrideRunTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentOverrideRunTest.java deleted file mode 100644 index d654433..0000000 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentOverrideRunTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.hystrix; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.camel.EndpointInject; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; - -public class HystrixComponentOverrideRunTest extends HystrixComponentBase { - - @EndpointInject(uri = "mock:run2") - protected MockEndpoint run2Endpoint; - - @Test - public void invokesOverrideHeader() throws Exception { - run2Endpoint.expectedMessageCount(1); - resultEndpoint.expectedMessageCount(0); - errorEndpoint.expectedMessageCount(0); - - Map headers = new HashMap<>(); - headers.put("key", "cacheKey"); - headers.put(HystrixConstants.CAMEL_HYSTRIX_RUN_ENDPOINT, "direct:run2"); - - template.sendBodyAndHeaders("body", headers); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - - public void configure() { - from("direct:fallback") - .to("mock:error"); - - from("direct:run") - .to("mock:result"); - - from("direct:run2") - .to("mock:run2"); - - from("direct:start") - .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&initializeRequestContext=true"); - } - }; - } -} - http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentRequestContextTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentRequestContextTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentRequestContextTest.java deleted file mode 100644 index acc3c1f..0000000 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentRequestContextTest.java +++ /dev/null @@ -1,101 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.hystrix; - -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext; -import org.apache.camel.CamelContext; -import org.apache.camel.EndpointInject; -import org.apache.camel.Produce; -import org.apache.camel.ProducerTemplate; -import org.apache.camel.builder.ExpressionBuilder; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.impl.DefaultCamelContext; -import org.apache.camel.impl.SimpleRegistry; -import org.apache.camel.test.junit4.CamelTestSupport; -import org.junit.Test; - -public class HystrixComponentRequestContextTest extends HystrixComponentBase { - - @Test - public void invokesCachedEndpointWithCustomRequestContext() throws Exception { - resultEndpoint.expectedMessageCount(1); - errorEndpoint.expectedMessageCount(0); - - HystrixRequestContext customContext = HystrixRequestContext.initializeContext(); - final Map headers = new HashMap<>(); - headers.put("key", "cachedKey"); - headers.put(HystrixConstants.CAMEL_HYSTRIX_REQUEST_CONTEXT, customContext); - - template.sendBodyAndHeaders("body", headers); - - final CountDownLatch latch = new CountDownLatch(1); - new Thread(new Runnable() { - @Override - public void run() { - template.sendBodyAndHeaders("body", headers); - latch.countDown(); - } - }).start(); - - latch.await(2, TimeUnit.SECONDS); - - assertMockEndpointsSatisfied(); - } - - @Test - public void invokesCachedEndpointTwiceWhenCacheIsCleared() throws Exception { - resultEndpoint.expectedMessageCount(2); - errorEndpoint.expectedMessageCount(0); - - HystrixRequestContext customContext = HystrixRequestContext.initializeContext(); - final Map headers = new HashMap<>(); - headers.put("key", "cachedKey"); - headers.put(HystrixConstants.CAMEL_HYSTRIX_REQUEST_CONTEXT, customContext); - - template.sendBodyAndHeaders("body", headers); - - headers.put(HystrixConstants.CAMEL_HYSTRIX_CLEAR_CACHE_FIRST, true); - - template.sendBodyAndHeaders("body", headers); - assertMockEndpointsSatisfied(); - } - - @Override - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - - public void configure() { - - from("direct:fallback") - .to("mock:error"); - - from("direct:run") - .to("mock:result"); - - from("direct:start") - .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&cacheKey=header.key"); - } - }; - } -} - http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentTimeOutTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentTimeOutTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentTimeOutTest.java deleted file mode 100644 index c5ef84d..0000000 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentTimeOutTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.hystrix; - -import org.apache.camel.CamelContext; -import org.apache.camel.EndpointInject; -import org.apache.camel.Exchange; -import org.apache.camel.Processor; -import org.apache.camel.Produce; -import org.apache.camel.ProducerTemplate; -import org.apache.camel.builder.ExpressionBuilder; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.impl.DefaultCamelContext; -import org.apache.camel.impl.SimpleRegistry; -import org.apache.camel.test.junit4.CamelTestSupport; -import org.junit.Test; - -public class HystrixComponentTimeOutTest extends CamelTestSupport { - - @Produce(uri = "direct:start") - protected ProducerTemplate template; - - @EndpointInject(uri = "mock:result") - protected MockEndpoint resultEndpoint; - - @EndpointInject(uri = "mock:error") - protected MockEndpoint errorEndpoint; - - @Test - public void slowOperationTimesOutAndFallbacks() throws Exception { - resultEndpoint.expectedMessageCount(0); - errorEndpoint.expectedMessageCount(1); - - template.sendBody("test"); - - assertMockEndpointsSatisfied(); - } - - @Test - public void slowOperationSucceedsWithExtendedTimeout() throws Exception { - resultEndpoint.expectedMessageCount(1); - errorEndpoint.expectedMessageCount(0); - - template.sendBodyAndHeader("test", HystrixConstants.CAMEL_HYSTRIX_EXECUTION_TIMEOUT_IN_MILLISECONDS, Integer.valueOf(700)); - - assertMockEndpointsSatisfied(); - } - - @Override - protected CamelContext createCamelContext() throws Exception { - SimpleRegistry registry = new SimpleRegistry(); - CamelContext context = new DefaultCamelContext(registry); - registry.put("run", context.getEndpoint("direct:run")); - registry.put("fallback", context.getEndpoint("direct:fallback")); - registry.put("headerExpression", ExpressionBuilder.headerExpression("key")); - return context; - } - - @Override - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - - public void configure() { - - from("direct:fallback") - .to("mock:error"); - - from("direct:run") - .process(new Processor() { - @Override - public void process(Exchange exchange) throws Exception { - Thread.sleep(500); //a slow operation - } - }) - .to("mock:result"); - - from("direct:start") - .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&executionTimeoutInMilliseconds=100"); - } - }; - } -} - http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/TryCatchTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/TryCatchTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/TryCatchTest.java deleted file mode 100644 index 089b3b8..0000000 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/TryCatchTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.hystrix; - -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.test.junit4.CamelTestSupport; -import org.junit.Test; - -public class TryCatchTest extends CamelTestSupport { - - @Test - public void testTryCatch() throws Exception { - getMockEndpoint("mock:result").expectedBodiesReceived("Fallback message"); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("direct:start") - .doTry() - .to("direct:foo") - .doCatch(Exception.class) - .transform().constant("Fallback message") - .end() - .to("mock:result"); - - from("direct:foo").errorHandler(noErrorHandler()) - .throwException(new IllegalArgumentException("Forced")); - } - }; - } - -} http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/BlueprintHystrixRouteFallbackTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/BlueprintHystrixRouteFallbackTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/BlueprintHystrixRouteFallbackTest.java index 861a2ed..218a7b2 100644 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/BlueprintHystrixRouteFallbackTest.java +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/BlueprintHystrixRouteFallbackTest.java @@ -16,7 +16,6 @@ */ package org.apache.camel.component.hystrix.processor; -import org.apache.camel.component.hystrix.HystrixConstants; import org.apache.camel.test.blueprint.CamelBlueprintTestSupport; import org.junit.Test; http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/BlueprintHystrixRouteOkTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/BlueprintHystrixRouteOkTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/BlueprintHystrixRouteOkTest.java index 21263fd..bc42368 100644 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/BlueprintHystrixRouteOkTest.java +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/BlueprintHystrixRouteOkTest.java @@ -16,7 +16,6 @@ */ package org.apache.camel.component.hystrix.processor; -import org.apache.camel.component.hystrix.HystrixConstants; import org.apache.camel.test.blueprint.CamelBlueprintTestSupport; import org.junit.Test; http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackTest.java index 8efe089..a28ccf5 100644 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackTest.java +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackTest.java @@ -17,7 +17,6 @@ package org.apache.camel.component.hystrix.processor; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.hystrix.HystrixConstants; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test; http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackViaNetworkTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackViaNetworkTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackViaNetworkTest.java index a262813..f6716fd 100644 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackViaNetworkTest.java +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackViaNetworkTest.java @@ -17,7 +17,6 @@ package org.apache.camel.component.hystrix.processor; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.hystrix.HystrixConstants; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test; http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteOkTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteOkTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteOkTest.java index a56c405..494881f 100644 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteOkTest.java +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteOkTest.java @@ -17,7 +17,6 @@ package org.apache.camel.component.hystrix.processor; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.hystrix.HystrixConstants; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test; http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteFallbackTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteFallbackTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteFallbackTest.java index 4bbff0e..2857b27 100644 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteFallbackTest.java +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteFallbackTest.java @@ -16,7 +16,6 @@ */ package org.apache.camel.component.hystrix.processor; -import org.apache.camel.component.hystrix.HystrixConstants; import org.apache.camel.test.spring.CamelSpringTestSupport; import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; http://git-wip-us.apache.org/repos/asf/camel/blob/93f5952e/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteOkTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteOkTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteOkTest.java index 5f408de..18622bb 100644 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteOkTest.java +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteOkTest.java @@ -16,7 +16,6 @@ */ package org.apache.camel.component.hystrix.processor; -import org.apache.camel.component.hystrix.HystrixConstants; import org.apache.camel.test.spring.CamelSpringTestSupport; import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext;