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.impl.DefaultEndpoint;
026 import org.apache.camel.impl.DefaultExchange;
027 import org.apache.camel.impl.DefaultProducer;
028
029 /**
030 * Represents a queue endpoint that uses a {@link BlockingQueue}
031 * object to process inbound exchanges.
032 *
033 * @org.apache.xbean.XBean
034 * @version $Revision: 519973 $
035 */
036 public class QueueEndpoint<E extends Exchange> extends DefaultEndpoint<E> {
037 private BlockingQueue<E> queue;
038
039 public QueueEndpoint(String uri, QueueComponent<E> component) {
040 super(uri, component);
041 this.queue = component.createQueue();
042 }
043
044 public Producer<E> createProducer() throws Exception {
045 return new DefaultProducer(this) {
046 public void process(Exchange exchange) {
047 queue.add(toExchangeType(exchange));
048 }
049 };
050 }
051
052 public Consumer<E> createConsumer(Processor processor) throws Exception {
053 return new QueueEndpointConsumer<E>(this, processor);
054 }
055
056 public E createExchange() {
057 // How can we create a specific Exchange if we are generic??
058 // perhaps it would be better if we did not implement this.
059 return (E) new DefaultExchange(getContext());
060 }
061
062 public BlockingQueue<E> getQueue() {
063 return queue;
064 }
065
066 public boolean isSingleton() {
067 return true;
068 }
069
070
071 }