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.Future; 020 021 import org.apache.camel.AsyncCallback; 022 import org.apache.camel.AsyncProcessor; 023 import org.apache.camel.Consumer; 024 import org.apache.camel.Endpoint; 025 import org.apache.camel.Exchange; 026 import org.apache.camel.Processor; 027 import org.apache.camel.impl.converter.AsyncProcessorTypeConverter; 028 import org.apache.camel.spi.ExceptionHandler; 029 import org.apache.camel.util.ServiceHelper; 030 031 /** 032 * @version $Revision: 36635 $ 033 */ 034 public class DefaultConsumer<E extends Exchange> extends ServiceSupport implements Consumer<E> { 035 private Endpoint<E> endpoint; 036 private Processor processor; 037 private AsyncProcessor asyncProcessor; 038 private ExceptionHandler exceptionHandler; 039 040 public DefaultConsumer(Endpoint<E> endpoint, Processor processor) { 041 this.endpoint = endpoint; 042 this.processor = processor; 043 } 044 045 @Override 046 public String toString() { 047 return "Consumer on " + endpoint; 048 } 049 050 public Endpoint<E> getEndpoint() { 051 return endpoint; 052 } 053 054 public Processor getProcessor() { 055 return processor; 056 } 057 058 /** 059 * Provides an {@link AsyncProcessor} interface to the configured 060 * processor on the consumer. If the processor does not implement 061 * the interface, it will be adapted so that it does. 062 */ 063 public AsyncProcessor getAsyncProcessor() { 064 if (asyncProcessor == null) { 065 asyncProcessor = AsyncProcessorTypeConverter.convert(processor); 066 } 067 return asyncProcessor; 068 } 069 070 public ExceptionHandler getExceptionHandler() { 071 if (exceptionHandler == null) { 072 exceptionHandler = new LoggingExceptionHandler(getClass()); 073 } 074 return exceptionHandler; 075 } 076 077 public void setExceptionHandler(ExceptionHandler exceptionHandler) { 078 this.exceptionHandler = exceptionHandler; 079 } 080 081 protected void doStop() throws Exception { 082 ServiceHelper.stopServices(processor); 083 } 084 085 protected void doStart() throws Exception { 086 ServiceHelper.startServices(processor); 087 } 088 089 /** 090 * Handles the given exception using the {@link #getExceptionHandler()} 091 * 092 * @param t the exception to handle 093 */ 094 protected void handleException(Throwable t) { 095 getExceptionHandler().handleException(t); 096 } 097 }