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 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 LoadBalancer loadBalancer = LoadBalancerType.getLoadBalancer(routeContext, loadBalancerType, ref); 085 for (ProcessorType processorType : outputs) { 086 // The outputs should be the SendProcessor 087 SendProcessor processor = (SendProcessor) processorType.createProcessor(routeContext); 088 loadBalancer.addProcessor(processor); 089 } 090 return loadBalancer; 091 } 092 093 // when this method will be called 094 @Override 095 public Processor createProcessor(RouteContext routeContext) throws Exception { 096 LoadBalancer loadBalancer = LoadBalancerType.getLoadBalancer(routeContext, loadBalancerType, ref); 097 for (ProcessorType processorType : getOutputs()) { 098 // The outputs should be the SendProcessor 099 SendProcessor processor = (SendProcessor) processorType.createProcessor(routeContext); 100 loadBalancer.addProcessor(processor); 101 } 102 103 return loadBalancer; 104 } 105 106 // Fluent API 107 // ------------------------------------------------------------------------- 108 public LoadBalanceType setLoadBalancer(LoadBalancer loadBalancer) { 109 loadBalancerType = new LoadBalancerType(loadBalancer); 110 return this; 111 } 112 113 public LoadBalanceType roundRobin() { 114 loadBalancerType = new LoadBalancerType(new RoundRobinLoadBalancer()); 115 return this; 116 } 117 118 public LoadBalanceType random() { 119 loadBalancerType = new LoadBalancerType(new RandomLoadBalancer()); 120 return this; 121 } 122 123 public LoadBalanceType sticky(Expression<Exchange> correlationExpression) { 124 loadBalancerType = new LoadBalancerType(new StickyLoadBalancer(correlationExpression)); 125 return this; 126 } 127 128 public LoadBalanceType topic() { 129 loadBalancerType = new LoadBalancerType(new TopicLoadBalancer()); 130 return this; 131 } 132 133 @Override 134 public String getLabel() { 135 CollectionStringBuffer buffer = new CollectionStringBuffer(); 136 List<ProcessorType<?>> list = getOutputs(); 137 for (ProcessorType<?> processorType : list) { 138 buffer.append(processorType.getLabel()); 139 } 140 return buffer.toString(); 141 } 142 143 @Override 144 public String toString() { 145 String result; 146 if (loadBalancerType != null) { 147 result = "LoadBalanceType[" + loadBalancerType + ", "; 148 } else { 149 result = "LoadBalanceType[" + ref + ", "; 150 } 151 result = result + getOutputs() + "]"; 152 return result; 153 } 154 155 }