Skip to content

Commit

Permalink
CAMEL-8684: Fixed load balancer model to not implement LoadBalancer/P…
Browse files Browse the repository at this point in the history
…rocessor as that is runtime. Also ensure Java DSL build the routes using the model classes instead of cheating.
  • Loading branch information
davsclaus committed Apr 22, 2015
1 parent 5b27ecc commit ea6d2fc
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,7 @@
import org.apache.camel.model.loadbalancer.StickyLoadBalancerDefinition;
import org.apache.camel.model.loadbalancer.TopicLoadBalancerDefinition;
import org.apache.camel.model.loadbalancer.WeightedLoadBalancerDefinition;
import org.apache.camel.processor.loadbalancer.CircuitBreakerLoadBalancer;
import org.apache.camel.processor.loadbalancer.FailOverLoadBalancer;
import org.apache.camel.processor.loadbalancer.LoadBalancer;
import org.apache.camel.processor.loadbalancer.RandomLoadBalancer;
import org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer;
import org.apache.camel.processor.loadbalancer.StickyLoadBalancer;
import org.apache.camel.processor.loadbalancer.TopicLoadBalancer;
import org.apache.camel.processor.loadbalancer.WeightedLoadBalancer;
import org.apache.camel.processor.loadbalancer.WeightedRandomLoadBalancer;
import org.apache.camel.processor.loadbalancer.WeightedRoundRobinLoadBalancer;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.RouteContext;
import org.apache.camel.util.CollectionStringBuffer;
Expand Down Expand Up @@ -204,10 +195,11 @@ public LoadBalanceDefinition failover(Class<?>... exceptions) {
* @return the builder
*/
public LoadBalanceDefinition failover(int maximumFailoverAttempts, boolean inheritErrorHandler, boolean roundRobin, Class<?>... exceptions) {
FailOverLoadBalancer failover = new FailOverLoadBalancer(Arrays.asList(exceptions));
failover.setMaximumFailoverAttempts(maximumFailoverAttempts);
failover.setRoundRobin(roundRobin);
setLoadBalancerType(new LoadBalancerDefinition(failover));
FailoverLoadBalancerDefinition def = new FailoverLoadBalancerDefinition();
def.setExceptionTypes(Arrays.asList(exceptions));
def.setMaximumFailoverAttempts(maximumFailoverAttempts);
def.setRoundRobin(roundRobin);
setLoadBalancerType(def);
this.setInheritErrorHandler(inheritErrorHandler);
return this;
}
Expand All @@ -232,11 +224,11 @@ public LoadBalanceDefinition weighted(boolean roundRobin, String distributionRat
* @return the builder
*/
public LoadBalanceDefinition circuitBreaker(int threshold, long halfOpenAfter, Class<?>... exceptions) {
CircuitBreakerLoadBalancer breakerLoadBalancer = new CircuitBreakerLoadBalancer(Arrays.asList(exceptions));
breakerLoadBalancer.setThreshold(threshold);
breakerLoadBalancer.setHalfOpenAfter(halfOpenAfter);

setLoadBalancerType(new LoadBalancerDefinition(breakerLoadBalancer));
CircuitBreakerLoadBalancerDefinition def = new CircuitBreakerLoadBalancerDefinition();
def.setExceptionTypes(Arrays.asList(exceptions));
def.setThreshold(threshold);
def.setHalfOpenAfter(halfOpenAfter);
setLoadBalancerType(def);
return this;
}

Expand All @@ -249,20 +241,11 @@ public LoadBalanceDefinition circuitBreaker(int threshold, long halfOpenAfter, C
* @return the builder
*/
public LoadBalanceDefinition weighted(boolean roundRobin, String distributionRatio, String distributionRatioDelimiter) {
WeightedLoadBalancer weighted;
List<Integer> distributionRatioList = new ArrayList<Integer>();

String[] ratios = distributionRatio.split(distributionRatioDelimiter);
for (String ratio : ratios) {
distributionRatioList.add(new Integer(ratio.trim()));
}

if (!roundRobin) {
weighted = new WeightedRandomLoadBalancer(distributionRatioList);
} else {
weighted = new WeightedRoundRobinLoadBalancer(distributionRatioList);
}
setLoadBalancerType(new LoadBalancerDefinition(weighted));
WeightedLoadBalancerDefinition def = new WeightedLoadBalancerDefinition();
def.setRoundRobin(roundRobin);
def.setDistributionRatio(distributionRatio);
def.setDistributionRatioDelimiter(distributionRatioDelimiter);
setLoadBalancerType(def);
return this;
}

Expand All @@ -272,7 +255,7 @@ public LoadBalanceDefinition weighted(boolean roundRobin, String distributionRat
* @return the builder
*/
public LoadBalanceDefinition roundRobin() {
setLoadBalancerType(new LoadBalancerDefinition(new RoundRobinLoadBalancer()));
setLoadBalancerType(new RoundRobinLoadBalancerDefinition());
return this;
}

Expand All @@ -282,7 +265,7 @@ public LoadBalanceDefinition roundRobin() {
* @return the builder
*/
public LoadBalanceDefinition random() {
setLoadBalancerType(new LoadBalancerDefinition(new RandomLoadBalancer()));
setLoadBalancerType(new RandomLoadBalancerDefinition());
return this;
}

Expand All @@ -306,7 +289,9 @@ public LoadBalanceDefinition custom(String ref) {
* @return the builder
*/
public LoadBalanceDefinition sticky(Expression correlationExpression) {
setLoadBalancerType(new LoadBalancerDefinition(new StickyLoadBalancer(correlationExpression)));
StickyLoadBalancerDefinition def = new StickyLoadBalancerDefinition();
def.setCorrelationExpression(new ExpressionSubElementDefinition(correlationExpression));
setLoadBalancerType(def);
return this;
}

Expand All @@ -316,7 +301,7 @@ public LoadBalanceDefinition sticky(Expression correlationExpression) {
* @return the builder
*/
public LoadBalanceDefinition topic() {
setLoadBalancerType(new LoadBalancerDefinition(new TopicLoadBalancer()));
setLoadBalancerType(new TopicLoadBalancerDefinition());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@
*/
package org.apache.camel.model;

import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

import org.apache.camel.AsyncCallback;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.processor.loadbalancer.LoadBalancer;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.RouteContext;
Expand All @@ -37,7 +33,7 @@
@Metadata(label = "eip,routing")
@XmlType(name = "loadBalancer")
@XmlAccessorType(XmlAccessType.FIELD)
public class LoadBalancerDefinition extends IdentifiedType implements LoadBalancer {
public class LoadBalancerDefinition extends IdentifiedType {
@XmlTransient
private LoadBalancer loadBalancer;
@XmlTransient
Expand Down Expand Up @@ -108,41 +104,6 @@ protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
return null;
}


public void addProcessor(Processor processor) {
ObjectHelper.notNull(loadBalancer, "loadBalancer", this);
loadBalancer.addProcessor(processor);
}

public List<Processor> getProcessors() {
ObjectHelper.notNull(loadBalancer, "loadBalancer", this);
return loadBalancer.getProcessors();
}

public void removeProcessor(Processor processor) {
ObjectHelper.notNull(loadBalancer, "loadBalancer", this);
loadBalancer.removeProcessor(processor);
}

public void process(Exchange exchange) throws Exception {
ObjectHelper.notNull(loadBalancer, "loadBalancer", this);
loadBalancer.process(exchange);
}

public boolean process(Exchange exchange, final AsyncCallback callback) {
ObjectHelper.notNull(loadBalancer, "loadBalancer");
return loadBalancer.process(exchange, new AsyncCallback() {
public void done(boolean doneSync) {
// only handle the async case
if (doneSync) {
return;
} else {
callback.done(false);
}
}
});
}

@Override
public String toString() {
if (loadBalancer != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.apache.camel.model.LoadBalancerDefinition;
import org.apache.camel.processor.loadbalancer.CircuitBreakerLoadBalancer;
Expand All @@ -44,6 +45,8 @@
@XmlRootElement(name = "circuitBreaker")
@XmlAccessorType(XmlAccessType.FIELD)
public class CircuitBreakerLoadBalancerDefinition extends LoadBalancerDefinition {
@XmlTransient
private List<Class<?>> exceptionTypes = new ArrayList<Class<?>>();
@XmlElement(name = "exception")
private List<String> exceptions = new ArrayList<String>();
@XmlAttribute
Expand All @@ -58,8 +61,10 @@ public CircuitBreakerLoadBalancerDefinition() {
protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
CircuitBreakerLoadBalancer answer;

if (!exceptions.isEmpty()) {
List<Class<?>> classes = new ArrayList<Class<?>>();
List<Class<?>> classes = new ArrayList<Class<?>>();
if (!exceptionTypes.isEmpty()) {
classes.addAll(exceptionTypes);
} else if (!exceptions.isEmpty()) {
for (String name : exceptions) {
Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(name);
if (type == null) {
Expand All @@ -70,9 +75,11 @@ protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
}
classes.add(type);
}
answer = new CircuitBreakerLoadBalancer(classes);
} else {
}
if (classes.isEmpty()) {
answer = new CircuitBreakerLoadBalancer();
} else {
answer = new CircuitBreakerLoadBalancer(classes);
}

if (getHalfOpenAfter() != null) {
Expand Down Expand Up @@ -118,6 +125,18 @@ public void setExceptions(List<String> exceptions) {
this.exceptions = exceptions;
}

public List<Class<?>> getExceptionTypes() {
return exceptionTypes;
}

/**
* A list of specific exceptions to monitor.
* If no exceptions is configured then all exceptions is monitored
*/
public void setExceptionTypes(List<Class<?>> exceptionTypes) {
this.exceptionTypes = exceptionTypes;
}

@Override
public String toString() {
return "CircuitBreakerLoadBalancer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.apache.camel.model.LoadBalancerDefinition;
import org.apache.camel.processor.loadbalancer.FailOverLoadBalancer;
import org.apache.camel.processor.loadbalancer.LoadBalancer;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.RouteContext;
import org.apache.camel.util.ObjectHelper;

/**
* Failover load balancer
Expand All @@ -42,6 +44,8 @@
@XmlRootElement(name = "failover")
@XmlAccessorType(XmlAccessType.FIELD)
public class FailoverLoadBalancerDefinition extends LoadBalancerDefinition {
@XmlTransient
private List<Class<?>> exceptionTypes = new ArrayList<Class<?>>();
@XmlElement(name = "exception")
private List<String> exceptions = new ArrayList<String>();
@XmlAttribute
Expand All @@ -56,18 +60,25 @@ public FailoverLoadBalancerDefinition() {
protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
FailOverLoadBalancer answer;

if (!exceptions.isEmpty()) {
List<Class<?>> classes = new ArrayList<Class<?>>();
List<Class<?>> classes = new ArrayList<Class<?>>();
if (!exceptionTypes.isEmpty()) {
classes.addAll(exceptionTypes);
} else if (!exceptions.isEmpty()) {
for (String name : exceptions) {
Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(name);
if (type == null) {
throw new IllegalArgumentException("Cannot find class: " + name + " in the classpath");
}
if (!ObjectHelper.isAssignableFrom(Throwable.class, type)) {
throw new IllegalArgumentException("Class is not an instance of Throwable: " + type);
}
classes.add(type);
}
answer = new FailOverLoadBalancer(classes);
} else {
}
if (classes.isEmpty()) {
answer = new FailOverLoadBalancer();
} else {
answer = new FailOverLoadBalancer(classes);
}

if (getMaximumFailoverAttempts() != null) {
Expand All @@ -92,6 +103,18 @@ public void setExceptions(List<String> exceptions) {
this.exceptions = exceptions;
}

public List<Class<?>> getExceptionTypes() {
return exceptionTypes;
}

/**
* A list of specific exceptions to monitor.
* If no exceptions is configured then all exceptions is monitored
*/
public void setExceptionTypes(List<Class<?>> exceptionTypes) {
this.exceptionTypes = exceptionTypes;
}

public Boolean getRoundRobin() {
return roundRobin;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void configure() throws Exception {
});
fail("Should have thrown an exception");
} catch (IllegalArgumentException e) {
assertEquals("Loadbalancer already configured to: FailoverLoadBalancer[[]]. Cannot set it to: RoundRobinLoadBalancer", e.getMessage());
assertEquals("Loadbalancer already configured to: FailoverLoadBalancer. Cannot set it to: RoundRobinLoadBalancer", e.getMessage());
}
}

Expand All @@ -57,7 +57,7 @@ public void configure() throws Exception {
});
fail("Should have thrown an exception");
} catch (IllegalArgumentException e) {
assertEquals("Loadbalancer already configured to: FailoverLoadBalancer[[]]. Cannot set it to: RandomLoadBalancer", e.getMessage());
assertEquals("Loadbalancer already configured to: FailoverLoadBalancer. Cannot set it to: RandomLoadBalancer", e.getMessage());
}
}

Expand All @@ -73,7 +73,7 @@ public void configure() throws Exception {
});
fail("Should have thrown an exception");
} catch (IllegalArgumentException e) {
assertEquals("Loadbalancer already configured to: RandomLoadBalancer. Cannot set it to: FailoverLoadBalancer[[]]", e.getMessage());
assertEquals("Loadbalancer already configured to: RandomLoadBalancer. Cannot set it to: FailoverLoadBalancer", e.getMessage());
}
}
}

0 comments on commit ea6d2fc

Please sign in to comment.