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