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