Search in sources :

Example 1 with Synchronization

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();
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) Query(org.apache.ignite.cache.query.Query) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Synchronization(org.apache.camel.spi.Synchronization)

Example 2 with Synchronization

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;
}
Also used : Exchange(org.apache.camel.Exchange) Synchronization(org.apache.camel.spi.Synchronization) Endpoint(org.apache.camel.Endpoint)

Example 3 with Synchronization

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;
}
Also used : Exchange(org.apache.camel.Exchange) Synchronization(org.apache.camel.spi.Synchronization)

Example 4 with Synchronization

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);
        }
    }
}
Also used : Exchange(org.apache.camel.Exchange) Synchronization(org.apache.camel.spi.Synchronization)

Example 5 with Synchronization

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());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Synchronization(org.apache.camel.spi.Synchronization) File(java.io.File) URL(java.net.URL)

Aggregations

Synchronization (org.apache.camel.spi.Synchronization)22 Exchange (org.apache.camel.Exchange)12 AsyncCallback (org.apache.camel.AsyncCallback)4 Endpoint (org.apache.camel.Endpoint)4 ArrayList (java.util.ArrayList)3 DefaultExchange (org.apache.camel.impl.DefaultExchange)3 Async (io.vertx.ext.unit.Async)2 ProducerTemplate (org.apache.camel.ProducerTemplate)2 SynchronizationRouteAware (org.apache.camel.spi.SynchronizationRouteAware)2 Test (org.junit.Test)2 File (java.io.File)1 URL (java.net.URL)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExchangeTimedOutException (org.apache.camel.ExchangeTimedOutException)1 InvalidPayloadException (org.apache.camel.InvalidPayloadException)1 DirectConsumerNotAvailableException (org.apache.camel.component.direct.DirectConsumerNotAvailableException)1 ReactiveStreamsConsumer (org.apache.camel.component.reactive.streams.ReactiveStreamsConsumer)1 AbstractMessageHandler (org.apache.camel.component.sjms.consumer.AbstractMessageHandler)1 InOnlyMessageHandler (org.apache.camel.component.sjms.consumer.InOnlyMessageHandler)1 InOutMessageHandler (org.apache.camel.component.sjms.consumer.InOutMessageHandler)1