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.impl.RouteContext; 033 import org.apache.camel.model.loadbalancer.LoadBalancerType; 034 import org.apache.camel.model.loadbalancer.RandomLoadBalanceStrategy; 035 import org.apache.camel.model.loadbalancer.RoundRobinLoadBalanceStrategy; 036 import org.apache.camel.model.loadbalancer.StickyLoadBalanceStrategy; 037 import org.apache.camel.model.loadbalancer.TopicLoadBalanceStrategy; 038 import org.apache.camel.processor.SendProcessor; 039 import org.apache.camel.processor.loadbalancer.LoadBalancer; 040 import org.apache.camel.processor.loadbalancer.RandomLoadBalancer; 041 import org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer; 042 import org.apache.camel.processor.loadbalancer.StickyLoadBalancer; 043 import org.apache.camel.processor.loadbalancer.TopicLoadBalancer; 044 import org.apache.camel.util.CollectionStringBuffer; 045 046 @XmlRootElement(name = "loadBalance") 047 @XmlAccessorType(XmlAccessType.FIELD) 048 public class LoadBalanceType extends OutputType<LoadBalanceType> { 049 @XmlAttribute(required = false) 050 private String ref; 051 052 @XmlElements({ 053 @XmlElement(required = false, name = "roundRobin", type = RoundRobinLoadBalanceStrategy.class), 054 @XmlElement(required = false, name = "random", type = RandomLoadBalanceStrategy.class), 055 @XmlElement(required = false, name = "sticky", type = StickyLoadBalanceStrategy.class), 056 @XmlElement(required = false, name = "topic", type = TopicLoadBalanceStrategy.class)} 057 ) 058 private LoadBalancerType loadBalancerType; 059 060 061 062 public LoadBalanceType() { 063 064 } 065 066 public String getRef() { 067 return ref; 068 } 069 070 public void setRef(String ref) { 071 this.ref = ref; 072 } 073 074 public LoadBalancerType getLoadBalancerType() { 075 return loadBalancerType; 076 } 077 078 public void setLoadBalancerType(LoadBalancerType loadbalancer) { 079 loadBalancerType = loadbalancer; 080 } 081 082 protected Processor createOutputsProcessor(RouteContext routeContext, Collection<ProcessorType<?>> outputs) 083 throws Exception { 084 085 LoadBalancer loadBalancer = LoadBalancerType.getLoadBalancer(routeContext, loadBalancerType, ref); 086 087 for (ProcessorType processorType : outputs) { 088 //The outputs should be the SendProcessor 089 SendProcessor processor = (SendProcessor) processorType.createProcessor(routeContext); 090 091 loadBalancer.addProcessor(processor); 092 } 093 return loadBalancer; 094 } 095 096 // when this method will be called 097 @Override 098 public Processor createProcessor(RouteContext routeContext) throws Exception { 099 100 LoadBalancer loadBalancer = LoadBalancerType.getLoadBalancer(routeContext, loadBalancerType, ref); 101 for (ProcessorType processorType : getOutputs()) { 102 //The outputs should be the SendProcessor 103 SendProcessor processor = (SendProcessor) processorType.createProcessor(routeContext); 104 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 139 @Override 140 public String getLabel() { 141 CollectionStringBuffer buffer = new CollectionStringBuffer(); 142 List<ProcessorType<?>> list = getOutputs(); 143 for (ProcessorType<?> processorType : list) { 144 buffer.append(processorType.getLabel()); 145 } 146 return buffer.toString(); 147 } 148 149 @Override 150 public String toString() { 151 String result; 152 if (loadBalancerType != null) { 153 result = "LoadBalanceType[" + loadBalancerType + ", "; 154 } else { 155 result = "LoadBalanceType[" + ref + ", "; 156 } 157 result = result + getOutputs() + "]"; 158 return result; 159 } 160 161 162 163 164 165 166 }