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 */ 017package org.apache.camel.model.loadbalancer; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlElement; 022import javax.xml.bind.annotation.XmlRootElement; 023 024import org.apache.camel.model.ExpressionSubElementDefinition; 025import org.apache.camel.model.LoadBalancerDefinition; 026import org.apache.camel.processor.loadbalancer.LoadBalancer; 027import org.apache.camel.processor.loadbalancer.StickyLoadBalancer; 028import org.apache.camel.spi.Metadata; 029import org.apache.camel.spi.RouteContext; 030 031/** 032 * Sticky load balancer 033 * 034 * Sticky load balancing using an Expression to calculate a correlation key to perform the sticky load balancing; 035 * rather like jsessionid in the web or JMSXGroupID in JMS. 036 */ 037@Metadata(label = "configuration,loadbalance") 038@XmlRootElement(name = "sticky") 039@XmlAccessorType(XmlAccessType.FIELD) 040public class StickyLoadBalancerDefinition extends LoadBalancerDefinition { 041 @XmlElement(name = "correlationExpression", required = true) 042 private ExpressionSubElementDefinition correlationExpression; 043 044 public StickyLoadBalancerDefinition() { 045 } 046 047 @Override 048 protected LoadBalancer createLoadBalancer(RouteContext routeContext) { 049 return new StickyLoadBalancer(correlationExpression.createExpression(routeContext)); 050 } 051 052 public ExpressionSubElementDefinition getCorrelationExpression() { 053 return correlationExpression; 054 } 055 056 /** 057 * The correlation expression to use to calculate the correlation key 058 */ 059 public void setCorrelationExpression(ExpressionSubElementDefinition correlationExpression) { 060 this.correlationExpression = correlationExpression; 061 } 062 063 @Override 064 public String toString() { 065 return "StickyLoadBalancer[" + correlationExpression + "]"; 066 } 067}