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.impl;
018    
019    import java.util.concurrent.ConcurrentHashMap;
020    import java.util.concurrent.ConcurrentMap;
021    import java.util.concurrent.atomic.AtomicInteger;
022    
023    import org.apache.camel.Endpoint;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.spi.InflightRepository;
026    import org.apache.camel.support.ServiceSupport;
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    
030    /**
031     * Default implement which just uses a counter
032     *
033     * @version 
034     */
035    public class DefaultInflightRepository extends ServiceSupport implements InflightRepository  {
036    
037        private static final Logger LOG = LoggerFactory.getLogger(DefaultInflightRepository.class);
038        private final AtomicInteger totalCount = new AtomicInteger();
039        private final ConcurrentMap<String, AtomicInteger> routeCount = new ConcurrentHashMap<String, AtomicInteger>();
040    
041        public void add(Exchange exchange) {
042            totalCount.incrementAndGet();
043        }
044    
045        public void remove(Exchange exchange) {
046            totalCount.decrementAndGet();
047        }
048    
049        public void add(Exchange exchange, String routeId) {
050            AtomicInteger existing = routeCount.putIfAbsent(routeId, new AtomicInteger(1));
051            if (existing != null) {
052                existing.incrementAndGet();
053            }
054        }
055    
056        public void remove(Exchange exchange, String routeId) {
057            AtomicInteger existing = routeCount.get(routeId);
058            if (existing != null) {
059                existing.decrementAndGet();
060            }
061        }
062    
063        public int size() {
064            return totalCount.get();
065        }
066    
067        @Deprecated
068        public int size(Endpoint endpoint) {
069            return 0;
070        }
071    
072        @Override
073        public void removeRoute(String routeId) {
074            routeCount.remove(routeId);
075        }
076    
077        @Override
078        public int size(String routeId) {
079            AtomicInteger existing = routeCount.get(routeId);
080            return existing != null ? existing.get() : 0; 
081        }
082    
083        @Override
084        protected void doStart() throws Exception {
085        }
086    
087        @Override
088        protected void doStop() throws Exception {
089            int count = size();
090            if (count > 0) {
091                LOG.warn("Shutting down while there are still " + count + " inflight exchanges.");
092            } else {
093                LOG.debug("Shutting down with no inflight exchanges.");
094            }
095            routeCount.clear();
096        }
097    }