use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.
the class JCacheAggregationRepository method 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);
} else {
cache.remove(key);
}
}
use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.
the class JmsBinding method extractBodyFromJms.
/**
* Extracts the body from the JMS message
*
* @param exchange the exchange
* @param message the message to extract its body
* @return the body, can be <tt>null</tt>
*/
public Object extractBodyFromJms(Exchange exchange, Message message) {
try {
// based on message type
if (endpoint != null && endpoint.getMessageConverter() != null) {
if (LOG.isTraceEnabled()) {
LOG.trace("Extracting body using a custom MessageConverter: {} from JMS message: {}", endpoint.getMessageConverter(), message);
}
return endpoint.getMessageConverter().fromMessage(message);
}
// if we are configured to not map the jms message then return it as body
if (endpoint != null && !endpoint.getConfiguration().isMapJmsMessage()) {
LOG.trace("Option map JMS message is false so using JMS message as body: {}", message);
return message;
}
if (message instanceof ObjectMessage) {
LOG.trace("Extracting body as a ObjectMessage from JMS message: {}", message);
ObjectMessage objectMessage = (ObjectMessage) message;
Object payload = objectMessage.getObject();
if (payload instanceof DefaultExchangeHolder) {
DefaultExchangeHolder holder = (DefaultExchangeHolder) payload;
DefaultExchangeHolder.unmarshal(exchange, holder);
return exchange.getIn().getBody();
} else {
return objectMessage.getObject();
}
} else if (message instanceof TextMessage) {
LOG.trace("Extracting body as a TextMessage from JMS message: {}", message);
TextMessage textMessage = (TextMessage) message;
return textMessage.getText();
} else if (message instanceof MapMessage) {
LOG.trace("Extracting body as a MapMessage from JMS message: {}", message);
return createMapFromMapMessage((MapMessage) message);
} else if (message instanceof BytesMessage) {
LOG.trace("Extracting body as a BytesMessage from JMS message: {}", message);
return createByteArrayFromBytesMessage((BytesMessage) message);
} else if (message instanceof StreamMessage) {
LOG.trace("Extracting body as a StreamMessage from JMS message: {}", message);
return message;
} else {
return null;
}
} catch (JMSException e) {
throw new RuntimeCamelException("Failed to extract body due to: " + e + ". Message: " + message, e);
}
}
use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.
the class LevelDBCamelCodec 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 CassandraCamelCodec method unmarshallExchange.
public Exchange unmarshallExchange(CamelContext camelContext, ByteBuffer buffer) throws IOException, ClassNotFoundException {
DefaultExchangeHolder pe = (DefaultExchangeHolder) deserialize(new ByteBufferInputStream(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 HazelcastAggregationRepository method add.
@Override
public Exchange add(CamelContext camelContext, String key, Exchange oldExchange, Exchange newExchange) throws OptimisticLockingException {
if (!optimistic) {
throw new UnsupportedOperationException();
}
LOG.trace("Adding an Exchange with ID {} for key {} in an optimistic manner.", newExchange.getExchangeId(), key);
if (oldExchange == null) {
DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
final DefaultExchangeHolder misbehaviorHolder = cache.putIfAbsent(key, holder);
if (misbehaviorHolder != null) {
Exchange misbehaviorEx = unmarshallExchange(camelContext, misbehaviorHolder);
LOG.error("Optimistic locking failed for exchange with key {}: IMap#putIfAbsend returned Exchange with ID {}, while it's expected no exchanges to be returned", key, misbehaviorEx != null ? misbehaviorEx.getExchangeId() : "<null>");
throw new OptimisticLockingException();
}
} else {
DefaultExchangeHolder oldHolder = DefaultExchangeHolder.marshal(oldExchange, true, allowSerializedHeaders);
DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
if (!cache.replace(key, oldHolder, newHolder)) {
LOG.error("Optimistic locking failed for exchange with key {}: IMap#replace returned no Exchanges, while it's expected to replace one", key);
throw new OptimisticLockingException();
}
}
LOG.trace("Added an Exchange with ID {} for key {} in optimistic manner.", newExchange.getExchangeId(), key);
return oldExchange;
}
Aggregations