You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Purple Tea <ma...@hotmail.com> on 2012/04/30 21:26:48 UTC

Invoking a bean returned by a previous bean invocation

Hi,

I have a route that returns a bean, and I would like to call a method in the
bean that was returned (which isn't in the registry). 
I've been looking at the bean binding documentation but I didn't find this
scenario.

Bellow is a sample code:

<bean id="myBean" class="com.MyBean">
</bean>

<route>
      <from uri="direct:start" />
      <to ref="myBean" method="getBean" />
       
      <to ref="myReturnedBean" method="doSomething" />
</route>


class MyBean  {
     MyReturnedBean getBean () {
         return new MyReturnedBean();
    }
}

class MyReturnedBean {
     void doSomething() {
     }
}

I really appreciate your help.

--
View this message in context: http://camel.465427.n5.nabble.com/Invoking-a-bean-returned-by-a-previous-bean-invocation-tp5676696.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Invoking a bean returned by a previous bean invocation

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, May 3, 2012 at 8:24 PM, gavindscott <ga...@gmail.com> wrote:
>
> Claus Ibsen-2 wrote
>>
>> On Tue, May 1, 2012 at 8:11 PM, Purple Tea &lt;maisa_manoel@&gt; wrote:
>>> Thank you for your reply.
>>>
>>> Does the simple language also allows us to send multiple method
>>> parameters
>>> like the new features offered in the bean binding?
>>>
>>
>> Yes, just provide a method signature such with the parameters
>> separated by comma.
>> There is a limitation on what can be passed in as parameters, but
>> thats documented on that bean binding page
>>
>> <simple>${body.doSomething("Hi Camel", ${header.bar}, true)}</simple>
>> [ SNIP ]
>>
>
> This seems to only work when the parameters are simple values such as
> strings, integers, etc.  For example:
>
>        public void testBodyOgnlOnAnimalWithOgnlParams() throws Exception {
>                exchange.getIn().setBody(new Animal("tiger", 13));
>                exchange.getIn().setHeader("friend", new Animal("donkey", 4));
>                assertExpression("${body.setFriend(${header.friend})}", null);
>        }
>
> Fails with org.apache.camel.NoTypeConversionAvailableException: No type
> converter available to convert from type: java.lang.String to the required
> type: org.apache.camel.language.simple.SimpleTest.Animal with value donkey
>
> Is that a known limitation?
>

Yes its a limitation currently. I have logged a JIRA
https://issues.apache.org/jira/browse/CAMEL-5252


> --
> Gavin Scott
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Invoking-a-bean-returned-by-a-previous-bean-invocation-tp5676696p5684000.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Invoking a bean returned by a previous bean invocation

Posted by Rocco Gallo Citera <ga...@gmail.com>.
Gavin,
Type Converters only works with common types, but you can write your
own if you need it (ie, Animal).

On Thu, May 3, 2012 at 3:24 PM, gavindscott <ga...@gmail.com> wrote:
>
> Claus Ibsen-2 wrote
>>
>> On Tue, May 1, 2012 at 8:11 PM, Purple Tea &lt;maisa_manoel@&gt; wrote:
>>> Thank you for your reply.
>>>
>>> Does the simple language also allows us to send multiple method
>>> parameters
>>> like the new features offered in the bean binding?
>>>
>>
>> Yes, just provide a method signature such with the parameters
>> separated by comma.
>> There is a limitation on what can be passed in as parameters, but
>> thats documented on that bean binding page
>>
>> <simple>${body.doSomething("Hi Camel", ${header.bar}, true)}</simple>
>> [ SNIP ]
>>
>
> This seems to only work when the parameters are simple values such as
> strings, integers, etc.  For example:
>
>        public void testBodyOgnlOnAnimalWithOgnlParams() throws Exception {
>                exchange.getIn().setBody(new Animal("tiger", 13));
>                exchange.getIn().setHeader("friend", new Animal("donkey", 4));
>                assertExpression("${body.setFriend(${header.friend})}", null);
>        }
>
> Fails with org.apache.camel.NoTypeConversionAvailableException: No type
> converter available to convert from type: java.lang.String to the required
> type: org.apache.camel.language.simple.SimpleTest.Animal with value donkey
>
> Is that a known limitation?
>
> --
> Gavin Scott
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Invoking-a-bean-returned-by-a-previous-bean-invocation-tp5676696p5684000.html
> Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Invoking a bean returned by a previous bean invocation

Posted by gavindscott <ga...@gmail.com>.
Claus Ibsen-2 wrote
> 
> On Tue, May 1, 2012 at 8:11 PM, Purple Tea &lt;maisa_manoel@&gt; wrote:
>> Thank you for your reply.
>>
>> Does the simple language also allows us to send multiple method
>> parameters
>> like the new features offered in the bean binding?
>>
> 
> Yes, just provide a method signature such with the parameters
> separated by comma.
> There is a limitation on what can be passed in as parameters, but
> thats documented on that bean binding page
> 
> <simple>${body.doSomething("Hi Camel", ${header.bar}, true)}</simple>
> [ SNIP ]
> 

This seems to only work when the parameters are simple values such as
strings, integers, etc.  For example:

	public void testBodyOgnlOnAnimalWithOgnlParams() throws Exception {
		exchange.getIn().setBody(new Animal("tiger", 13));
		exchange.getIn().setHeader("friend", new Animal("donkey", 4));
		assertExpression("${body.setFriend(${header.friend})}", null);
	}

Fails with org.apache.camel.NoTypeConversionAvailableException: No type
converter available to convert from type: java.lang.String to the required
type: org.apache.camel.language.simple.SimpleTest.Animal with value donkey

Is that a known limitation?

--
Gavin Scott

--
View this message in context: http://camel.465427.n5.nabble.com/Invoking-a-bean-returned-by-a-previous-bean-invocation-tp5676696p5684000.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Invoking a bean returned by a previous bean invocation

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, May 1, 2012 at 8:11 PM, Purple Tea <ma...@hotmail.com> wrote:
> Thank you for your reply.
>
> Does the simple language also allows us to send multiple method parameters
> like the new features offered in the bean binding?
>

Yes, just provide a method signature such with the parameters
separated by comma.
There is a limitation on what can be passed in as parameters, but
thats documented on that bean binding page

<simple>${body.doSomething("Hi Camel", ${header.bar}, true)}</simple>

For example from an unit test

    public void testBodyOgnlOnStringWithOgnlParams() throws Exception {
        exchange.getIn().setBody("Camel");
        exchange.getIn().setHeader("max", 4);
        exchange.getIn().setHeader("min", 2);

        assertExpression("${body.substring(${header.min},
${header.max})}", "me");
    }


