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;
}
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;
}
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);
}
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());
}
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);
}
}
}
Aggregations