use of org.apache.camel.cloud.ServiceChooser in project camel by apache.
the class ServiceCallDefinition method createProcessor.
// *****************************
// Processor Factory
// *****************************
@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
final CamelContext camelContext = routeContext.getCamelContext();
final ServiceDiscovery serviceDiscovery = retrieveServiceDiscovery(camelContext);
final ServiceFilter serviceFilter = retrieveServiceFilter(camelContext);
final ServiceChooser serviceChooser = retrieveServiceChooser(camelContext);
final LoadBalancer loadBalancer = retrieveLoadBalancer(camelContext);
final Expression expression = retrieveExpression(camelContext);
if (loadBalancer instanceof CamelContextAware) {
((CamelContextAware) loadBalancer).setCamelContext(camelContext);
}
if (loadBalancer instanceof ServiceDiscoveryAware) {
((ServiceDiscoveryAware) loadBalancer).setServiceDiscovery(serviceDiscovery);
}
if (loadBalancer instanceof ServiceFilterAware) {
((ServiceFilterAware) loadBalancer).setServiceFilter(serviceFilter);
}
if (loadBalancer instanceof ServiceChooserAware) {
((ServiceChooserAware) loadBalancer).setServiceChooser(serviceChooser);
}
// The component is used to configure the default scheme to use (eg camel component name).
// The component configured on EIP takes precedence vs configured on configuration.
String component = this.component;
if (component == null) {
ServiceCallConfigurationDefinition conf = retrieveConfig(camelContext);
if (conf != null) {
component = conf.getComponent();
}
}
if (component == null) {
ServiceCallConfigurationDefinition conf = retrieveDefaultConfig(camelContext);
if (conf != null) {
component = conf.getComponent();
}
}
return new DefaultServiceCallProcessor(camelContext, name, component, uri, pattern, loadBalancer, expression);
}
use of org.apache.camel.cloud.ServiceChooser in project camel by apache.
the class ServiceCallDefinition method retrieveServiceChooser.
// ******************************************
// ServiceChooser
// ******************************************
private ServiceChooser retrieveServiceChooser(CamelContext camelContext, Function<CamelContext, ServiceCallConfigurationDefinition> function) throws Exception {
ServiceChooser answer = null;
ServiceCallConfigurationDefinition config = function.apply(camelContext);
if (config != null) {
answer = retrieve(ServiceChooser.class, camelContext, config::getServiceChooser, config::getServiceChooserRef);
if (answer == null) {
String ref = config.getServiceChooserRef();
if (ObjectHelper.equal("roundrobin", ref, true)) {
answer = new RoundRobinServiceChooser();
} else if (ObjectHelper.equal("round-robin", ref, true)) {
answer = new RoundRobinServiceChooser();
} else if (ObjectHelper.equal("random", ref, true)) {
answer = new RandomServiceChooser();
}
}
}
return answer;
}
use of org.apache.camel.cloud.ServiceChooser in project camel by apache.
the class ServiceCallServiceChooserConfiguration method newInstance.
// *************************************************************************
// Factory
// *************************************************************************
@Override
public ServiceChooser newInstance(CamelContext camelContext) throws Exception {
ObjectHelper.notNull(factoryKey, "ServiceChooser factoryKey");
ServiceChooser answer;
// First try to find the factory from the registry.
ServiceChooserFactory factory = CamelContextHelper.lookup(camelContext, factoryKey, ServiceChooserFactory.class);
if (factory != null) {
// If a factory is found in the registry do not re-configure it as
// it should be pre-configured.
answer = factory.newInstance(camelContext);
} else {
Class<?> type;
try {
// Then use Service factory.
type = camelContext.getFactoryFinder(RESOURCE_PATH).findClass(factoryKey);
} catch (Exception e) {
throw new NoFactoryAvailableException(RESOURCE_PATH + factoryKey, e);
}
if (type != null) {
if (ServiceChooserFactory.class.isAssignableFrom(type)) {
factory = (ServiceChooserFactory) camelContext.getInjector().newInstance(type);
} else {
throw new NoFactoryAvailableException("Resolving ServiceChooser: " + factoryKey + " detected type conflict: Not a ServiceChooserFactory implementation. Found: " + type.getName());
}
}
try {
Map<String, Object> parameters = new HashMap<>();
IntrospectionSupport.getProperties(this, parameters, null, false);
parameters.put("properties", getPropertiesAsMap(camelContext));
IntrospectionSupport.setProperties(factory, parameters);
answer = factory.newInstance(camelContext);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
return answer;
}
Aggregations