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     */
017    package org.apache.camel.component.queue;
018    
019    import java.util.concurrent.BlockingQueue;
020    
021    import org.apache.camel.Consumer;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Processor;
024    import org.apache.camel.Producer;
025    import org.apache.camel.Component;
026    import org.apache.camel.impl.DefaultEndpoint;
027    import org.apache.camel.impl.DefaultExchange;
028    import org.apache.camel.impl.DefaultProducer;
029    
030    /**
031     * An implementation of the <a href="http://activemq.apache.org/camel/queue.html">Queue components</a>
032     * for asynchronous SEDA exchanges on a {@link BlockingQueue} within a CamelContext
033     *
034     * @org.apache.xbean.XBean
035     * @version $Revision: 519973 $
036     */
037    public class QueueEndpoint<E extends Exchange> extends DefaultEndpoint<E> {
038        private BlockingQueue<E> queue;
039    
040        public QueueEndpoint(String endpointUri, Component component, BlockingQueue<E> queue) {
041            super(endpointUri, component);
042            this.queue = queue;
043        }
044    
045        public QueueEndpoint(String uri, QueueComponent<E> component) {
046            this(uri, component, component.createQueue());
047        }
048    
049        public Producer<E> createProducer() throws Exception {
050            return new DefaultProducer(this) {
051                public void process(Exchange exchange) {
052                    queue.add(toExchangeType(exchange));
053                }
054            };
055        }
056    
057        public Consumer<E> createConsumer(Processor processor) throws Exception {
058            return new QueueConsumer<E>(this, processor);
059        }
060    
061        public E createExchange() {
062            // How can we create a specific Exchange if we are generic??
063            // perhaps it would be better if we did not implement this. 
064            return (E) new DefaultExchange(getContext());
065        }
066    
067        public BlockingQueue<E> getQueue() {
068            return queue;
069        }
070        
071            public boolean isSingleton() {
072                    return true;
073            }
074    
075    
076    }