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.concurrent.CompletableFuture;
020import java.util.concurrent.RejectedExecutionException;
021
022import org.apache.camel.AsyncCallback;
023import org.apache.camel.AsyncProcessor;
024import org.apache.camel.AsyncProducer;
025import org.apache.camel.Endpoint;
026import org.apache.camel.Exchange;
027import org.apache.camel.ExchangePattern;
028import org.apache.camel.Processor;
029import org.apache.camel.Producer;
030import org.apache.camel.Service;
031
032/**
033 * Cache containing created {@link Producer}.
034 */
035public interface ProducerCache extends Service {
036
037    /**
038     * Acquires a pooled producer which you <b>must</b> release back again after usage using the
039     * {@link #releaseProducer(org.apache.camel.Endpoint, org.apache.camel.AsyncProducer)} method.
040     *
041     * @param endpoint the endpoint
042     * @return the producer
043     */
044    AsyncProducer acquireProducer(Endpoint endpoint);
045
046    /**
047     * Releases an acquired producer back after usage.
048     *
049     * @param endpoint the endpoint
050     * @param producer the producer to release
051     */
052    void releaseProducer(Endpoint endpoint, AsyncProducer producer);
053
054    /**
055     * Sends the exchange to the given endpoint.
056     * <p>
057     * This method will <b>not</b> throw an exception. If processing of the given
058     * Exchange failed then the exception is stored on the provided Exchange
059     *
060     * @param endpoint the endpoint to send the exchange to
061     * @param exchange the exchange to send
062     * @throws RejectedExecutionException is thrown if CamelContext is stopped
063     */
064    Exchange send(Endpoint endpoint, Exchange exchange, Processor resultProcessor);
065
066    /**
067     * Asynchronously sends an exchange to an endpoint using a supplied
068     * {@link Processor} to populate the exchange
069     * <p>
070     * This method will <b>neither</b> throw an exception <b>nor</b> complete future exceptionally.
071     * If processing of the given Exchange failed then the exception is stored on the return Exchange
072     *
073     * @param endpoint        the endpoint to send the exchange to
074     * @param pattern         the message {@link ExchangePattern} such as
075     *                        {@link ExchangePattern#InOnly} or {@link ExchangePattern#InOut}
076     * @param processor       the transformer used to populate the new exchange
077     * @param resultProcessor a processor to process the exchange when the send is complete.
078     * @param exchange        an exchange to use in processing. Exchange will be created if parameter is null.
079     * @param future          the preexisting future to complete when processing is done or null if to create new one
080     * @return future that completes with exchange when processing is done. Either passed into future parameter
081     *              or new one if parameter was null
082     */
083    CompletableFuture<Exchange> asyncSendExchange(Endpoint endpoint, ExchangePattern pattern,
084            Processor processor, Processor resultProcessor, Exchange exchange, CompletableFuture<Exchange> future);
085
086    /**
087     * Gets the source which uses this cache
088     *
089     * @return the source
090     */
091    Object getSource();
092
093    /**
094     * Returns the current size of the cache
095     *
096     * @return the current size
097     */
098    int size();
099
100    /**
101     * Gets the maximum cache size (capacity).
102     *
103     * @return the capacity
104     */
105    int getCapacity();
106
107    /**
108     * Gets the cache hits statistic
109     * <p/>
110     * Will return <tt>-1</tt> if it cannot determine this if a custom cache was used.
111     *
112     * @return the hits
113     */
114    long getHits();
115
116    /**
117     * Gets the cache misses statistic
118     * <p/>
119     * Will return <tt>-1</tt> if it cannot determine this if a custom cache was used.
120     *
121     * @return the misses
122     */
123    long getMisses();
124
125    /**
126     * Gets the cache evicted statistic
127     * <p/>
128     * Will return <tt>-1</tt> if it cannot determine this if a custom cache was used.
129     *
130     * @return the evicted
131     */
132    long getEvicted();
133
134    /**
135     * Resets the cache statistics
136     */
137    void resetCacheStatistics();
138
139    /**
140     * Purges this cache
141     */
142    void purge();
143
144    /**
145     * Cleanup the cache (purging stale entries)
146     */
147    void cleanUp();
148
149    boolean isEventNotifierEnabled();
150
151    /**
152     * Whether {@link org.apache.camel.spi.EventNotifier} is enabled
153     */
154    void setEventNotifierEnabled(boolean eventNotifierEnabled);
155
156    /**
157     * Gets the endpoint statistics
158     */
159    EndpointUtilizationStatistics getEndpointUtilizationStatistics();
160
161    /**
162     * Sends an exchange to an endpoint using a supplied callback supporting the asynchronous routing engine.
163     * <p/>
164     * If an exception was thrown during processing, it would be set on the given Exchange
165     *
166     * @param endpoint         the endpoint to send the exchange to
167     * @param exchange         the exchange, can be <tt>null</tt> if so then create a new exchange from the producer
168     * @param callback         the asynchronous callback
169     * @param producerCallback the producer template callback to be executed
170     * @return (doneSync) <tt>true</tt> to continue execute synchronously, <tt>false</tt> to continue being executed asynchronously
171     */
172    boolean doInAsyncProducer(Endpoint endpoint, Exchange exchange, AsyncCallback callback, AsyncProducerCallback producerCallback);
173
174    /**
175     * Callback for sending a exchange message to a endpoint using an {@link AsyncProcessor} capable producer.
176     * <p/>
177     * Using this callback as a template pattern ensures that Camel handles the resource handling and will
178     * start and stop the given producer, to avoid resource leaks.
179     */
180    interface AsyncProducerCallback {
181
182        /**
183         * Performs operation on the given producer to send the given exchange.
184         *
185         * @param asyncProducer   the async producer, is never <tt>null</tt>
186         * @param exchange        the exchange to process
187         * @param callback        the async callback
188         * @return (doneSync) <tt>true</tt> to continue execute synchronously, <tt>false</tt> to continue being executed asynchronously
189         */
190        boolean doInAsyncProducer(AsyncProducer asyncProducer, Exchange exchange, AsyncCallback callback);
191    }
192
193}