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.HashMap;
020 import java.util.Map;
021 import java.util.concurrent.BlockingQueue;
022 import java.util.concurrent.LinkedBlockingQueue;
023
024 import org.apache.camel.Endpoint;
025 import org.apache.camel.Exchange;
026 import org.apache.camel.impl.DefaultComponent;
027
028 /**
029 * An implementation of the <a href="http://camel.apache.org/seda.html">SEDA components</a>
030 * for asynchronous SEDA exchanges on a {@link BlockingQueue} within a CamelContext
031 *
032 * @version $Revision: 795369 $
033 */
034 public class SedaComponent extends DefaultComponent {
035
036 private final Map<String, BlockingQueue<Exchange>> queues = new HashMap<String, BlockingQueue<Exchange>>();
037
038 public synchronized BlockingQueue<Exchange> createQueue(String uri, Map parameters) {
039 String key = getQueueKey(uri);
040
041 if (queues.containsKey(key)) {
042 return queues.get(key);
043 }
044
045 // create queue
046 int size = getAndRemoveParameter(parameters, "size", Integer.class, 1000);
047 BlockingQueue<Exchange> queue = new LinkedBlockingQueue<Exchange>(size);
048 queues.put(key, queue);
049 return queue;
050 }
051
052 @Override
053 protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
054 int consumers = getAndRemoveParameter(parameters, "concurrentConsumers", Integer.class, 1);
055 SedaEndpoint answer = new SedaEndpoint(uri, this, createQueue(uri, parameters), consumers);
056 answer.configureProperties(parameters);
057 return answer;
058 }
059
060 protected String getQueueKey(String uri) {
061 if (uri.contains("?")) {
062 // strip parameters
063 uri = uri.substring(0, uri.indexOf('?'));
064 }
065 return uri;
066 }
067
068 @Override
069 protected void doStop() throws Exception {
070 queues.clear();
071 super.doStop();
072 }
073
074 }