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