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;
018
019import java.util.List;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlRootElement;
025
026import org.apache.camel.spi.Metadata;
027import org.apache.camel.util.CollectionStringBuffer;
028
029/**
030 * Route to be executed when Hystrix EIP executes fallback
031 */
032@Metadata(label = "eip,routing,circuitbreaker")
033@XmlRootElement(name = "onFallback")
034@XmlAccessorType(XmlAccessType.FIELD)
035public class OnFallbackDefinition extends OutputDefinition<OnFallbackDefinition> {
036
037    @XmlAttribute
038    @Metadata(label = "command", defaultValue = "false")
039    private Boolean fallbackViaNetwork;
040
041    public OnFallbackDefinition() {
042    }
043
044    @Override
045    public String toString() {
046        if (fallbackViaNetwork != null && fallbackViaNetwork) {
047            return "OnFallbackViaNetwork[" + getOutputs() + "]";
048        } else {
049            return "OnFallback[" + getOutputs() + "]";
050        }
051    }
052
053    @Override
054    public String getShortName() {
055        return "onFallback";
056    }
057
058    @Override
059    public String getLabel() {
060        String name = fallbackViaNetwork != null && fallbackViaNetwork ? "onFallbackViaNetwork" : "onFallback";
061        CollectionStringBuffer buffer = new CollectionStringBuffer(name);
062        buffer.append("[");
063        List<ProcessorDefinition<?>> list = getOutputs();
064        for (ProcessorDefinition<?> type : list) {
065            buffer.append(type.getLabel());
066        }
067        buffer.append("]");
068        return buffer.toString();
069    }
070
071    public Boolean getFallbackViaNetwork() {
072        return fallbackViaNetwork;
073    }
074
075    /**
076     * Whether the fallback goes over the network.
077     * <p/>
078     * If the fallback will go over the network it is another possible point of
079     * failure and so it also needs to be wrapped by a HystrixCommand. It is
080     * important to execute the fallback command on a separate thread-pool,
081     * otherwise if the main command were to become latent and fill the
082     * thread-pool this would prevent the fallback from running if the two
083     * commands share the same pool.
084     */
085    public void setFallbackViaNetwork(Boolean fallbackViaNetwork) {
086        this.fallbackViaNetwork = fallbackViaNetwork;
087    }
088
089    public boolean isFallbackViaNetwork() {
090        // is default false
091        return fallbackViaNetwork != null && fallbackViaNetwork;
092    }
093
094}