Search in sources :

Example 11 with DefaultExchangeHolder

use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.

the class JdbcCamelCodec method unmarshallExchange.

public Exchange unmarshallExchange(CamelContext camelContext, byte[] buffer) throws IOException, ClassNotFoundException {
    DefaultExchangeHolder pe = decode(camelContext, buffer);
    Exchange answer = new DefaultExchange(camelContext);
    DefaultExchangeHolder.unmarshal(answer, pe);
    // restore the from endpoint
    String fromEndpointUri = (String) answer.removeProperty("CamelAggregatedFromEndpoint");
    if (fromEndpointUri != null) {
        Endpoint fromEndpoint = camelContext.hasEndpoint(fromEndpointUri);
        if (fromEndpoint != null) {
            answer.setFromEndpoint(fromEndpoint);
        }
    }
    return answer;
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Endpoint(org.apache.camel.Endpoint)

Example 12 with DefaultExchangeHolder

use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.

the class HazelcastSedaConsumer method run.

public void run() {
    final BlockingQueue<?> queue = endpoint.getQueue();
    while (queue != null && isRunAllowed()) {
        final Exchange exchange = this.getEndpoint().createExchange();
        TransactionContext transactionCtx = null;
        if (endpoint.getConfiguration().isTransacted()) {
            // Get and begin transaction if exist
            transactionCtx = endpoint.getHazelcastInstance().newTransactionContext();
            if (transactionCtx != null) {
                log.trace("Begin transaction: {}", transactionCtx.getTxnId());
                transactionCtx.beginTransaction();
            }
        }
        try {
            final Object body = queue.poll(endpoint.getConfiguration().getPollTimeout(), TimeUnit.MILLISECONDS);
            if (body != null) {
                if (body instanceof DefaultExchangeHolder) {
                    DefaultExchangeHolder.unmarshal(exchange, (DefaultExchangeHolder) body);
                } else {
                    exchange.getIn().setBody(body);
                }
                try {
                    // process using the asynchronous routing engine
                    processor.process(exchange, new AsyncCallback() {

                        public void done(boolean asyncDone) {
                        // noop
                        }
                    });
                    if (exchange.getException() != null) {
                        // Rollback
                        if (transactionCtx != null) {
                            transactionCtx.rollbackTransaction();
                        }
                        getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
                    }
                } catch (Exception e) {
                    LOG.error("Hzlq Exception caught: " + e, e);
                    // Rollback
                    if (transactionCtx != null) {
                        log.trace("Rollback transaction: {}", transactionCtx.getTxnId());
                        transactionCtx.rollbackTransaction();
                    }
                }
            }
            // It's OK, I commit
            if (exchange.getException() == null && transactionCtx != null) {
                log.trace("Commit transaction: {}", transactionCtx.getTxnId());
                transactionCtx.commitTransaction();
            }
        } catch (InterruptedException e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Hzlq Consumer Interrupted: " + e, e);
            }
            continue;
        } catch (Throwable e) {
            // Rollback
            if (transactionCtx != null) {
                log.trace("Rollback transaction: {}", transactionCtx.getTxnId());
                transactionCtx.rollbackTransaction();
            }
            getExceptionHandler().handleException("Error processing exchange", exchange, e);
        }
    }
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder) TransactionContext(com.hazelcast.transaction.TransactionContext) AsyncCallback(org.apache.camel.AsyncCallback)

Example 13 with DefaultExchangeHolder

use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.

the class HazelcastAggregationRepository method remove.

/**
     * This method performs transactional operation on removing the {@code exchange}
     * from the operational storage and moving it into the persistent one if the {@link HazelcastAggregationRepository}
     * runs in recoverable mode and {@code optimistic} is false. It will act at <u>your own</u> risk otherwise.
     * @param camelContext   the current CamelContext
     * @param key            the correlation key
     * @param exchange       the exchange to remove
     */
