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.seda;
018
019 import java.util.ArrayList;
020 import java.util.HashSet;
021 import java.util.List;
022 import java.util.Set;
023 import java.util.concurrent.BlockingQueue;
024 import java.util.concurrent.CopyOnWriteArraySet;
025 import java.util.concurrent.LinkedBlockingQueue;
026
027 import org.apache.camel.Component;
028 import org.apache.camel.Consumer;
029 import org.apache.camel.Exchange;
030 import org.apache.camel.Processor;
031 import org.apache.camel.Producer;
032 import org.apache.camel.impl.DefaultEndpoint;
033 import org.apache.camel.spi.BrowsableEndpoint;
034
035 /**
036 * An implementation of the <a
037 * href="http://camel.apache.org/queue.html">Queue components</a> for
038 * asynchronous SEDA exchanges on a {@link BlockingQueue} within a CamelContext
039 *
040 * @version $Revision: 748032 $
041 */
042 public class SedaEndpoint extends DefaultEndpoint implements BrowsableEndpoint {
043 private BlockingQueue<Exchange> queue;
044 private int size = 1000;
045 private int concurrentConsumers = 1;
046 private Set<SedaProducer> producers = new CopyOnWriteArraySet<SedaProducer>();
047 private Set<SedaConsumer> consumers = new CopyOnWriteArraySet<SedaConsumer>();
048
049 public SedaEndpoint() {
050 }
051
052 public SedaEndpoint(String endpointUri, Component component, BlockingQueue<Exchange> queue) {
053 this(endpointUri, component, queue, 1);
054 }
055
056 public SedaEndpoint(String endpointUri, Component component, BlockingQueue<Exchange> queue, int concurrentConsumers) {
057 super(endpointUri, component);
058 this.queue = queue;
059 this.concurrentConsumers = concurrentConsumers;
060 }
061
062 public SedaEndpoint(String endpointUri, BlockingQueue<Exchange> queue) {
063 this(endpointUri, queue, 1);
064 }
065
066 public SedaEndpoint(String endpointUri, BlockingQueue<Exchange> queue, int concurrentConsumers) {
067 super(endpointUri);
068 this.queue = queue;
069 this.concurrentConsumers = concurrentConsumers;
070 }
071
072 public Producer createProducer() throws Exception {
073 return new SedaProducer(this, getQueue());
074 }
075
076 public Consumer createConsumer(Processor processor) throws Exception {
077 return new SedaConsumer(this, processor);
078 }
079
080 public synchronized BlockingQueue<Exchange> getQueue() {
081 if (queue == null) {
082 queue = new LinkedBlockingQueue<Exchange>(size);
083 }
084 return queue;
085 }
086
087 public void setQueue(BlockingQueue<Exchange> queue) {
088 this.queue = queue;
089 }
090
091 public int getSize() {
092 return size;
093 }
094
095 public void setSize(int size) {
096 this.size = size;
097 }
098
099 public void setConcurrentConsumers(int concurrentConsumers) {
100 this.concurrentConsumers = concurrentConsumers;
101 }
102
103 public int getConcurrentConsumers() {
104 return concurrentConsumers;
105 }
106
107 public boolean isSingleton() {
108 return true;
109 }
110
111 /**
112 * Returns the current pending exchanges
113 */
114 public List<Exchange> getExchanges() {
115 return new ArrayList<Exchange>(getQueue());
116 }
117
118 /**
119 * Returns the current active consumers on this endpoint
120 */
121 public Set<SedaConsumer> getConsumers() {
122 return new HashSet<SedaConsumer>(consumers);
123 }
124
125 /**
126 * Returns the current active producers on this endpoint
127 */
128 public Set<SedaProducer> getProducers() {
129 return new HashSet<SedaProducer>(producers);
130 }
131
132 void onStarted(SedaProducer producer) {
133 producers.add(producer);
134 }
135
136 void onStopped(SedaProducer producer) {
137 producers.remove(producer);
138 }
139
140 void onStarted(SedaConsumer consumer) {
141 consumers.add(consumer);
142 }
143
144 void onStopped(SedaConsumer consumer) {
145 consumers.remove(consumer);
146 }
147 }