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.FailedToCreateProducerException;
022import org.apache.camel.Producer;
023import org.apache.camel.util.ServiceHelper;
024
025/**
026 * A {@link org.apache.camel.impl.ProducerCache} which is always empty and does not cache any {@link org.apache.camel.Producer}s.
027 */
028public class EmptyProducerCache extends ProducerCache {
029
030    public EmptyProducerCache(Object source, CamelContext camelContext) {
031        super(source, camelContext, 0);
032    }
033
034    @Override
035    public Producer acquireProducer(Endpoint endpoint) {
036        // always create a new producer
037        Producer answer;
038        try {
039            answer = endpoint.createProducer();
040            if (getCamelContext().isStartingRoutes() && answer.isSingleton()) {
041                // if we are currently starting a route, then add as service and enlist in JMX
042                // - but do not enlist non-singletons in JMX
043                // - note addService will also start the service
044                getCamelContext().addService(answer);
045            } else {
046                // must then start service so producer is ready to be used
047                ServiceHelper.startService(answer);
048            }
049        } catch (Exception e) {
050            throw new FailedToCreateProducerException(endpoint, e);
051        }
052        return answer;
053    }
054
055    @Override
056    public void releaseProducer(Endpoint endpoint, Producer producer) throws Exception {
057        // stop and shutdown the producer as its not cache or reused
058        ServiceHelper.stopAndShutdownService(producer);
059    }
060
061    @Override
062    public String toString() {
063        return "EmptyProducerCache for source: " + getSource();
064    }
065
066}