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