2
votes

I need to persist a single string value which changes hourly and is fetched by a route for other routes to use. I'm using the Spring XML DSL.

I've got it working fine, but it seems clumsy. I have a java class with a setter and a getter to wrap the string and I set it with:

<to uri="bean:store?method=setValue"/>

And get it back into the body in another route with:

<to uri="bean:store?method=getValue"/>

Is there a pure Spring way of doing this in the xml without needing a bespoke java class?

1
This is trivial when using Java DSL since you can just refer to some value container object, when using XML DSL I think there is now other way around that. You can use camel:setProperty to set property on exchange, but for the routes in question that might not apply if they don't share exchange. So I am afraid there is no better way in XML DSL. - hgrey
Thanks @hgrey. At least that's two of us think that way. - Julian
Not a spring person myself but why don't you store it in a camel cache and do lookup when you need the value? - Souciance Eqdam Rashti
Because I hadn't spotted it. Thanks, I'm going to check your blog out too. - Julian
Cool, glad to help. Like Raul mentions, it is relatively easy to use the Cache for basic state lookup. - Souciance Eqdam Rashti

1 Answers

4
votes

You have several options:

  1. Use a cache component such as Apache Ignite, EhCache, etc. Although this is overkill for such a simple scenario as yours. I don't recommend it.
  2. Use a JDK AtomicReference as your bean, rather than creating a dedicated class just for storage.
    • Invoke the set method to store a value.
    • Invoke the get to recover the value.

Obviously you won't get rid of the steps in the route to store/fetch the value, but you can scrap away your custom class.