hotmeatballsoup
hotmeatballsoup

Reputation: 595

Setting Camel exchange property from inside a processor

Java 8 and Camel 2.19.x here. I have the following Camel route:

<route id="widgetProcessing">
  <from uri="activemq:inputQueue"/>
  <to uri="{{widgetFetcher}}"/>
</route>

And the widgetFetcher processor:

@Component("widgetFetcher")
public class WidgetFetcher {
  private WidgetDao widgetDao;

  public WidgetFetcher(WidgetDao widgetDao) {
    this.widgetDao = widgetDao;
  }

  public Widget getWidgetToProcess() {
    // get the next widget id from the database
    final Integer firstWidgetId = widgetDao.getFirstSubmittedWidgetId();

    // Do lots of stuff with 'firstWidgetId' down here...
  }
}

I would like to create an exchange property after the <from> and prior to the WidgetFetcher, and set this property's initial value to null; and then conditionally set its value to something else from inside the WidgetFetcher. Furthermore, I'd like this reassigned value to "stick" for the remainder of the route/processing. So something like:

<route id="widgetProcessing">
  <from uri="activemq:inputQueue"/>

  <setProperty propertyName="fizzId">
    <constant>null</constant>
  </setProperty>

  <to uri="{{widgetFetcher}}"/>

  <log message="fizzId = ${property[fizzId]}" loggingLevel="ERROR"/>
</route>

And then:

public Widget getWidgetToProcess(@ExchangeProperty("fizzId") final String fizzId) {
  // get the next widget id from the database
  final Integer firstWidgetId = widgetDao.getFirstSubmittedWidgetId();

  if (someMethodReturnsTrue()) {
    // Does this actually get saved outside the 
    log.info("About to update fizzId...")
    fizzId = UUID.randomUUID().toString();
  }

  // Do lots of stuff with 'firstWidgetId' down here...
}

However at runtime the local assignment fizzId = ... does not seem to take as the log output reads:

About to update fizzId...
fizzId = null

So I think my processor is receiving a copy of the fizzId exchange property, but re-assiging its value inline doesn't actually modify the actual value for the rest of the route. Any idea as to how to do this?

Upvotes: 0

Views: 2473

Answers (2)

Steve Huston
Steve Huston

Reputation: 1590

Instead of passing in the property to the processor, accept the Exchange - then you can set the property on the exchange.

Upvotes: 2

Matt Pavlovich
Matt Pavlovich

Reputation: 4306

You probably need a reference to something higher up to set the value. Try using the annotation for the full property map @Properties, or having your WidgetFetcher implement Processor to get a reference to the full exchange.

ref: Camel annotations

Upvotes: 1

Related Questions