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 */ 017 package org.apache.camel.impl; 018 019 import java.util.concurrent.TimeUnit; 020 021 import org.apache.camel.Consumer; 022 import org.apache.camel.Exchange; 023 import org.apache.camel.Route; 024 import org.apache.camel.spi.ExceptionHandler; 025 import org.apache.camel.spi.RoutePolicy; 026 import org.apache.camel.support.ServiceSupport; 027 import org.apache.camel.util.ServiceHelper; 028 import org.slf4j.Logger; 029 import org.slf4j.LoggerFactory; 030 031 /** 032 * A base class for developing custom {@link RoutePolicy} implementations. 033 * 034 * @version 035 */ 036 public abstract class RoutePolicySupport extends ServiceSupport implements RoutePolicy { 037 038 // TODO: Move to support package 039 040 protected final transient Logger log = LoggerFactory.getLogger(getClass()); 041 private ExceptionHandler exceptionHandler; 042 043 public void onInit(Route route) { 044 // noop 045 } 046 047 public void onRemove(Route route) { 048 // noop 049 } 050 051 @Override 052 public void onStart(Route route) { 053 // noop 054 } 055 056 @Override 057 public void onStop(Route route) { 058 // noop 059 } 060 061 @Override 062 public void onSuspend(Route route) { 063 // noop 064 } 065 066 @Override 067 public void onResume(Route route) { 068 // noop 069 } 070 071 public void onExchangeBegin(Route route, Exchange exchange) { 072 // noop 073 } 074 075 public void onExchangeDone(Route route, Exchange exchange) { 076 // noop 077 } 078 079 protected boolean startConsumer(Consumer consumer) throws Exception { 080 boolean resumed = ServiceHelper.resumeService(consumer); 081 if (resumed) { 082 log.debug("Resuming consumer {}", consumer); 083 } 084 return resumed; 085 } 086 087 protected boolean stopConsumer(Consumer consumer) throws Exception { 088 boolean suspended = ServiceHelper.suspendService(consumer); 089 if (suspended) { 090 log.debug("Suspended consumer {}", consumer); 091 } 092 return suspended; 093 } 094 095 protected void startRoute(Route route) throws Exception { 096 route.getRouteContext().getCamelContext().startRoute(route.getId()); 097 } 098 099 protected void resumeRoute(Route route) throws Exception { 100 route.getRouteContext().getCamelContext().resumeRoute(route.getId()); 101 } 102 103 protected void suspendRoute(Route route) throws Exception { 104 route.getRouteContext().getCamelContext().suspendRoute(route.getId()); 105 } 106 107 protected void suspendRoute(Route route, long timeout, TimeUnit timeUnit) throws Exception { 108 route.getRouteContext().getCamelContext().suspendRoute(route.getId(), timeout, timeUnit); 109 } 110 111 protected void stopRoute(Route route) throws Exception { 112 route.getRouteContext().getCamelContext().stopRoute(route.getId()); 113 } 114 115 protected void stopRoute(Route route, long timeout, TimeUnit timeUnit) throws Exception { 116 route.getRouteContext().getCamelContext().stopRoute(route.getId(), timeout, timeUnit); 117 } 118 119 /** 120 * Handles the given exception using the {@link #getExceptionHandler()} 121 * 122 * @param t the exception to handle 123 */ 124 protected void handleException(Throwable t) { 125 getExceptionHandler().handleException(t); 126 } 127 128 @Override 129 protected void doStart() throws Exception { 130 // noop 131 } 132 133 @Override 134 protected void doStop() throws Exception { 135 // noop 136 } 137 138 public ExceptionHandler getExceptionHandler() { 139 if (exceptionHandler == null) { 140 exceptionHandler = new LoggingExceptionHandler(getClass()); 141 } 142 return exceptionHandler; 143 } 144 145 public void setExceptionHandler(ExceptionHandler exceptionHandler) { 146 this.exceptionHandler = exceptionHandler; 147 } 148 149 }