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();
}
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();
}
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;
}
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;
}
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;
}
}
Aggregations