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