Search in sources :

Example 41 with Producer

use of org.apache.camel.Producer in project camel by apache.

the class FromFtpToBinaryFilesTest method prepareFtpServer.

private void prepareFtpServer() throws Exception {
    // prepares the FTP Server by creating a file on the server that we want to unit
    // test that we can pool and store as a local file
    String ftpUrl = "ftp://admin@localhost:" + getPort() + "/incoming?password=admin&binary=true" + "&consumer.delay=2000&recursive=false";
    Endpoint endpoint = context.getEndpoint(ftpUrl);
    Exchange exchange = endpoint.createExchange();
    exchange.getIn().setBody(IOConverter.toFile("src/test/data/ftpbinarytest/logo.jpeg"));
    exchange.getIn().setHeader(Exchange.FILE_NAME, "logo.jpeg");
    Producer producer = endpoint.createProducer();
    producer.start();
    producer.process(exchange);
    producer.stop();
    ftpUrl = "ftp://admin@localhost:" + getPort() + "/incoming/a?password=admin&binary=true" + "&consumer.delay=2000&recursive=false";
    endpoint = context.getEndpoint(ftpUrl);
    exchange = endpoint.createExchange();
    exchange.getIn().setBody(IOConverter.toFile("src/test/data/ftpbinarytest/logo1.jpeg"));
    exchange.getIn().setHeader(Exchange.FILE_NAME, "logo1.jpeg");
    producer = endpoint.createProducer();
    producer.start();
    producer.process(exchange);
    producer.stop();
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Producer(org.apache.camel.Producer)

Example 42 with Producer

use of org.apache.camel.Producer in project camel by apache.

the class FromFtpToFileNoFileNameHeaderTest method prepareFtpServer.

private void prepareFtpServer() throws Exception {
    // prepares the FTP Server by creating a file on the server that we want to unit
    // test that we can pool and store as a local file
    Endpoint endpoint = context.getEndpoint(getFtpUrl());
    Exchange exchange = endpoint.createExchange();
    exchange.getIn().setBody("Hello World from FTPServer");
    exchange.getIn().setHeader(Exchange.FILE_NAME, "hello.txt");
    Producer producer = endpoint.createProducer();
    producer.start();
    producer.process(exchange);
    producer.stop();
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Producer(org.apache.camel.Producer)

Example 43 with Producer

use of org.apache.camel.Producer in project camel by apache.

the class DataSetEndpoint method createProducer.

@Override
public Producer createProducer() throws Exception {
    Producer answer = super.createProducer();
    long size = getDataSet().getSize();
    expectedMessageCount((int) size);
    return answer;
}
Also used : Producer(org.apache.camel.Producer)

Example 44 with Producer

use of org.apache.camel.Producer in project camel by apache.

the class ProducerCache method doInProducer.

/**
     * Sends an exchange to an endpoint using a supplied callback, using the synchronous processing.
     * <p/>
     * If an exception was thrown during processing, it would be set on the given Exchange
     *
     * @param endpoint  the endpoint to send the exchange to
     * @param exchange  the exchange, can be <tt>null</tt> if so then create a new exchange from the producer
     * @param pattern   the exchange pattern, can be <tt>null</tt>
     * @param callback  the callback
     * @return the response from the callback
     * @see #doInAsyncProducer(org.apache.camel.Endpoint, org.apache.camel.Exchange, org.apache.camel.ExchangePattern, org.apache.camel.AsyncCallback, org.apache.camel.AsyncProducerCallback)
     */
public <T> T doInProducer(Endpoint endpoint, Exchange exchange, ExchangePattern pattern, ProducerCallback<T> callback) {
    T answer = null;
    // get the producer and we do not mind if its pooled as we can handle returning it back to the pool
    Producer producer = doGetProducer(endpoint, true);
    if (producer == null) {
        if (isStopped()) {
            LOG.warn("Ignoring exchange sent after processor is stopped: " + exchange);
            return null;
        } else {
            throw new IllegalStateException("No producer, this processor has not been started: " + this);
        }
    }
    try {
        // invoke the callback
        answer = callback.doInProducer(producer, exchange, pattern);
    } catch (Throwable e) {
        if (exchange != null) {
            exchange.setException(e);
        }
    } finally {
        if (producer instanceof ServicePoolAware) {
            // release back to the pool
            pool.release(endpoint, producer);
        } else if (!producer.isSingleton()) {
            // stop and shutdown non-singleton producers as we should not leak resources
            try {
                ServiceHelper.stopAndShutdownService(producer);
            } catch (Exception e) {
                // ignore and continue
                LOG.warn("Error stopping/shutting down producer: " + producer, e);
            }
        }
    }
    return answer;
}
Also used : Producer(org.apache.camel.Producer) ServicePoolAware(org.apache.camel.ServicePoolAware) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException)

Example 45 with Producer

use of org.apache.camel.Producer in project camel by apache.

the class ProducerCache method doInAsyncProducer.

/**
     * Sends an exchange to an endpoint using a supplied callback supporting the asynchronous routing engine.
     * <p/>
     * If an exception was thrown during processing, it would be set on the given Exchange
     *
     * @param endpoint         the endpoint to send the exchange to
     * @param exchange         the exchange, can be <tt>null</tt> if so then create a new exchange from the producer
     * @param pattern          the exchange pattern, can be <tt>null</tt>
     * @param callback         the asynchronous callback
     * @param producerCallback the producer template callback to be executed
     * @return (doneSync) <tt>true</tt> to continue execute synchronously, <tt>false</tt> to continue being executed asynchronously
     */
public boolean doInAsyncProducer(final Endpoint endpoint, final Exchange exchange, final ExchangePattern pattern, final AsyncCallback callback, final AsyncProducerCallback producerCallback) {
    Producer target;
    try {
        // get the producer and we do not mind if its pooled as we can handle returning it back to the pool
        target = doGetProducer(endpoint, true);
        if (target == null) {
            if (isStopped()) {
                LOG.warn("Ignoring exchange sent after processor is stopped: " + exchange);
                callback.done(true);
                return true;
            } else {
                exchange.setException(new IllegalStateException("No producer, this processor has not been started: " + this));
                callback.done(true);
                return true;
            }
        }
    } catch (Throwable e) {
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    final Producer producer = target;
    // record timing for sending the exchange using the producer
    final StopWatch watch = eventNotifierEnabled && exchange != null ? new StopWatch() : null;
    try {
        if (eventNotifierEnabled && exchange != null) {
            EventHelper.notifyExchangeSending(exchange.getContext(), exchange, endpoint);
        }
        // invoke the callback
        AsyncProcessor asyncProcessor = AsyncProcessorConverterHelper.convert(producer);
        return producerCallback.doInAsyncProducer(producer, asyncProcessor, exchange, pattern, doneSync -> {
            try {
                if (eventNotifierEnabled && watch != null) {
                    long timeTaken = watch.stop();
                    EventHelper.notifyExchangeSent(exchange.getContext(), exchange, endpoint, timeTaken);
                }
                if (producer instanceof ServicePoolAware) {
                    pool.release(endpoint, producer);
                } else if (!producer.isSingleton()) {
                    try {
                        ServiceHelper.stopAndShutdownService(producer);
                    } catch (Exception e) {
                        LOG.warn("Error stopping/shutting down producer: " + producer, e);
                    }
                }
            } finally {
                callback.done(doneSync);
            }
        });
    } catch (Throwable e) {
        // ensure exceptions is caught and set on the exchange
        if (exchange != null) {
            exchange.setException(e);
        }
        callback.done(true);
        return true;
    }
}
Also used : Producer(org.apache.camel.Producer) AsyncProcessor(org.apache.camel.AsyncProcessor) ServicePoolAware(org.apache.camel.ServicePoolAware) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) StopWatch(org.apache.camel.util.StopWatch)

Aggregations

Producer (org.apache.camel.Producer)198 Endpoint (org.apache.camel.Endpoint)140 Exchange (org.apache.camel.Exchange)138 Test (org.junit.Test)72 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)69 Processor (org.apache.camel.Processor)34 RouteBuilder (org.apache.camel.builder.RouteBuilder)23 Message (org.apache.camel.Message)21 CountDownLatch (java.util.concurrent.CountDownLatch)16 File (java.io.File)12 CamelContext (org.apache.camel.CamelContext)12 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)10 DefaultExchange (org.apache.camel.impl.DefaultExchange)9 Mockito.anyLong (org.mockito.Mockito.anyLong)9 Consumer (org.apache.camel.Consumer)8 FileDataSource (javax.activation.FileDataSource)7 AsyncProcessor (org.apache.camel.AsyncProcessor)7 DataHandler (javax.activation.DataHandler)6 Field (java.lang.reflect.Field)5 ExchangePattern (org.apache.camel.ExchangePattern)5