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.spi;
018
019import java.util.Collection;
020
021import org.apache.camel.Endpoint;
022import org.apache.camel.Exchange;
023import org.apache.camel.StaticService;
024
025/**
026 * A repository which tracks in flight {@link Exchange}s.
027 *
028 * @version 
029 */
030public interface InflightRepository extends StaticService {
031
032    /**
033     * Information about the inflight exchange.
034     */
035    interface InflightExchange {
036
037        /**
038         * The exchange being inflight
039         */
040        Exchange getExchange();
041
042        /**
043         * The duration in millis the exchange has been inflight
044         */
045        long getDuration();
046
047        /**
048         * The elapsed time in millis processing the exchange at the current node
049         */
050        long getElapsed();
051
052        /**
053         * The id of the node from the route where the exchange currently is being processed
054         * <p/>
055         * Is <tt>null</tt> if message history is disabled.
056         */
057        String getNodeId();
058
059        /**
060         * The id of the route where the exchange currently is being processed
061         * <p/>
062         * Is <tt>null</tt> if message history is disabled.
063         */
064        String getRouteId();
065
066    }
067
068    /**
069     * Adds the exchange to the inflight registry to the total counter
070     *
071     * @param exchange  the exchange
072     */
073    void add(Exchange exchange);
074
075    /**
076     * Removes the exchange from the inflight registry to the total counter
077     *
078     * @param exchange  the exchange
079     */
080    void remove(Exchange exchange);
081
082    /**
083     * Adds the exchange to the inflight registry associated to the given route
084     *
085     * @param exchange  the exchange
086     * @param routeId the id of the route
087     */
088    void add(Exchange exchange, String routeId);
089
090    /**
091     * Removes the exchange from the inflight registry removing association to the given route
092     *
093     * @param exchange  the exchange
094     * @param routeId the id of the route
095     */
096    void remove(Exchange exchange, String routeId);
097
098    /**
099     * Current size of inflight exchanges.
100     * <p/>
101     * Will return 0 if there are no inflight exchanges.
102     *
103     * @return number of exchanges currently in flight.
104     */
105    int size();
106
107    /**
108     * Will always return 0 due method is deprecated.
109     * @deprecated will be removed in a future Camel release.
110     */
111    @Deprecated
112    int size(Endpoint endpoint);
113
114    /**
115     * Removes the route from the in flight registry.
116     * <p/>
117     * Is used for cleaning up resources to avoid leaking.
118     *
119     * @param routeId the id of the route
120     */
121    void removeRoute(String routeId);
122
123    /**
124    * Current size of inflight exchanges which are from the given route.
125     * <p/>
126     * Will return 0 if there are no inflight exchanges.
127     *
128     * @param routeId the id of the route
129     * @return number of exchanges currently in flight.
130     */
131    int size(String routeId);
132
133    /**
134     * A <i>read-only</i> browser of the {@link InflightExchange}s that are currently inflight.
135     */
136    Collection<InflightExchange> browse();
137
138    /**
139     * A <i>read-only</i> browser of the {@link InflightExchange}s that are currently inflight.
140     *
141     * @param limit maximum number of entries to return
142     * @param sortByLongestDuration to sort by the longest duration. Set to <tt>true</tt> to include the exchanges that has been inflight the longest time,
143     *                              set to <tt>false</tt> to sort by exchange id
144     */
145    Collection<InflightExchange> browse(int limit, boolean sortByLongestDuration);
146
147}