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.impl;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.Endpoint;
021import org.apache.camel.FailedToCreateConsumerException;
022import org.apache.camel.IsSingleton;
023import org.apache.camel.PollingConsumer;
024import org.apache.camel.util.ObjectHelper;
025import org.apache.camel.util.ServiceHelper;
026
027/**
028 * A {@link ConsumerCache} which is always empty and does not cache any {@link org.apache.camel.Consumer}s.
029 */
030public class EmptyConsumerCache extends ConsumerCache {
031
032    public EmptyConsumerCache(Object source, CamelContext camelContext) {
033        super(source, camelContext, 0);
034    }
035
036    @Override
037    public PollingConsumer acquirePollingConsumer(Endpoint endpoint) {
038        // always create a new consumer
039        PollingConsumer answer;
040        try {
041            answer = endpoint.createPollingConsumer();
042            boolean singleton = true;
043            if (answer instanceof IsSingleton) {
044                singleton = ((IsSingleton) answer).isSingleton();
045            }
046            if (getCamelContext().isStartingRoutes() && singleton) {
047                // if we are currently starting a route, then add as service and enlist in JMX
048                // - but do not enlist non-singletons in JMX
049                // - note addService will also start the service
050                getCamelContext().addService(answer);
051            } else {
052                // must then start service so producer is ready to be used
053                ServiceHelper.startService(answer);
054            }
055        } catch (Exception e) {
056            throw new FailedToCreateConsumerException(endpoint, e);
057        }
058        return answer;
059    }
060
061    @Override
062    public void releasePollingConsumer(Endpoint endpoint, PollingConsumer pollingConsumer) {
063        // stop and shutdown the consumer as its not cache or reused
064        try {
065            ServiceHelper.stopAndShutdownService(pollingConsumer);
066        } catch (Exception e) {
067            throw ObjectHelper.wrapRuntimeCamelException(e);
068        }
069    }
070
071    @Override
072    public String toString() {
073        return "EmptyConsumerCache for source: " + getSource();
074    }
075
076}