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.List;
020
021 import org.apache.camel.Consumer;
022 import org.apache.camel.Endpoint;
023 import org.apache.camel.Navigate;
024 import org.apache.camel.Processor;
025 import org.apache.camel.Service;
026 import org.apache.camel.SuspendableService;
027 import org.apache.camel.processor.DelegateAsyncProcessor;
028 import org.apache.camel.spi.RouteContext;
029
030 /**
031 * A {@link DefaultRoute} which starts with an
032 * <a href="http://camel.apache.org/event-driven-consumer.html">Event Driven Consumer</a>
033 * <p/>
034 * Use the API from {@link org.apache.camel.CamelContext} to control the lifecycle of a route,
035 * such as starting and stopping using the {@link org.apache.camel.CamelContext#startRoute(String)}
036 * and {@link org.apache.camel.CamelContext#stopRoute(String)} methods.
037 *
038 * @version
039 */
040 public class EventDrivenConsumerRoute extends DefaultRoute {
041 private final Processor processor;
042 private Consumer consumer;
043
044 public EventDrivenConsumerRoute(RouteContext routeContext, Endpoint endpoint, Processor processor) {
045 super(routeContext, endpoint);
046 this.processor = processor;
047 }
048
049 @Override
050 public String toString() {
051 return "EventDrivenConsumerRoute[" + getEndpoint() + " -> " + processor + "]";
052 }
053
054 public Processor getProcessor() {
055 return processor;
056 }
057
058 /**
059 * Factory method to lazily create the complete list of services required for this route
060 * such as adding the processor or consumer
061 */
062 @Override
063 protected void addServices(List<Service> services) throws Exception {
064 Endpoint endpoint = getEndpoint();
065 consumer = endpoint.createConsumer(processor);
066 if (consumer != null) {
067 services.add(consumer);
068 }
069 Processor processor = getProcessor();
070 if (processor instanceof Service) {
071 services.add((Service)processor);
072 }
073 }
074
075 @SuppressWarnings("unchecked")
076 public Navigate<Processor> navigate() {
077 Processor answer = getProcessor();
078
079 // we do not want to navigate the instrument and inflight processors
080 // which is the first 2 delegate async processors, so skip them
081 // skip the instrumentation processor if this route was wrapped by one
082 if (answer instanceof DelegateAsyncProcessor) {
083 answer = ((DelegateAsyncProcessor) answer).getProcessor();
084 if (answer instanceof DelegateAsyncProcessor) {
085 answer = ((DelegateAsyncProcessor) answer).getProcessor();
086 }
087 }
088
089 if (answer instanceof Navigate) {
090 return (Navigate<Processor>) answer;
091 }
092 return null;
093 }
094
095 public Consumer getConsumer() {
096 return consumer;
097 }
098
099 public boolean supportsSuspension() {
100 return consumer instanceof SuspendableService;
101 }
102 }