0

Is there a one liner Java DSL to do the following:

.process(new Processor() {
    @Override
    public void process(Exchange exchange) throws Exception {
        exchange.setProperty("name", new MyBean());
    }
})

The goal is to have a property 'name' initialised with an new instance of MyBean class before other logic in the route.

I think it must be in the form of:

.setProperty("name", ... expression ...)

I cannot find the right expression.

Thanks.

2
  • I can make a custom expression: <br/> public static Expression newInstance(final Class clazz) { return new ExpressionAdapter() { @Override public Object evaluate(Exchange exchange) { try { return clazz.newInstance(); } catch (Exception e) { throw new IllegalArgumentException("Cannot instantiate class '" + clazz.getName() + "': " + e.getMessage()); } } }; } But surely there must be an existing expression.. Commented Jul 6, 2015 at 7:06
  • Ahhrgg, sorry not good at the comment syntax. Commented Jul 6, 2015 at 7:15

2 Answers 2

4

i used

.setProperty("name").exchange(ex -> new MyBean())
Sign up to request clarification or add additional context in comments.

Comments

-1

yes there is.

from("direct:in")
    .setProperty("key").constant(new MyBean())
    .to("direct:out")

You can also use .ref("bean-ref") to refer to something in the registry

3 Comments

Constant will give the same object instance with each new message. This is not what i need.
Ref with the bean set to prototype will give you a new object per exchange, otherwise you need to do it with a processor like your question states, or else call a bean which does the same thing. Or if you really really want to keep in the in DSL then this might work .setProperty("key").groovy("new foo.bar.MyBean()"), but I didn't test it
as JStefan already stated "Constant will give the same object instance" so misleading answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.