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 Logger log = LoggerFactory.getLogger(getClass()); 041 private ExceptionHandler exceptionHandler; 042 043 public void onInit(Route route) { 044 if (exceptionHandler == null) { 045 exceptionHandler = new LoggingExceptionHandler(route.getRouteContext().getCamelContext(), getClass()); 046 } 047 } 048 049 public void onRemove(Route route) { 050 // noop 051 } 052 053 @Override 054 public void onStart(Route route) { 055 // noop 056 } 057 058 @Override 059 public void onStop(Route route) { 060 // noop 061 } 062 063 @Override 064 public void onSuspend(Route route) { 065 // noop 066 } 067 068 @Override 069 public void onResume(Route route) { 070 // noop 071 } 072 073 public void onExchangeBegin(Route route, Exchange exchange) { 074 // noop 075 } 076 077 public void onExchangeDone(Route route, Exchange exchange) { 078 // noop 079 } 080 081 protected boolean startConsumer(Consumer consumer) throws Exception { 082 boolean resumed = ServiceHelper.resumeService(consumer); 083 if (resumed) { 084 log.debug("Resuming consumer {}", consumer); 085 } 086 return resumed; 087 } 088 089 protected boolean stopConsumer(Consumer consumer) throws Exception { 090 boolean suspended = ServiceHelper.suspendService(consumer); 091 if (suspended) { 092 log.debug("Suspended consumer {}", consumer); 093 } 094 return suspended; 095 } 096 097 protected void startRoute(Route route) throws Exception { 098 route.getRouteContext().getCamelContext().startRoute(route.getId()); 099 } 100 101 protected void resumeRoute(Route route) throws Exception { 102 route.getRouteContext().getCamelContext().resumeRoute(route.getId()); 103 } 104 105 protected void suspendRoute(Route route) throws Exception { 106 route.getRouteContext().getCamelContext().suspendRoute(route.getId()); 107 } 108 109 protected void suspendRoute(Route route, long timeout, TimeUnit timeUnit) throws Exception { 110 route.getRouteContext().getCamelContext().suspendRoute(route.getId(), timeout, timeUnit); 111 } 112 113 protected void stopRoute(Route route) throws Exception { 114 route.getRouteContext().getCamelContext().stopRoute(route.getId()); 115 } 116 117 protected void stopRoute(Route route, long timeout, TimeUnit timeUnit) throws Exception { 118 route.getRouteContext().getCamelContext().stopRoute(route.getId(), timeout, timeUnit); 119 } 120 121 /** 122 * Handles the given exception using the {@link #getExceptionHandler()} 123 * 124 * @param t the exception to handle 125 */ 126 protected void handleException(Throwable t) { 127 if (exceptionHandler != null) { 128 exceptionHandler.handleException(t); 129 } 130 } 131 132 @Override 133 protected void doStart() throws Exception { 134 // noop 135 } 136 137 @Override 138 protected void doStop() throws Exception { 139 // noop 140 } 141 142 public ExceptionHandler getExceptionHandler() { 143 return exceptionHandler; 144 } 145 146 public void setExceptionHandler(ExceptionHandler exceptionHandler) { 147 this.exceptionHandler = exceptionHandler; 148 } 149 150 }