Search in sources :

Example 6 with DefaultExchangeHolder

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

the class MyObject method deserialize.

@Test
public void deserialize() throws IOException, ClassNotFoundException {
    CamelContext context = new DefaultCamelContext();
    final DefaultExchange exchange = new DefaultExchange(context);
    final List<MyObject> objects = new ArrayList<>();
    final MyObject o = new MyObject("leb", "hello".getBytes());
    objects.add(o);
    exchange.getIn().setBody(objects);
    final DefaultExchangeHolder deh = DefaultExchangeHolder.marshal(exchange);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(deh);
    oos.flush();
    final byte[] serialized = baos.toByteArray();
    final ObjectInputStream bis = new ClassLoadingAwareObjectInputStream(context, new ByteArrayInputStream(serialized));
    final DefaultExchangeHolder deserialized = (DefaultExchangeHolder) bis.readObject();
    final DefaultExchange exchange2 = new DefaultExchange(context);
    DefaultExchangeHolder.unmarshal(exchange2, deserialized);
    List<MyObject> receivedObjects = exchange2.getIn().getBody(List.class);
    assertEquals(1, receivedObjects.size());
    assertEquals(o, receivedObjects.get(0));
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultExchange(org.apache.camel.impl.DefaultExchange) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 7 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 {
        // if we are configured to not map the jms message then return it as body
        if (!mapJmsMessage) {
            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) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) JMSException(javax.jms.JMSException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) TextMessage(javax.jms.TextMessage)

Example 8 with DefaultExchangeHolder

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

the class JCacheAggregationRepository method add.

@Override
public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
    if (optimistic) {
        throw new UnsupportedOperationException();
    }
    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.getAndPut(key, newHolder);
    return unmarshallExchange(camelContext, oldHolder);
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder)

Example 9 with DefaultExchangeHolder

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

the class JdbcCamelCodec method marshallExchange.

public byte[] marshallExchange(CamelContext camelContext, Exchange exchange, boolean allowSerializedHeaders) throws IOException {
    // use DefaultExchangeHolder to marshal to a serialized object
    DefaultExchangeHolder pe = DefaultExchangeHolder.marshal(exchange, false, allowSerializedHeaders);
    // add the aggregated size and timeout property as the only properties we want to retain
    DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_SIZE, exchange.getProperty(Exchange.AGGREGATED_SIZE, Integer.class));
    DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_TIMEOUT, exchange.getProperty(Exchange.AGGREGATED_TIMEOUT, Long.class));
    // add the aggregated completed by property to retain
    DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_COMPLETED_BY, exchange.getProperty(Exchange.AGGREGATED_COMPLETED_BY, String.class));
    // add the aggregated correlation key property to retain
    DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_CORRELATION_KEY, exchange.getProperty(Exchange.AGGREGATED_CORRELATION_KEY, String.class));
    DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_CORRELATION_KEY, exchange.getProperty(Exchange.AGGREGATED_CORRELATION_KEY, String.class));
    // and a guard property if using the flexible toolbox aggregator
    DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_COLLECTION_GUARD, exchange.getProperty(Exchange.AGGREGATED_COLLECTION_GUARD, String.class));
    // persist the from endpoint as well
    if (exchange.getFromEndpoint() != null) {
        DefaultExchangeHolder.addProperty(pe, "CamelAggregatedFromEndpoint", exchange.getFromEndpoint().getEndpointUri());
    }
    return encode(pe);
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder)

Example 10 with DefaultExchangeHolder

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

the class JdbcCamelCodec method decode.

private DefaultExchangeHolder decode(CamelContext camelContext, byte[] dataIn) throws IOException, ClassNotFoundException {
    ByteArrayInputStream bytesIn = new ByteArrayInputStream(dataIn);
    ObjectInputStream objectIn = null;
    Object obj = null;
    try {
        objectIn = new ClassLoadingAwareObjectInputStream(camelContext, bytesIn);
        obj = objectIn.readObject();
    } finally {
        IOHelper.close(objectIn);
    }
    return (DefaultExchangeHolder) obj;
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream)

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