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.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, Suspendable { 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 super.doStart(); 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 super.doStop(); 062 } 063 064 @Override 065 protected void doSuspend() throws Exception { 066 endpoint.removeConsumer(this); 067 } 068 069 @Override 070 protected void doResume() throws Exception { 071 // resume by using the start logic 072 doStart(); 073 } 074 075 public boolean deferShutdown(ShutdownRunningTask shutdownRunningTask) { 076 // deny stopping on shutdown as we want direct consumers to run in case some other queues 077 // depend on this consumer to run, so it can complete its exchanges 078 return true; 079 } 080 081 public int getPendingExchangesSize() { 082 // return 0 as we do not have an internal memory queue with a variable size 083 // of inflight messages. 084 return 0; 085 } 086 087 public void prepareShutdown(boolean suspendOnly, boolean forced) { 088 // noop 089 } 090}