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 */
028public interface InflightRepository extends StaticService {
029
030    /**
031     * Information about the inflight exchange.
032     */
033    interface InflightExchange {
034
035        /**
036         * The exchange being inflight
037         */
038        Exchange getExchange();
039
040        /**
041         * The duration in millis the exchange has been inflight
042         */
043        long getDuration();
044
045        /**
046         * The elapsed time in millis processing the exchange at the current node
047         */
048        long getElapsed();
049
050        /**
051         * The id of the node from the route where the exchange currently is being processed
052         * <p/>
053         * Is <tt>null</tt> if message history is disabled.
054         */
055        String getNodeId();
056
057        /**
058         * The id of the route where the exchange originates (started)
059         */
060        String getFromRouteId();
061
062        /**
063         * The id of the route where the exchange currently is being processed
064         * <p/>
065         * Is <tt>null</tt> if message history is disabled.
066         */
067        String getAtRouteId();
068
069    }
070
071    /**
072     * Adds the exchange to the inflight registry to the total counter
073     *
074     * @param exchange  the exchange
075     */
076    void add(Exchange exchange);
077
078    /**
079     * Removes the exchange from the inflight registry to the total counter
080     *
081     * @param exchange  the exchange
082     */
083    void remove(Exchange exchange);
084
085    /**
086     * Adds the exchange to the inflight registry associated to the given route
087     *
088     * @param exchange  the exchange
089     * @param routeId the id of the route
090     */
091    void add(Exchange exchange, String routeId);
092
093    /**
094     * Removes the exchange from the inflight registry removing association to the given route
095     *
096     * @param exchange  the exchange
097     * @param routeId the id of the route
098     */
099    void remove(Exchange exchange, String routeId);
100
101    /**
102     * Current size of inflight exchanges.
103     * <p/>
104     * Will return 0 if there are no inflight exchanges.
105     *
106     * @return number of exchanges currently in flight.
107     */
108    int size();
109
110    /**
111     * Adds the route from the in flight registry.
112     * <p/>
113     * Is used for initializing up resources
114     *
115     * @param routeId the id of the route
116     */
117    void addRoute(String routeId);
118
119    /**
120     * Removes the route from the in flight registry.
121     * <p/>
122     * Is used for cleaning up resources to avoid leaking.
123     *
124     * @param routeId the id of the route
125     */
126    void removeRoute(String routeId);
127
128    /**
129    * Current size of inflight exchanges which are from the given route.
130     * <p/>
131     * Will return 0 if there are no inflight exchanges.
132     *
133     * @param routeId the id of the route
134     * @return number of exchanges currently in flight.
135     */
136    int size(String routeId);
137
138    /**
139     * A <i>read-only</i> browser of the {@link InflightExchange}s that are currently inflight.
140     */
141    Collection<InflightExchange> browse();
142
143    /**
144     * A <i>read-only</i> browser of the {@link InflightExchange}s that are currently inflight that started from the given route.
145     *
146     * @param fromRouteId  the route id, or <tt>null</tt> for all routes.
147     */
148    Collection<InflightExchange> browse(String fromRouteId);
149
150    /**
151     * A <i>read-only</i> browser of the {@link InflightExchange}s that are currently inflight.
152     *
153     * @param limit maximum number of entries to return
154     * @param sortByLongestDuration to sort by the longest duration. Set to <tt>true</tt> to include the exchanges that has been inflight the longest time,
155     *                              set to <tt>false</tt> to sort by exchange id
156     */
157    Collection<InflightExchange> browse(int limit, boolean sortByLongestDuration);
158
159    /**
160     * A <i>read-only</i> browser of the {@link InflightExchange}s that are currently inflight that started from the given route.
161     *
162     * @param fromRouteId  the route id, or <tt>null</tt> for all routes.
163     * @param limit maximum number of entries to return
164     * @param sortByLongestDuration to sort by the longest duration. Set to <tt>true</tt> to include the exchanges that has been inflight the longest time,
165     *                              set to <tt>false</tt> to sort by exchange id
166     */
167    Collection<InflightExchange> browse(String fromRouteId, int limit, boolean sortByLongestDuration);
168
169    /**
170     * Gets the oldest {@link InflightExchange} that are currently inflight that started from the given route.
171     *
172     * @param fromRouteId  the route id, or <tt>null</tt> for all routes.
173     * @return the oldest, or <tt>null</tt> if none inflight
174     */
175    InflightExchange oldest(String fromRouteId);
176
177}