@Override
public void remove(CamelContext camelContext, String key, Exchange exchange) {
    DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
    if (optimistic) {
        LOG.trace("Removing an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(), key);
        if (!cache.remove(key, holder)) {
            LOG.error("Optimistic locking failed for exchange with key {}: IMap#remove removed no Exchanges, while it's expected to remove one.", key);
            throw new OptimisticLockingException();
        }
        LOG.trace("Removed an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(), key);
        if (useRecovery) {
            LOG.trace("Putting an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.", exchange.getExchangeId(), key);
            persistedCache.put(exchange.getExchangeId(), holder);
            LOG.trace("Put an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.", exchange.getExchangeId(), key);
        }
    } else {
        if (useRecovery) {
            LOG.trace("Removing an exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
            // The only considerable case for transaction usage is fault tolerance:
            // the transaction will be rolled back automatically (default timeout is 2 minutes)
            // if no commit occurs during the timeout. So we are still consistent whether local node crashes.
            TransactionOptions tOpts = new TransactionOptions();
            tOpts.setTransactionType(TransactionOptions.TransactionType.ONE_PHASE);
            TransactionContext tCtx = hzInstance.newTransactionContext(tOpts);
            try {
                tCtx.beginTransaction();
                TransactionalMap<String, DefaultExchangeHolder> tCache = tCtx.getMap(cache.getName());
                TransactionalMap<String, DefaultExchangeHolder> tPersistentCache = tCtx.getMap(persistedCache.getName());
                DefaultExchangeHolder removedHolder = tCache.remove(key);
                LOG.trace("Putting an exchange with ID {} for key {} into a recoverable storage in a thread-safe manner.", exchange.getExchangeId(), key);
                tPersistentCache.put(exchange.getExchangeId(), removedHolder);
                tCtx.commitTransaction();
                LOG.trace("Removed an exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
                LOG.trace("Put an exchange with ID {} for key {} into a recoverable storage in a thread-safe manner.", exchange.getExchangeId(), key);
            } catch (Throwable throwable) {
                tCtx.rollbackTransaction();
                final String msg = String.format("Transaction with ID %s was rolled back for remove operation with a key %s and an Exchange ID %s.", tCtx.getTxnId(), key, exchange.getExchangeId());
                LOG.warn(msg, throwable);
                throw new RuntimeException(msg, throwable);
            }
        } else {
            cache.remove(key);
        }
    }
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder) TransactionOptions(com.hazelcast.transaction.TransactionOptions) TransactionContext(com.hazelcast.transaction.TransactionContext)

Example 14 with DefaultExchangeHolder

use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.

the class HawtDBCamelCodec method unmarshallExchange.

public Exchange unmarshallExchange(CamelContext camelContext, Buffer buffer) throws IOException {
    DataByteArrayInputStream bais = new DataByteArrayInputStream(buffer);
    DefaultExchangeHolder pe = exchangeCodec.decode(bais);
    Exchange answer = new DefaultExchange(camelContext);
    DefaultExchangeHolder.unmarshal(answer, pe);
    // restore the from endpoint
    String fromEndpointUri = (String) answer.removeProperty("CamelAggregatedFromEndpoint");
    if (fromEndpointUri != null) {
        Endpoint fromEndpoint = camelContext.hasEndpoint(fromEndpointUri);
        if (fromEndpoint != null) {
            answer.setFromEndpoint(fromEndpoint);
        }
    }
    return answer;
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Endpoint(org.apache.camel.Endpoint) DataByteArrayInputStream(org.fusesource.hawtbuf.DataByteArrayInputStream)

Example 15 with DefaultExchangeHolder

use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.

the class InfinispanLocalAggregationRepository method add.

@Override
public Exchange add(final CamelContext camelContext, final String key, final Exchange exchange) {
    LOG.trace("Adding an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
    DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
    DefaultExchangeHolder oldHolder = cache.put(key, newHolder);
    return unmarshallExchange(camelContext, oldHolder);
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder)

Aggregations

DefaultExchangeHolder (org.apache.camel.impl.DefaultExchangeHolder)20 Exchange (org.apache.camel.Exchange)6 DefaultExchange (org.apache.camel.impl.DefaultExchange)6 Endpoint (org.apache.camel.Endpoint)4 TransactionContext (com.hazelcast.transaction.TransactionContext)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 BytesMessage (javax.jms.BytesMessage)2 JMSException (javax.jms.JMSException)2 MapMessage (javax.jms.MapMessage)2 ObjectMessage (javax.jms.ObjectMessage)2 StreamMessage (javax.jms.StreamMessage)2 TextMessage (javax.jms.TextMessage)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 DataByteArrayInputStream (org.fusesource.hawtbuf.DataByteArrayInputStream)2 DataByteArrayOutputStream (org.fusesource.hawtbuf.DataByteArrayOutputStream)2 TransactionOptions (com.hazelcast.transaction.TransactionOptions)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ArrayList (java.util.ArrayList)1