Joe2013
Joe2013

Reputation: 997

Camel - Keeping a copy of the message

My camel route is as follows (sample)

from (activemq:xyz) --- Receive the message from a QUEUE

to(smpp:abc) --- Submit the message to SMSC

to(cxf:hij) --- Based on SMSC response as success call the webservice

The problem I am facing is as below

I have few exchange properties/headers received from the queue, but after receiving the response from SMPP, my exchange headers/properties that I have sent are cleared and not available for me to call the webservice. What can I do to keep these values to be as it is untill I reach the end of route. I have no control on the SMSC implementation and cannot change the SMSC response. I am using SPRING dsl

Upvotes: 1

Views: 5900

Answers (2)

Ralf
Ralf

Reputation: 6853

You could use a bean and store the headers and properties you would like to preserve in a thread-local member. A simple example that keeps and restores all headers/properties could look like this:

public class BeanToHoldHeadersAndProps {
    ThreadLocal<Map<String,Object>> headers = new ThreadLocal<>();
    ThreadLocal<Map<String,Object>> props = new ThreadLocal<>();

    public void saveHeaders(Exchange exchange) {
        headers.set(exchange.getIn().getHeaders());
        props.set(exchange.getProperties());
    }

    public void restoreHeaders(Exchange exchange) {
        exchange.getIn().setHeaders(headers.get());
        exchange.getProperties().putAll(props.get());
    }
}

Route:

<camel:route id="header_preserving_route">
    <camel:from url="activemq:xyz" />
    <camel:bean ref="headerPreserver" method="saveHeaders" />
    <camel:to url="smpp:abc" />
    <camel:bean ref="headerPreserver" method="restoreHeaders" />
    <camel:to url="cxf:hij" />
</camel:route>

Depending on your requirements there is a third element you might want to carry over, which is the attachments of the exchange.

Upvotes: 0

vikingsteve
vikingsteve

Reputation: 40378

You can consider using the enterprise integration pattern named Content Enricher for the SMPP part, with a custom AggregationStrategy that 'keeps' your original exchange (with all the headers and properties), and takes what you need (the body, I presume?) from whatever SMSC does.

from (activemq:xyz)
    .enrich(smpp:abc, new PreserveHeadersAndPropertiesAggregationStrategy())
    .to(cxf:hij)
;

with

public class PreserveHeadersAndPropertiesAggregationStrategy implements AggregationStrategy {
    @Override
    public Exchange aggregate(Exchange original, Exchange resource) {
         // use body from getIn() or getOut() depending on the exchange pattern...
        original.getIn().setBody(resource.getIn().getBody());
        original.getOut().setBody(resource.getOut().getBody());
        return original;
    }
}

Upvotes: 6

Related Questions