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