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.component.direct;
018
019 import org.apache.camel.Endpoint;
020 import org.apache.camel.Processor;
021 import org.apache.camel.ShutdownRunningTask;
022 import org.apache.camel.SuspendableService;
023 import org.apache.camel.impl.DefaultConsumer;
024 import org.apache.camel.spi.ShutdownAware;
025
026 /**
027 * The direct consumer.
028 *
029 * @version
030 */
031 public class DirectConsumer extends DefaultConsumer implements ShutdownAware, SuspendableService {
032
033 private DirectEndpoint endpoint;
034
035 public DirectConsumer(Endpoint endpoint, Processor processor) {
036 super(endpoint, processor);
037 this.endpoint = (DirectEndpoint) endpoint;
038 }
039
040 @Override
041 protected void doStart() throws Exception {
042 // add consumer to endpoint
043 boolean existing = this == endpoint.getConsumer();
044 if (!existing && endpoint.hasConsumer(this)) {
045 throw new IllegalArgumentException("Cannot add a 2nd consumer to the same endpoint. Endpoint " + endpoint + " only allows one consumer.");
046 }
047 if (!existing) {
048 endpoint.addConsumer(this);
049 }
050 }
051
052 @Override
053 protected void doStop() throws Exception {
054 endpoint.removeConsumer(this);
055 }
056
057 @Override
058 protected void doSuspend() throws Exception {
059 endpoint.removeConsumer(this);
060 }
061
062 @Override
063 protected void doResume() throws Exception {
064 // resume by using the start logic
065 doStart();
066 }
067
068 public boolean deferShutdown(ShutdownRunningTask shutdownRunningTask) {
069 // deny stopping on shutdown as we want direct consumers to run in case some other queues
070 // depend on this consumer to run, so it can complete its exchanges
071 return true;
072 }
073
074 public int getPendingExchangesSize() {
075 // return 0 as we do not have an internal memory queue with a variable size
076 // of inflight messages.
077 return 0;
078 }
079
080 public void prepareShutdown(boolean forced) {
081 // noop
082 }
083 }