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.AsyncCallback;
020import org.apache.camel.Exchange;
021import org.apache.camel.impl.DefaultAsyncProducer;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025/**
026 * The direct producer.
027 *
028 * @version 
029 */
030public class DirectProducer extends DefaultAsyncProducer {
031    private static final transient Logger LOG = LoggerFactory.getLogger(DirectProducer.class);
032    private final DirectEndpoint endpoint;
033
034    public DirectProducer(DirectEndpoint endpoint) {
035        super(endpoint);
036        this.endpoint = endpoint;
037    }
038
039    public void process(Exchange exchange) throws Exception {
040        if (endpoint.getConsumer() == null) {
041            if (endpoint.isFailIfNoConsumers()) {
042                throw new DirectConsumerNotAvailableException("No consumers available on endpoint: " + endpoint, exchange);
043            } else {
044                LOG.debug("message ignored, no consumers available on endpoint: {}", endpoint);
045            }
046        } else {
047            endpoint.getConsumer().getProcessor().process(exchange);
048        }
049    }
050
051    public boolean process(Exchange exchange, AsyncCallback callback) {
052        if (endpoint.getConsumer() == null) {
053            if (endpoint.isFailIfNoConsumers()) {
054                // indicate its done synchronously
055                exchange.setException(new DirectConsumerNotAvailableException("No consumers available on endpoint: " + endpoint, exchange));
056            } else {
057                LOG.debug("message ignored, no consumers available on endpoint: {}", endpoint);
058            }
059            callback.done(true);
060            return true;
061        } else {
062            return endpoint.getConsumer().getAsyncProcessor().process(exchange, callback);
063        }
064    }
065
066}