Search in sources :

Example 16 with DefaultExchangeHolder

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);
    }
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder)

Example 17 with DefaultExchangeHolder

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);
    }
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) Object(org.apache.camel.component.jms.JmsMessageType.Object) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) JMSException(javax.jms.JMSException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) TextMessage(javax.jms.TextMessage)

Example 18 with DefaultExchangeHolder

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;
}
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 19 with DefaultExchangeHolder

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;
}
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 20 with DefaultExchangeHolder

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

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