Search in sources :

Example 1 with FailoverLoadBalancerDefinition

use of org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition in project camel by apache.

the class LoadBalanceDefinition method createProcessor.

@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    // the load balancer is stateful so we should only create it once in case its used from a context scoped error handler
    LoadBalancer loadBalancer = loadBalancerType.getLoadBalancer(routeContext);
    if (loadBalancer == null) {
        // then create it and reuse it
        loadBalancer = loadBalancerType.createLoadBalancer(routeContext);
        loadBalancerType.setLoadBalancer(loadBalancer);
        // some load balancer can only support a fixed number of outputs
        int max = loadBalancerType.getMaximumNumberOfOutputs();
        int size = getOutputs().size();
        if (size > max) {
            throw new IllegalArgumentException("To many outputs configured on " + loadBalancerType + ": " + size + " > " + max);
        }
        for (ProcessorDefinition<?> processorType : getOutputs()) {
            // on Windows boxes or with IBM JDKs etc.
            if (LoadBalanceDefinition.class.isInstance(processorType)) {
                throw new IllegalArgumentException("Loadbalancer already configured to: " + loadBalancerType + ". Cannot set it to: " + processorType);
            }
            Processor processor = createProcessor(routeContext, processorType);
            processor = wrapChannel(routeContext, processor, processorType);
            loadBalancer.addProcessor(processor);
        }
    }
    Boolean inherit = inheritErrorHandler;
    if (loadBalancerType instanceof FailoverLoadBalancerDefinition) {
        // special for failover load balancer where you can configure it to not inherit error handler for its children
        // but the load balancer itself should inherit so Camels error handler can react afterwards
        inherit = true;
    }
    Processor target = wrapChannel(routeContext, loadBalancer, this, inherit);
    return target;
}
Also used : Processor(org.apache.camel.Processor) LoadBalancer(org.apache.camel.processor.loadbalancer.LoadBalancer) FailoverLoadBalancerDefinition(org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition)

Example 2 with FailoverLoadBalancerDefinition

use of org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition in project camel by apache.

the class LoadBalanceDefinition method failover.

/**
     * Uses fail over load balancer
     *
     * @param maximumFailoverAttempts  maximum number of failover attempts before exhausting.
     *                                 Use -1 to newer exhaust when round robin is also enabled.
     *                                 If round robin is disabled then it will exhaust when there are no more endpoints to failover
     * @param inheritErrorHandler      whether or not to inherit error handler.
     *                                 If <tt>false</tt> then it will failover immediately in case of an exception
     * @param roundRobin               whether or not to use round robin (which keeps state)
     * @param sticky                   whether or not to use sticky (which keeps state)
     * @param exceptions               exception classes which we want to failover if one of them was thrown
     * @return the builder
     */
public LoadBalanceDefinition failover(int maximumFailoverAttempts, boolean inheritErrorHandler, boolean roundRobin, boolean sticky, Class<?>... exceptions) {
    FailoverLoadBalancerDefinition def = new FailoverLoadBalancerDefinition();
    def.setExceptionTypes(Arrays.asList(exceptions));
    def.setMaximumFailoverAttempts(maximumFailoverAttempts);
    def.setRoundRobin(roundRobin);
    def.setSticky(sticky);
    setLoadBalancerType(def);
    this.setInheritErrorHandler(inheritErrorHandler);
    return this;
}
Also used : FailoverLoadBalancerDefinition(org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition)

Example 3 with FailoverLoadBalancerDefinition

use of org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition in project camel by apache.

the class XmlParseTest method testParseFailoverLoadBalance.

public void testParseFailoverLoadBalance() throws Exception {
    RouteDefinition route = assertOneRoute("routeWithFailoverLoadBalance.xml");
    assertFrom(route, "seda:a");
    LoadBalanceDefinition loadBalance = assertOneProcessorInstanceOf(LoadBalanceDefinition.class, route);
    assertEquals("Here should have 3 output here", 3, loadBalance.getOutputs().size());
    assertTrue("The loadBalancer should be FailoverLoadBalancerDefinition", loadBalance.getLoadBalancerType() instanceof FailoverLoadBalancerDefinition);
    FailoverLoadBalancerDefinition strategy = (FailoverLoadBalancerDefinition) loadBalance.getLoadBalancerType();
    assertEquals("there should be 2 exceptions", 2, strategy.getExceptions().size());
}
Also used : FailoverLoadBalancerDefinition(org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition)

Aggregations

FailoverLoadBalancerDefinition (org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition)3 Processor (org.apache.camel.Processor)1 LoadBalancer (org.apache.camel.processor.loadbalancer.LoadBalancer)1