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