>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Invoking-a-bean-returned-by-a-previous-bean-invocation-tp5676696p5678641.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Invoking a bean returned by a previous bean invocation

Posted by Purple Tea <ma...@hotmail.com>.
Thank you for your reply.

Does the simple language also allows us to send multiple method parameters
like the new features offered in the bean binding?


--
View this message in context: http://camel.465427.n5.nabble.com/Invoking-a-bean-returned-by-a-previous-bean-invocation-tp5676696p5678641.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Invoking a bean returned by a previous bean invocation

Posted by Claus Ibsen <cl...@gmail.com>.
On Mon, Apr 30, 2012 at 9:26 PM, Purple Tea <ma...@hotmail.com> wrote:
> Hi,
>
> I have a route that returns a bean, and I would like to call a method in the
> bean that was returned (which isn't in the registry).
> I've been looking at the bean binding documentation but I didn't find this
> scenario.
>
> Bellow is a sample code:
>
> <bean id="myBean" class="com.MyBean">
> </bean>
>
> <route>
>      <from uri="direct:start" />
>      <to ref="myBean" method="getBean" />
>
>      <to ref="myReturnedBean" method="doSomething" />
> </route>
>

You would need to use a dynamic language or simple language to invoke
a method on the message body.
And use the message translator EIP

For example the simple language supports OGNL like method invocation
chains. See more at
http://camel.apache.org/simple

And for bean parameter bindings you can see details at
http://camel.apache.org/bean-binding.html

So you should be able to do something a like this:

 <route>
      <from uri="direct:start" />
      <to ref="myBean" method="getBean" />
      <transform><simple>${body.doSomething}</simple></transform>
 </route>


>
> class MyBean  {
>     MyReturnedBean getBean () {
>         return new MyReturnedBean();
>    }
> }
>
> class MyReturnedBean {
>     void doSomething() {
>     }
> }
>
> I really appreciate your help.
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Invoking-a-bean-returned-by-a-previous-bean-invocation-tp5676696.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/