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.spi;
018
019 /**
020 * A service pool is like a connection pool but can pool any kind of objects.
021 * <p/>
022 * Services that is capable of being pooled should implement the marker interface
023 * {@link org.apache.camel.ServicePoolAware}.
024 * <p/>
025 * Notice the capacity is <b>per key</b> which means that each key can contain at most
026 * (the capacity) services. The pool can contain an unbounded number of keys.
027 * <p/>
028 * By default the capacity is set to 100.
029 *
030 * @version
031 */
032 public interface ServicePool<Key, Service> {
033
034 /**
035 * Sets the capacity, which is capacity <b>per key</b>.
036 *
037 * @param capacity the capacity per key
038 */
039 void setCapacity(int capacity);
040
041 /**
042 * Gets the capacity per key.
043 *
044 * @return the capacity per key
045 */
046 int getCapacity();
047
048 /**
049 * Adds the given service to the pool and acquires it.
050 *
051 * @param key the key
052 * @param service the service
053 * @return the acquired service, is newer <tt>null</tt>
054 * @throws IllegalStateException if the queue is full (capacity has been reached)
055 */
056 Service addAndAcquire(Key key, Service service);
057
058 /**
059 * Tries to acquire the service with the given key
060 *
061 * @param key the key
062 * @return the acquired service, or <tt>null</tt> if no free in pool
063 */
064 Service acquire(Key key);
065
066 /**
067 * Releases the service back to the pool
068 *
069 * @param key the key
070 * @param service the service
071 */
072 void release(Key key, Service service);
073
074 /**
075 * Returns the current size of the pool
076 *
077 * @return the current size of the pool
078 */
079 int size();
080
081 /**
082 * Purges the pool.
083 */
084 void purge();
085
086 }