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 */ 017package org.apache.camel.impl; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.Map; 022import java.util.concurrent.ArrayBlockingQueue; 023import java.util.concurrent.BlockingQueue; 024 025import org.apache.camel.spi.ServicePool; 026import org.apache.camel.support.ServiceSupport; 027import org.apache.camel.util.LRUCacheFactory; 028import org.apache.camel.util.ServiceHelper; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032/** 033 * Default implementation to inherit for a basic service pool. 034 * 035 * @version 036 */ 037@Deprecated 038public abstract class DefaultServicePool<Key, Service> extends ServiceSupport implements ServicePool<Key, Service> { 039 protected final Logger log = LoggerFactory.getLogger(getClass()); 040 protected final Map<Key, BlockingQueue<Service>> pool; 041 protected int capacity = 100; 042 043 protected DefaultServicePool() { 044 this.pool = LRUCacheFactory.newLRUCache(capacity); 045 } 046 047 public DefaultServicePool(int capacity) { 048 this.capacity = capacity; 049 this.pool = LRUCacheFactory.newLRUCache(capacity); 050 } 051 052 public int getCapacity() { 053 return capacity; 054 } 055 056 public void setCapacity(int capacity) { 057 this.capacity = capacity; 058 } 059 060 public synchronized int size() { 061 int size = 0; 062 for (BlockingQueue<Service> entry : pool.values()) { 063 size += entry.size(); 064 } 065 return size; 066 } 067 068 public synchronized Service addAndAcquire(Key key, Service service) { 069 BlockingQueue<Service> entry = pool.get(key); 070 if (entry == null) { 071 entry = new ArrayBlockingQueue<Service>(capacity); 072 pool.put(key, entry); 073 } 074 log.trace("AddAndAcquire key: {} service: {}", key, service); 075 076 // test if queue will be full 077 if (entry.size() >= capacity) { 078 throw new IllegalStateException("Queue full"); 079 } 080 return service; 081 } 082 083 public synchronized Service acquire(Key key) { 084 BlockingQueue<Service> services = pool.get(key); 085 if (services == null || services.isEmpty()) { 086 log.trace("No free services in pool to acquire for key: {}", key); 087 return null; 088 } 089 090 Service answer = services.poll(); 091 log.trace("Acquire: {} service: {}", key, answer); 092 return answer; 093 } 094 095 public synchronized void release(Key key, Service service) { 096 log.trace("Release: {} service: {}", key, service); 097 BlockingQueue<Service> services = pool.get(key); 098 if (services != null) { 099 services.add(service); 100 } 101 } 102 103 public void purge() { 104 pool.clear(); 105 } 106 107 protected void doStart() throws Exception { 108 log.debug("Starting service pool: {}", this); 109 } 110 111 protected void doStop() throws Exception { 112 log.debug("Stopping service pool: {}", this); 113 for (BlockingQueue<Service> entry : pool.values()) { 114 Collection<Service> values = new ArrayList<Service>(); 115 entry.drainTo(values); 116 ServiceHelper.stopServices(values); 117 entry.clear(); 118 } 119 pool.clear(); 120 } 121 122}