Search in sources :

Example 1 with HystrixConfigurationDefinition

use of org.apache.camel.model.HystrixConfigurationDefinition in project camel by apache.

the class HystrixAutoConfiguration method defaultHystrixConfigurationDefinition.

@Bean(name = HystrixConstants.DEFAULT_HYSTRIX_CONFIGURATION_ID)
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(name = HystrixConstants.DEFAULT_HYSTRIX_CONFIGURATION_ID)
public HystrixConfigurationDefinition defaultHystrixConfigurationDefinition() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    IntrospectionSupport.getProperties(config, properties, null, false);
    HystrixConfigurationDefinition definition = new HystrixConfigurationDefinition();
    IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), definition, properties);
    return definition;
}
Also used : HashMap(java.util.HashMap) HystrixConfigurationDefinition(org.apache.camel.model.HystrixConfigurationDefinition) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with HystrixConfigurationDefinition

use of org.apache.camel.model.HystrixConfigurationDefinition in project camel by apache.

the class HystrixProcessorFactory method buildHystrixConfiguration.

// *******************************
// Helpers
// *******************************
HystrixConfigurationDefinition buildHystrixConfiguration(CamelContext camelContext, HystrixDefinition definition) throws Exception {
    Map<String, Object> properties = new HashMap<>();
    // Extract properties from default configuration, the one configured on
    // camel context takes the precedence over those in the registry
    loadProperties(properties, Suppliers.firstNotNull(() -> camelContext.getHystrixConfiguration(null), () -> lookup(camelContext, HystrixConstants.DEFAULT_HYSTRIX_CONFIGURATION_ID, HystrixConfigurationDefinition.class)));
    // on camel context takes the precedence over those in the registry
    if (definition.getHystrixConfigurationRef() != null) {
        final String ref = definition.getHystrixConfigurationRef();
        loadProperties(properties, Suppliers.firstNotNull(() -> camelContext.getHystrixConfiguration(ref), () -> mandatoryLookup(camelContext, ref, HystrixConfigurationDefinition.class)));
    }
    // Extract properties from local configuration
    loadProperties(properties, Optional.ofNullable(definition.getHystrixConfiguration()));
    // Extract properties from definition
    IntrospectionSupport.getProperties(definition, properties, null, false);
    HystrixConfigurationDefinition config = new HystrixConfigurationDefinition();
    // Apply properties to a new configuration
    IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), config, properties);
    return config;
}
Also used : HashMap(java.util.HashMap) HystrixConfigurationDefinition(org.apache.camel.model.HystrixConfigurationDefinition)

Example 3 with HystrixConfigurationDefinition

use of org.apache.camel.model.HystrixConfigurationDefinition in project camel by apache.

the class HystrixProcessorFactory method doCreateProcessor.

@Override
public Processor doCreateProcessor(RouteContext routeContext, HystrixDefinition definition) throws Exception {
    // create the regular and fallback processors
    Processor processor = definition.createChildProcessor(routeContext, true);
    Processor fallback = null;
    if (definition.getOnFallback() != null) {
        fallback = definition.getOnFallback().createProcessor(routeContext);
    }
    final HystrixConfigurationDefinition config = buildHystrixConfiguration(routeContext.getCamelContext(), definition);
    final String id = definition.idOrCreate(routeContext.getCamelContext().getNodeIdFactory());
    // group and thread pool keys to use they can be configured on configRef and config, so look there first, and if none then use default
    String groupKey = config.getGroupKey();
    String threadPoolKey = config.getThreadPoolKey();
    if (groupKey == null) {
        groupKey = HystrixConfigurationDefinition.DEFAULT_GROUP_KEY;
    }
    if (threadPoolKey == null) {
        // by default use the thread pool from the group
        threadPoolKey = groupKey;
    }
    // use the node id as the command key
    HystrixCommandKey hcCommandKey = HystrixCommandKey.Factory.asKey(id);
    HystrixCommandKey hcFallbackCommandKey = HystrixCommandKey.Factory.asKey(id + "-fallback");
    // use the configured group key
    HystrixCommandGroupKey hcGroupKey = HystrixCommandGroupKey.Factory.asKey(groupKey);
    HystrixThreadPoolKey tpKey = HystrixThreadPoolKey.Factory.asKey(threadPoolKey);
    // create setter using the default options
    HystrixCommand.Setter setter = HystrixCommand.Setter.withGroupKey(hcGroupKey).andCommandKey(hcCommandKey).andThreadPoolKey(tpKey);
    HystrixCommandProperties.Setter commandSetter = HystrixCommandProperties.Setter();
    setter.andCommandPropertiesDefaults(commandSetter);
    HystrixThreadPoolProperties.Setter threadPoolSetter = HystrixThreadPoolProperties.Setter();
    setter.andThreadPoolPropertiesDefaults(threadPoolSetter);
    configureHystrix(commandSetter, threadPoolSetter, config);
    // create setter for fallback via network
    HystrixCommand.Setter fallbackSetter = null;
    boolean fallbackViaNetwork = definition.getOnFallback() != null && definition.getOnFallback().isFallbackViaNetwork();
    if (fallbackViaNetwork) {
        // use a different thread pool that is for fallback (should never use the same thread pool as the regular command)
        HystrixThreadPoolKey tpFallbackKey = HystrixThreadPoolKey.Factory.asKey(threadPoolKey + "-fallback");
        fallbackSetter = HystrixCommand.Setter.withGroupKey(hcGroupKey).andCommandKey(hcFallbackCommandKey).andThreadPoolKey(tpFallbackKey);
        HystrixCommandProperties.Setter commandFallbackSetter = HystrixCommandProperties.Setter();
        fallbackSetter.andCommandPropertiesDefaults(commandFallbackSetter);
        HystrixThreadPoolProperties.Setter fallbackThreadPoolSetter = HystrixThreadPoolProperties.Setter();
        fallbackSetter.andThreadPoolPropertiesDefaults(fallbackThreadPoolSetter);
        // at first configure any shared options
        configureHystrix(commandFallbackSetter, fallbackThreadPoolSetter, config);
    }
    return new HystrixProcessor(hcGroupKey, hcCommandKey, hcFallbackCommandKey, setter, fallbackSetter, processor, fallback, fallbackViaNetwork);
}
Also used : Processor(org.apache.camel.Processor) HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) HystrixThreadPoolKey(com.netflix.hystrix.HystrixThreadPoolKey) HystrixCommandGroupKey(com.netflix.hystrix.HystrixCommandGroupKey) HystrixCommand(com.netflix.hystrix.HystrixCommand) HystrixCommandProperties(com.netflix.hystrix.HystrixCommandProperties) HystrixConfigurationDefinition(org.apache.camel.model.HystrixConfigurationDefinition) HystrixThreadPoolProperties(com.netflix.hystrix.HystrixThreadPoolProperties)

