/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.support;

import org.apache.camel.AsyncCallback;
import org.apache.camel.AsyncProducer;
import org.apache.camel.DelegateProcessor;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.support.service.ServiceHelper;

/**
 * A {@link org.apache.camel.Producer} which is created and started lazy, on the first message processed.
 */
public final class LazyStartProducer extends DefaultAsyncProducer implements DelegateProcessor {

    private AsyncProducer delegate;

    public LazyStartProducer(Endpoint endpoint) {
        super(endpoint);
    }

    @Override
    public boolean process(Exchange exchange, AsyncCallback callback) {
        try {
            // create and start producer lazy
            if (delegate == null) {
                synchronized (lock) {
                    if (delegate == null) {
                        delegate = AsyncProcessorConverterHelper.convert(getEndpoint().createProducer());
                    }
                }
            }
            if (!ServiceHelper.isStarted(delegate)) {
                ServiceHelper.startService(delegate);
            }
        } catch (Throwable e) {
            exchange.setException(e);
            callback.done(true);
            return true;
        }
        return delegate.process(exchange, callback);
    }

    @Override
    public boolean isSingleton() {
        return getEndpoint().isSingleton();
    }

    @Override
    protected void doInit() throws Exception {
        // noop as we dont want to start the delegate but its started on the first message processed
    }

    @Override
    protected void doStart() throws Exception {
        // noop as we dont want to start the delegate but its started on the first message processed
    }

    @Override
    protected void doStop() throws Exception {
        ServiceHelper.stopService(delegate);
    }

    @Override
    protected void doSuspend() throws Exception {
        ServiceHelper.suspendService(delegate);
    }

    @Override
    protected void doResume() throws Exception {
        ServiceHelper.resumeService(delegate);
    }

    @Override
    protected void doShutdown() throws Exception {
        ServiceHelper.stopAndShutdownService(delegate);
    }

    @Override
    public Processor getProcessor() {
        return delegate;
    }
}

@@ -1335,16 +1335,25 @@
 private 
+volatile 
 AsyncPro
@@ -2240,24 +2240,120 @@
 ngleton() %7B%0A
+        if (delegate != null) %7B%0A            return delegate.isSingleton();%0A        %7D else %7B%0A    
         retu
@@ -2380,24 +2380,34 @@
 ingleton();%0A
+        %7D%0A
     %7D%0A%0A    @

Allow producer to lazy start until first message