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 org.apache.camel.AsyncProcessor;
020 import org.apache.camel.Consumer;
021 import org.apache.camel.Endpoint;
022 import org.apache.camel.Processor;
023 import org.apache.camel.spi.ExceptionHandler;
024 import org.apache.camel.support.ServiceSupport;
025 import org.apache.camel.util.AsyncProcessorConverterHelper;
026 import org.apache.camel.util.ServiceHelper;
027 import org.apache.camel.util.URISupport;
028 import org.slf4j.Logger;
029 import org.slf4j.LoggerFactory;
030
031 /**
032 * A default consumer useful for implementation inheritance.
033 *
034 * @version
035 */
036 public class DefaultConsumer extends ServiceSupport implements Consumer {
037 protected final transient Logger log = LoggerFactory.getLogger(getClass());
038 private final Endpoint endpoint;
039 private final Processor processor;
040 private volatile AsyncProcessor asyncProcessor;
041 private ExceptionHandler exceptionHandler;
042
043 public DefaultConsumer(Endpoint endpoint, Processor processor) {
044 this.endpoint = endpoint;
045 this.processor = processor;
046 }
047
048 @Override
049 public String toString() {
050 return "Consumer[" + URISupport.sanitizeUri(endpoint.getEndpointUri()) + "]";
051 }
052
053 public Endpoint getEndpoint() {
054 return endpoint;
055 }
056
057 public Processor getProcessor() {
058 return processor;
059 }
060
061 /**
062 * Provides an {@link org.apache.camel.AsyncProcessor} interface to the configured
063 * processor on the consumer. If the processor does not implement the interface,
064 * it will be adapted so that it does.
065 */
066 public synchronized AsyncProcessor getAsyncProcessor() {
067 if (asyncProcessor == null) {
068 asyncProcessor = AsyncProcessorConverterHelper.convert(processor);
069 }
070 return asyncProcessor;
071 }
072
073 public ExceptionHandler getExceptionHandler() {
074 if (exceptionHandler == null) {
075 exceptionHandler = new LoggingExceptionHandler(getClass());
076 }
077 return exceptionHandler;
078 }
079
080 public void setExceptionHandler(ExceptionHandler exceptionHandler) {
081 this.exceptionHandler = exceptionHandler;
082 }
083
084 protected void doStop() throws Exception {
085 log.debug("Stopping consumer: {}", this);
086 ServiceHelper.stopServices(processor);
087 }
088
089 protected void doStart() throws Exception {
090 log.debug("Starting consumer: {}", this);
091 ServiceHelper.startServices(processor);
092 }
093
094 /**
095 * Handles the given exception using the {@link #getExceptionHandler()}
096 *
097 * @param t the exception to handle
098 */
099 protected void handleException(Throwable t) {
100 Throwable newt = (t == null) ? new IllegalArgumentException("Handling [null] exception") : t;
101 getExceptionHandler().handleException(newt);
102 }
103
104 /**
105 * Handles the given exception using the {@link #getExceptionHandler()}
106 *
107 * @param message additional message about the exception
108 * @param t the exception to handle
109 */
110 protected void handleException(String message, Throwable t) {
111 Throwable newt = (t == null) ? new IllegalArgumentException("Handling [null] exception") : t;
112 getExceptionHandler().handleException(message, newt);
113 }
114 }