use of org.apache.camel.spi.Synchronization in project camel by apache.
the class IgniteCacheProducer method doQuery.
@SuppressWarnings("unchecked")
private void doQuery(Message in, Message out, Exchange exchange) {
Query<Object> query = in.getHeader(IgniteConstants.IGNITE_CACHE_QUERY, Query.class);
if (query == null) {
try {
query = in.getMandatoryBody(Query.class);
} catch (InvalidPayloadException e) {
exchange.setException(e);
return;
}
}
final QueryCursor<Object> cursor = cache.query(query);
out.setBody(cursor.iterator());
exchange.addOnCompletion(new Synchronization() {
@Override
public void onFailure(Exchange exchange) {
cursor.close();
}
@Override
public void onComplete(Exchange exchange) {
cursor.close();
}
});
}
use of org.apache.camel.spi.Synchronization in project camel by apache.
the class IronMQConsumer method processBatch.
@Override
public int processBatch(Queue<Object> exchanges) throws Exception {
int total = exchanges.size();
for (int index = 0; index < total && isBatchAllowed(); index++) {
// only loop if we are started (allowed to run)
final Exchange exchange = ObjectHelper.cast(Exchange.class, exchanges.poll());
// add current index and total as properties
exchange.setProperty(Exchange.BATCH_INDEX, index);
exchange.setProperty(Exchange.BATCH_SIZE, total);
exchange.setProperty(Exchange.BATCH_COMPLETE, index == total - 1);
// update pending number of exchanges
pendingExchanges = total - index - 1;
// if batchDelete is not enabled
if (!getEndpoint().getConfiguration().isBatchDelete()) {
exchange.addOnCompletion(new Synchronization() {
final String reservationId = ExchangeHelper.getMandatoryHeader(exchange, IronMQConstants.MESSAGE_RESERVATION_ID, String.class);
final String messageid = ExchangeHelper.getMandatoryHeader(exchange, IronMQConstants.MESSAGE_ID, String.class);
public void onComplete(Exchange exchange) {
processCommit(exchange, messageid, reservationId);
}
public void onFailure(Exchange exchange) {
processRollback(exchange);
}
@Override
public String toString() {
return "IronMQConsumerOnCompletion";
}
});
}
LOG.trace("Processing exchange [{}]...", exchange);
getProcessor().process(exchange);
}
return total;
}
use of org.apache.camel.spi.Synchronization in project camel by apache.
the class KratiConsumer method processBatch.
@Override
public int processBatch(Queue<Object> exchanges) throws Exception {
int total = exchanges.size();
for (int index = 0; index < total && isBatchAllowed(); index++) {
// only loop if we are started (allowed to run)
Exchange exchange = ObjectHelper.cast(Exchange.class, exchanges.poll());
// add current index and total as properties
exchange.setProperty(Exchange.BATCH_INDEX, index);
exchange.setProperty(Exchange.BATCH_SIZE, total);
exchange.setProperty(Exchange.BATCH_COMPLETE, index == total - 1);
// update pending number of exchanges
pendingExchanges = total - index - 1;
// add on completion to handle after work when the exchange is done
exchange.addOnCompletion(new Synchronization() {
public void onComplete(Exchange exchange) {
try {
dataStore.delete(exchange.getProperty(KratiConstants.KEY));
} catch (Exception e) {
LOG.warn("Failed to remove from datastore. This exception is ignored.", e);
}
}
public void onFailure(Exchange exchange) {
// noop
}
});
LOG.trace("Processing exchange [{}]...", exchange);
getProcessor().process(exchange);
}
return total;
}
use of org.apache.camel.spi.Synchronization in project camel by apache.
the class DisruptorConsumer method process.
private void process(final SynchronizedExchange synchronizedExchange) {
try {
Exchange exchange = synchronizedExchange.getExchange();
final boolean ignore = exchange.hasProperties() && exchange.getProperties().containsKey(DisruptorEndpoint.DISRUPTOR_IGNORE_EXCHANGE);
if (ignore) {
// Property was set and it was set to true, so don't process Exchange.
LOGGER.trace("Ignoring exchange {}", exchange);
return;
}
// send a new copied exchange with new camel context
final Exchange result = prepareExchange(exchange);
// We need to be notified when the exchange processing is complete to synchronize the original exchange
// This is however the last part of the processing of this exchange and as such can't be done
// in the AsyncCallback as that is called *AFTER* processing is considered to be done
// (see org.apache.camel.processor.CamelInternalProcessor.InternalCallback#done).
// To solve this problem, a new synchronization is set on the exchange that is to be
// processed
result.addOnCompletion(new Synchronization() {
@Override
public void onComplete(Exchange exchange) {
synchronizedExchange.consumed(result);
}
@Override
public void onFailure(Exchange exchange) {
synchronizedExchange.consumed(result);
}
});
// As the necessary post-processing of the exchange is done by the registered Synchronization,
// we can suffice with a no-op AsyncCallback
processor.process(result, NOOP_ASYNC_CALLBACK);
} catch (Exception e) {
Exchange exchange = synchronizedExchange.getExchange();
if (exchange != null) {
getExceptionHandler().handleException("Error processing exchange", exchange, e);
} else {
getExceptionHandler().handleException(e);
}
}
}
use of org.apache.camel.spi.Synchronization in project camel by apache.
the class XsltBuilderTest method testXsltOutputFileDelete.
public void testXsltOutputFileDelete() throws Exception {
URL styleSheet = getClass().getResource("example.xsl");
XsltBuilder builder = XsltBuilder.xslt(styleSheet).outputFile().deleteOutputFile();
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setBody("<hello>world!</hello>");
exchange.getIn().setHeader(Exchange.XSLT_FILE_NAME, "target/xslt/xsltout.xml");
builder.process(exchange);
assertIsInstanceOf(File.class, exchange.getOut().getBody());
File file = new File("target/xslt/xsltout.xml");
assertTrue("Output file should exist", file.exists());
String body = exchange.getOut().getBody(String.class);
assertTrue(body.endsWith("<goodbye>world!</goodbye>"));
// now done the exchange
List<Synchronization> onCompletions = exchange.handoverCompletions();
UnitOfWorkHelper.doneSynchronizations(exchange, onCompletions, log);
// the file should be deleted
assertFalse("Output file should be deleted", file.exists());
}
Aggregations