001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.model;
018
019 import java.util.Collection;
020 import java.util.List;
021
022 import javax.xml.bind.annotation.XmlAccessType;
023 import javax.xml.bind.annotation.XmlAccessorType;
024 import javax.xml.bind.annotation.XmlAttribute;
025 import javax.xml.bind.annotation.XmlElement;
026 import javax.xml.bind.annotation.XmlElements;
027 import javax.xml.bind.annotation.XmlRootElement;
028
029 import org.apache.camel.Exchange;
030 import org.apache.camel.Expression;
031 import org.apache.camel.Processor;
032 import org.apache.camel.model.loadbalancer.LoadBalancerType;
033 import org.apache.camel.model.loadbalancer.RandomLoadBalanceStrategy;
034 import org.apache.camel.model.loadbalancer.RoundRobinLoadBalanceStrategy;
035 import org.apache.camel.model.loadbalancer.StickyLoadBalanceStrategy;
036 import org.apache.camel.model.loadbalancer.TopicLoadBalanceStrategy;
037 import org.apache.camel.processor.SendProcessor;
038 import org.apache.camel.processor.loadbalancer.LoadBalancer;
039 import org.apache.camel.processor.loadbalancer.RandomLoadBalancer;
040 import org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer;
041 import org.apache.camel.processor.loadbalancer.StickyLoadBalancer;
042 import org.apache.camel.processor.loadbalancer.TopicLoadBalancer;
043 import org.apache.camel.spi.RouteContext;
044 import org.apache.camel.util.CollectionStringBuffer;
045
046 /**
047 * Represents an XML <loadBalance/> element
048 */
049 @XmlRootElement(name = "loadBalance")
050 @XmlAccessorType(XmlAccessType.FIELD)
051 public class LoadBalanceType extends OutputType<LoadBalanceType> {
052 @XmlAttribute(required = false)
053 private String ref;
054
055 @XmlElements({
056 @XmlElement(required = false, name = "roundRobin", type = RoundRobinLoadBalanceStrategy.class),
057 @XmlElement(required = false, name = "random", type = RandomLoadBalanceStrategy.class),
058 @XmlElement(required = false, name = "sticky", type = StickyLoadBalanceStrategy.class),
059 @XmlElement(required = false, name = "topic", type = TopicLoadBalanceStrategy.class)}
060 )
061 private LoadBalancerType loadBalancerType;
062
063 public LoadBalanceType() {
064 }
065
066 @Override
067 public String getShortName() {
068 return "loadbalance";
069 }
070
071 public String getRef() {
072 return ref;
073 }
074
075 public void setRef(String ref) {
076 this.ref = ref;
077 }
078
079 public LoadBalancerType getLoadBalancerType() {
080 return loadBalancerType;
081 }
082
083 public void setLoadBalancerType(LoadBalancerType loadbalancer) {
084 loadBalancerType = loadbalancer;
085 }
086
087 protected Processor createOutputsProcessor(RouteContext routeContext, Collection<ProcessorType<?>> outputs)
088 throws Exception {
089 LoadBalancer loadBalancer = LoadBalancerType.getLoadBalancer(routeContext, loadBalancerType, ref);
090 for (ProcessorType processorType : outputs) {
091 // The outputs should be the SendProcessor
092 SendProcessor processor = (SendProcessor) processorType.createProcessor(routeContext);
093 loadBalancer.addProcessor(processor);
094 }
095 return loadBalancer;
096 }
097
098 // when this method will be called
099 @Override
100 public Processor createProcessor(RouteContext routeContext) throws Exception {
101 LoadBalancer loadBalancer = LoadBalancerType.getLoadBalancer(routeContext, loadBalancerType, ref);
102 for (ProcessorType processorType : getOutputs()) {
103 // The outputs should be the SendProcessor
104 SendProcessor processor = (SendProcessor) processorType.createProcessor(routeContext);
105 loadBalancer.addProcessor(processor);
106 }
107
108 return loadBalancer;
109 }
110
111 // Fluent API
112 // -------------------------------------------------------------------------
113 public LoadBalanceType setLoadBalancer(LoadBalancer loadBalancer) {
114 loadBalancerType = new LoadBalancerType(loadBalancer);
115 return this;
116 }
117
118 public LoadBalanceType roundRobin() {
119 loadBalancerType = new LoadBalancerType(new RoundRobinLoadBalancer());
120 return this;
121 }
122
123 public LoadBalanceType random() {
124 loadBalancerType = new LoadBalancerType(new RandomLoadBalancer());
125 return this;
126 }
127
128 public LoadBalanceType sticky(Expression<Exchange> correlationExpression) {
129 loadBalancerType = new LoadBalancerType(new StickyLoadBalancer(correlationExpression));
130 return this;
131 }
132
133 public LoadBalanceType topic() {
134 loadBalancerType = new LoadBalancerType(new TopicLoadBalancer());
135 return this;
136 }
137
138 @Override
139 public String getLabel() {
140 CollectionStringBuffer buffer = new CollectionStringBuffer();
141 List<ProcessorType<?>> list = getOutputs();
142 for (ProcessorType<?> processorType : list) {
143 buffer.append(processorType.getLabel());
144 }
145 return buffer.toString();
146 }
147
148 @Override
149 public String toString() {
150 String result;
151 if (loadBalancerType != null) {
152 result = "LoadBalanceType[" + loadBalancerType + ", ";
153 } else {
154 result = "LoadBalanceType[" + ref + ", ";
155 }
156 result = result + getOutputs() + "]";
157 return result;
158 }
159
160 }