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