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 @Override 040 protected void doStart() throws Exception { 041 super.doStart(); 042 endpoint.addProducer(this); 043 } 044 045 @Override 046 protected void doStop() throws Exception { 047 endpoint.removeProducer(this); 048 super.doStop(); 049 } 050 051 public void process(Exchange exchange) throws Exception { 052 DirectConsumer consumer = endpoint.getConsumer(); 053 if (consumer == null) { 054 if (endpoint.isFailIfNoConsumers()) { 055 throw new DirectConsumerNotAvailableException("No consumers available on endpoint: " + endpoint, exchange); 056 } else { 057 LOG.debug("message ignored, no consumers available on endpoint: {}", endpoint); 058 } 059 } else { 060 consumer.getProcessor().process(exchange); 061 } 062 } 063 064 public boolean process(Exchange exchange, AsyncCallback callback) { 065 try { 066 DirectConsumer consumer = endpoint.getConsumer(); 067 if (consumer == null) { 068 if (endpoint.isFailIfNoConsumers()) { 069 exchange.setException(new DirectConsumerNotAvailableException("No consumers available on endpoint: " + endpoint, exchange)); 070 } else { 071 LOG.debug("message ignored, no consumers available on endpoint: {}", endpoint); 072 } 073 callback.done(true); 074 return true; 075 } else { 076 return consumer.getAsyncProcessor().process(exchange, callback); 077 } 078 } catch (Exception e) { 079 exchange.setException(e); 080 callback.done(true); 081 return true; 082 } 083 } 084 085}