Example 4 with HystrixConfigurationDefinition

use of org.apache.camel.model.HystrixConfigurationDefinition in project camel by apache.

the class HystrixHierarchicalConfigTest method testContextConfiguration.

@Test
public void testContextConfiguration() throws Exception {
    final CamelContext context = new DefaultCamelContext();
    HystrixConfigurationDefinition def = new HystrixConfigurationDefinition();
    def.setGroupKey("global-group-key");
    def.setThreadPoolKey("global-thread-key");
    def.setCorePoolSize(10);
    HystrixConfigurationDefinition ref = new HystrixConfigurationDefinition();
    ref.setGroupKey("ref-group-key");
    ref.setCorePoolSize(5);
    context.setHystrixConfiguration(def);
    context.addHystrixConfiguration("ref-hystrix", ref);
    final HystrixProcessorFactory factory = new HystrixProcessorFactory();
    final HystrixConfigurationDefinition config = factory.buildHystrixConfiguration(context, new HystrixDefinition().hystrixConfiguration("ref-hystrix").hystrixConfiguration().groupKey("local-conf-group-key").requestLogEnabled(false).end());
    Assert.assertEquals("local-conf-group-key", config.getGroupKey());
    Assert.assertEquals("global-thread-key", config.getThreadPoolKey());
    Assert.assertEquals(new Integer(5), config.getCorePoolSize());
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) HystrixConfigurationDefinition(org.apache.camel.model.HystrixConfigurationDefinition) HystrixDefinition(org.apache.camel.model.HystrixDefinition) Test(org.junit.Test)

Example 5 with HystrixConfigurationDefinition

use of org.apache.camel.model.HystrixConfigurationDefinition in project camel by apache.

the class HystrixAutoConfiguration method addHystrixConfigurations.

@PostConstruct
public void addHystrixConfigurations() {
    if (!(beanFactory instanceof ConfigurableBeanFactory)) {
        LOGGER.warn("BeanFactory is not of type ConfigurableBeanFactory");
        return;
    }
    final ConfigurableBeanFactory factory = (ConfigurableBeanFactory) beanFactory;
    final Map<String, Object> properties = new HashMap<>();
    for (Map.Entry<String, HystrixConfigurationCommon> entry : config.getConfigurations().entrySet()) {
        // clear the properties map for reuse
        properties.clear();
        // extract properties
        IntrospectionSupport.getProperties(entry.getValue(), properties, null, false);
        try {
            HystrixConfigurationDefinition definition = new HystrixConfigurationDefinition();
            IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), definition, properties);
            // Registry the definition
            factory.registerSingleton(entry.getKey(), definition);
        } catch (Exception e) {
            throw new BeanCreationException(entry.getKey(), e);
        }
    }
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) BeanCreationException(org.springframework.beans.factory.BeanCreationException) HashMap(java.util.HashMap) HystrixConfigurationCommon(org.apache.camel.model.HystrixConfigurationCommon) HashMap(java.util.HashMap) Map(java.util.Map) HystrixConfigurationDefinition(org.apache.camel.model.HystrixConfigurationDefinition) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeansException(org.springframework.beans.BeansException) PostConstruct(javax.annotation.PostConstruct)

Aggregations

HystrixConfigurationDefinition (org.apache.camel.model.HystrixConfigurationDefinition)11 HystrixDefinition (org.apache.camel.model.HystrixDefinition)7 Test (org.junit.Test)7 RouteDefinition (org.apache.camel.model.RouteDefinition)4 HashMap (java.util.HashMap)3 CamelContext (org.apache.camel.CamelContext)3 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)3 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 SimpleRegistry (org.apache.camel.impl.SimpleRegistry)2 HystrixCommand (com.netflix.hystrix.HystrixCommand)1 HystrixCommandGroupKey (com.netflix.hystrix.HystrixCommandGroupKey)1 HystrixCommandKey (com.netflix.hystrix.HystrixCommandKey)1 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)1 HystrixThreadPoolKey (com.netflix.hystrix.HystrixThreadPoolKey)1 HystrixThreadPoolProperties (com.netflix.hystrix.HystrixThreadPoolProperties)1 Map (java.util.Map)1 PostConstruct (javax.annotation.PostConstruct)1 Processor (org.apache.camel.Processor)1 HystrixConfigurationCommon (org.apache.camel.model.HystrixConfigurationCommon)1 BeansException (org.springframework.beans.BeansException)1