Extract the name of the Method from a Bean invokation.
Hi All, I have a Camel Proxy that pushes "BeanInvokation" exchanges to a camel route, and I want to route these messages depending on the method name that is invoked. Is there any way to extract this information and put into an header so to use it in a ? For example, considering the following XML code: http://camel.apache.org/schema/spring "> ${header.methodName} == 'myMethod1' ${header.methodName} == 'myMethod2' I'm searching for a solution to be used between the two tags. I know I can write a processor, but I will have problems deploying it to the classpath where I put the route, so I need a solution that rely on the default camel capability (like using simple language or other expression languages available with camel). Any idea? Thank you all! Cristiano
Re: Extract the name of the Method from a Bean invokation.
mmm, it seems that "${body.method.name}" works: ${body.method.name} Any comment about this solution? (is it good?) What happens if the body is not of class org.apache.camel.component.bean.BeanInvocation? Is there a way to check if body is of the right instance? Many thanks!!! Cristiano 2013/7/10 Cristiano Costantini > Hi All, > > I have a Camel Proxy that pushes "BeanInvokation" exchanges to a camel > route, and I want to route these messages depending on the method name that > is invoked. > > Is there any way to extract this information and put into an header so to > use it in a ? > > > For example, considering the following XML code: > > http://camel.apache.org/schema/spring";> > serviceUrl="direct:entry" /> > > > > > > > > > > ${header.methodName} == 'myMethod1' > > > > ${header.methodName} == 'myMethod2' > > > > > > > > > > > I'm searching for a solution to be used between the two tags. > > I know I can write a processor, but I will have problems deploying it to > the classpath where I put the route, so I need a solution that rely on the > default camel capability (like using simple language or other expression > languages available with camel). > > > Any idea? > Thank you all! > > Cristiano > > >
Re: Extract the name of the Method from a Bean invokation.
There is a ? operator, so you can do ${body?.method?.name Then it will be null if the body if the body doesnt have a getMethod().getName() There is also a bodyAs(type) to use the Camel type converter. On Wed, Jul 10, 2013 at 4:48 PM, Cristiano Costantini wrote: > mmm, it seems that "${body.method.name}" works: > > > ${body.method.name} > > > > Any comment about this solution? (is it good?) > > What happens if the body is not of class > org.apache.camel.component.bean.BeanInvocation? > Is there a way to check if body is of the right instance? > > Many thanks!!! > > Cristiano > > > > > > 2013/7/10 Cristiano Costantini > >> Hi All, >> >> I have a Camel Proxy that pushes "BeanInvokation" exchanges to a camel >> route, and I want to route these messages depending on the method name that >> is invoked. >> >> Is there any way to extract this information and put into an header so to >> use it in a ? >> >> >> For example, considering the following XML code: >> >> http://camel.apache.org/schema/spring";> >> > serviceUrl="direct:entry" /> >> >> >> >> >> >> >> >> >> >> ${header.methodName} == 'myMethod1' >> >> >> >> ${header.methodName} == 'myMethod2' >> >> >> >> >> >> >> >> >> >> >> I'm searching for a solution to be used between the two tags. >> >> I know I can write a processor, but I will have problems deploying it to >> the classpath where I put the route, so I need a solution that rely on the >> default camel capability (like using simple language or other expression >> languages available with camel). >> >> >> Any idea? >> Thank you all! >> >> Cristiano >> >> >> -- Claus Ibsen - Red Hat, Inc. Email: cib...@redhat.com Twitter: davsclaus Blog: http://davsclaus.com Author of Camel in Action: http://www.manning.com/ibsen
Re: Extract the name of the Method from a Bean invokation.
, I would need exactly that, but I've tested sending a string with a producer template and I got this error: Failed to invoke method: ?.method?.name on null due to: org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to invoke method: method on null due to: org.apache.camel.component.bean.MethodNotFoundException: Method with name: method not found on bean: Hello World! of type: java.lang.String. Exchange[Message: Hello World!] caused by Failed to invoke method: method on null due to: org.apache.camel.component.bean.MethodNotFoundException: Method with name: method not found on bean: Hello World! of type: java.lang.String. Exchange[Message: Hello World!] again caused by Method with name: method not found on bean: Hello World! of type: java.lang.String. Exchange[Message: Hello World!] I'm using camel 2.10.4... Any suggestion? Thanks, Cristiano 2013/7/10 Claus Ibsen > There is a ? operator, so you can do > > ${body?.method?.name > > Then it will be null if the body if the body doesnt have a > getMethod().getName() > > There is also a bodyAs(type) to use the Camel type converter. > > > > On Wed, Jul 10, 2013 at 4:48 PM, Cristiano Costantini > wrote: > > mmm, it seems that "${body.method.name}" works: > > > > > > ${body.method.name} > > > > > > > > Any comment about this solution? (is it good?) > > > > What happens if the body is not of class > > org.apache.camel.component.bean.BeanInvocation? > > Is there a way to check if body is of the right instance? > > > > Many thanks!!! > > > > Cristiano > > > > > > > > > > > > 2013/7/10 Cristiano Costantini > > > >> Hi All, > >> > >> I have a Camel Proxy that pushes "BeanInvokation" exchanges to a camel > >> route, and I want to route these messages depending on the method name > that > >> is invoked. > >> > >> Is there any way to extract this information and put into an header so > to > >> use it in a ? > >> > >> > >> For example, considering the following XML code: > >> > >> http://camel.apache.org/schema/spring";> > >> >> serviceUrl="direct:entry" /> > >> > >> > >> > >> > >> > >> > >> > >> > >> > >> ${header.methodName} == 'myMethod1' > >> > >> > >> > >> ${header.methodName} == 'myMethod2' > >> > >> > >> > >> > >> > >> > >> > >> > >> > >> > >> I'm searching for a solution to be used between the two > tags. > >> > >> I know I can write a processor, but I will have problems deploying it to > >> the classpath where I put the route, so I need a solution that rely on > the > >> default camel capability (like using simple language or other expression > >> languages available with camel). > >> > >> > >> Any idea? > >> Thank you all! > >> > >> Cristiano > >> > >> > >> > > > > -- > Claus Ibsen > - > Red Hat, Inc. > Email: cib...@redhat.com > Twitter: davsclaus > Blog: http://davsclaus.com > Author of Camel in Action: http://www.manning.com/ibsen >
Re: Extract the name of the Method from a Bean invokation.
Hi Ah yeah the null safe doesn't apply for methods not found. I am not sure if this is a good idea to add support for that. As if you have a typo in the method name, then the null safe will ignore that also. And adding more stuff to the syntax would just confuse even more, eg For example with a double ?? to indicate ignore method not found. ${body??.method??.name} The ? is null safe, so its if the getMethod returns null, then break out of the OGNL path. On Wed, Jul 10, 2013 at 7:01 PM, Cristiano Costantini wrote: > , > I would need exactly that, but I've tested sending a string with a producer > template and I got this error: > > Failed to invoke method: ?.method?.name on null due to: > org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to > invoke method: method on null due to: > org.apache.camel.component.bean.MethodNotFoundException: Method with name: > method not found on bean: Hello World! of type: java.lang.String. > Exchange[Message: Hello World!] > > caused by > > Failed to invoke method: method on null due to: > org.apache.camel.component.bean.MethodNotFoundException: Method with name: > method not found on bean: Hello World! of type: java.lang.String. > Exchange[Message: Hello World!] > > > again caused by > > > Method with name: method not found on bean: Hello World! of type: > java.lang.String. Exchange[Message: Hello World!] > > > I'm using camel 2.10.4... > Any suggestion? > > Thanks, > Cristiano > > > > > > > 2013/7/10 Claus Ibsen > >> There is a ? operator, so you can do >> >> ${body?.method?.name >> >> Then it will be null if the body if the body doesnt have a >> getMethod().getName() >> >> There is also a bodyAs(type) to use the Camel type converter. >> >> >> >> On Wed, Jul 10, 2013 at 4:48 PM, Cristiano Costantini >> wrote: >> > mmm, it seems that "${body.method.name}" works: >> > >> > >> > ${body.method.name} >> > >> > >> > >> > Any comment about this solution? (is it good?) >> > >> > What happens if the body is not of class >> > org.apache.camel.component.bean.BeanInvocation? >> > Is there a way to check if body is of the right instance? >> > >> > Many thanks!!! >> > >> > Cristiano >> > >> > >> > >> > >> > >> > 2013/7/10 Cristiano Costantini >> > >> >> Hi All, >> >> >> >> I have a Camel Proxy that pushes "BeanInvokation" exchanges to a camel >> >> route, and I want to route these messages depending on the method name >> that >> >> is invoked. >> >> >> >> Is there any way to extract this information and put into an header so >> to >> >> use it in a ? >> >> >> >> >> >> For example, considering the following XML code: >> >> >> >> http://camel.apache.org/schema/spring";> >> >> > >> serviceUrl="direct:entry" /> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> ${header.methodName} == 'myMethod1' >> >> >> >> >> >> >> >> ${header.methodName} == 'myMethod2' >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> I'm searching for a solution to be used between the two >> tags. >> >> >> >> I know I can write a processor, but I will have problems deploying it to >> >> the classpath where I put the route, so I need a solution that rely on >> the >> >> default camel capability (like using simple language or other expression >> >> languages available with camel). >> >> >> >> >> >> Any idea? >> >> Thank you all! >> >> >> >> Cristiano >> >> >> >> >> >> >> >> >> >> -- >> Claus Ibsen >> - >> Red Hat, Inc. >> Email: cib...@redhat.com >> Twitter: davsclaus >> Blog: http://davsclaus.com >> Author of Camel in Action: http://www.manning.com/ibsen >> -- Claus Ibsen - Red Hat, Inc. Email: cib...@redhat.com Twitter: davsclaus Blog: http://davsclaus.com Author of Camel in Action: http://www.manning.com/ibsen
Re: Extract the name of the Method from a Bean invokation.
Hi Claus, thanks again, yes I understand the problem, and I agree that the Simple language should be simple ! While for my specific case, I've found that it exists an "is" operator in simple language to test instanceof : ${in.header.type} is 'java.lang.String' I can use it to check if the body is an instance of BeanInvocation, then use the ?. operator, else route the message somewhere else. For example I can change the original XML route in this way (it is a bit verbose but I hope it should work): http://camel.apache.org/schema/spring "> ${body} is 'org.apache.camel.component.bean.BeanInvocation' ${body?.method?.name} ${header.methodName} == 'myMethod1' ${header.methodName} == 'myMethod2' Please note that I've not tested yet the above route!! And again, any improvement is welcome. ;-) Cristiano 2013/7/11 Claus Ibsen > Hi > > Ah yeah the null safe doesn't apply for methods not found. > I am not sure if this is a good idea to add support for that. As if > you have a typo in the method name, then the null safe will ignore > that also. > > And adding more stuff to the syntax would just confuse even more, eg > > For example with a double ?? to indicate ignore method not found. > > ${body??.method??.name} > > The ? is null safe, so its if the getMethod returns null, then break > out of the OGNL path. > > > > > > On Wed, Jul 10, 2013 at 7:01 PM, Cristiano Costantini > wrote: > > , > > I would need exactly that, but I've tested sending a string with a > producer > > template and I got this error: > > > > Failed to invoke method: ?.method?.name on null due to: > > org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to > > invoke method: method on null due to: > > org.apache.camel.component.bean.MethodNotFoundException: Method with > name: > > method not found on bean: Hello World! of type: java.lang.String. > > Exchange[Message: Hello World!] > > > > caused by > > > > Failed to invoke method: method on null due to: > > org.apache.camel.component.bean.MethodNotFoundException: Method with > name: > > method not found on bean: Hello World! of type: java.lang.String. > > Exchange[Message: Hello World!] > > > > > > again caused by > > > > > > Method with name: method not found on bean: Hello World! of type: > > java.lang.String. Exchange[Message: Hello World!] > > > > > > I'm using camel 2.10.4... > > Any suggestion? > > > > Thanks, > > Cristiano > > > > > > > > > > > > > > 2013/7/10 Claus Ibsen > > > >> There is a ? operator, so you can do > >> > >> ${body?.method?.name > >> > >> Then it will be null if the body if the body doesnt have a > >> getMethod().getName() > >> > >> There is also a bodyAs(type) to use the Camel type converter. > >> > >> > >> > >> On Wed, Jul 10, 2013 at 4:48 PM, Cristiano Costantini > >> wrote: > >> > mmm, it seems that "${body.method.name}" works: > >> > > >> > > >> > ${body.method.name} > >> > > >> > > >> > > >> > Any comment about this solution? (is it good?) > >> > > >> > What happens if the body is not of class > >> > org.apache.camel.component.bean.BeanInvocation? > >> > Is there a way to check if body is of the right instance? > >> > > >> > Many thanks!!! > >> > > >> > Cristiano > >> > > >> > > >> > > >> > > >> > > >> > 2013/7/10 Cristiano Costantini > >> > > >> >> Hi All, > >> >> > >> >> I have a Camel Proxy that pushes "BeanInvokation" exchanges to a > camel > >> >> route, and I want to route these messages depending on the method > name > >> that > >> >> is invoked. > >> >> > >> >> Is there any way to extract this information and put into an header > so > >> to > >> >> use it in a ? > >> >> > >> >> > >> >> For example, considering the following XML code: > >> >> > >> >> http://camel.apache.org/schema/spring";> > >> >> >> >> serviceUrl="direct:entry" /> > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> ${header.methodName} == 'myMethod1' > >> >> > >> >> > >> >> > >> >> ${header.methodName} == 'myMethod2' > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> I'm searching for a solution to be used between the two > >> tags. > >> >> > >> >> I know I can write a processor, but I will have problems deploying > it to > >> >> the classpath where I put the route, so I need a solution that rely > on > >> the > >> >> default camel capability (like using simple language or other > expression > >> >> languages available with camel). > >> >> > >> >> > >> >> Any idea? > >> >> Thank you all! > >> >> > >> >> Cristiano > >> >> > >> >> > >> >> > >> > >> > >> > >> -- > >> Claus Ibsen > >> - > >> Red Hat, Inc. > >> Email: cib...@redhat.com > >> Twitter: davsclaus > >> Blog: http://davsclaus.com > >> Author of Camel in Action: http://www.manning.com/ibsen > >> > > > > -- > Claus Ibsen > - > Red Hat, Inc. > Email: cib...@redhat.com > Twitter: davsclaus > Blog: http://davsclaus.com > Author of Camel in Act
Re: Extract the name of the Method from a Bean invokation.
Hi Claus and all, I struggled to better write the previous route, I've come to this final solution. It seem to me ok Thank you for the support ;-) Cristiano http://camel.apache.org/schema/spring "> ${body} is not 'org.apache.camel.component.bean.BeanInvocation' ${body?.method?.name} == 'myMethod1' ${body?.method?.name} == 'myMethod2' 2013/7/11 Cristiano Costantini > Hi Claus, > thanks again, > > yes I understand the problem, and I agree that the Simple language should > be simple ! > > While for my specific case, I've found that it exists an "is" operator in > simple language to test instanceof : > ${in.header.type} is 'java.lang.String' > > I can use it to check if the body is an instance of BeanInvocation, then > use the ?. operator, else route the message somewhere else. > > For example I can change the original XML route in this way (it is a bit > verbose but I hope it should work): > > http://camel.apache.org/schema/spring";> > serviceUrl="direct:step1" /> > > > > > > ${body} is > 'org.apache.camel.component.bean.BeanInvocation' > > > > > > > > > > > > ${body?.method?.name} > > > > ${header.methodName} == 'myMethod1' > > > > ${header.methodName} == 'myMethod2' > > > > > > > > > > > Please note that I've not tested yet the above route!! > And again, any improvement is welcome. ;-) > > Cristiano > > > > > > > > > > > 2013/7/11 Claus Ibsen > >> Hi >> >> Ah yeah the null safe doesn't apply for methods not found. >> I am not sure if this is a good idea to add support for that. As if >> you have a typo in the method name, then the null safe will ignore >> that also. >> >> And adding more stuff to the syntax would just confuse even more, eg >> >> For example with a double ?? to indicate ignore method not found. >> >> ${body??.method??.name} >> >> The ? is null safe, so its if the getMethod returns null, then break >> out of the OGNL path. >> >> >> >> >> >> On Wed, Jul 10, 2013 at 7:01 PM, Cristiano Costantini >> wrote: >> > , >> > I would need exactly that, but I've tested sending a string with a >> producer >> > template and I got this error: >> > >> > Failed to invoke method: ?.method?.name on null due to: >> > org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to >> > invoke method: method on null due to: >> > org.apache.camel.component.bean.MethodNotFoundException: Method with >> name: >> > method not found on bean: Hello World! of type: java.lang.String. >> > Exchange[Message: Hello World!] >> > >> > caused by >> > >> > Failed to invoke method: method on null due to: >> > org.apache.camel.component.bean.MethodNotFoundException: Method with >> name: >> > method not found on bean: Hello World! of type: java.lang.String. >> > Exchange[Message: Hello World!] >> > >> > >> > again caused by >> > >> > >> > Method with name: method not found on bean: Hello World! of type: >> > java.lang.String. Exchange[Message: Hello World!] >> > >> > >> > I'm using camel 2.10.4... >> > Any suggestion? >> > >> > Thanks, >> > Cristiano >> > >> > >> > >> > >> > >> > >> > 2013/7/10 Claus Ibsen >> > >> >> There is a ? operator, so you can do >> >> >> >> ${body?.method?.name >> >> >> >> Then it will be null if the body if the body doesnt have a >> >> getMethod().getName() >> >> >> >> There is also a bodyAs(type) to use the Camel type converter. >> >> >> >> >> >> >> >> On Wed, Jul 10, 2013 at 4:48 PM, Cristiano Costantini >> >> wrote: >> >> > mmm, it seems that "${body.method.name}" works: >> >> > >> >> > >> >> > ${body.method.name} >> >> > >> >> > >> >> > >> >> > Any comment about this solution? (is it good?) >> >> > >> >> > What happens if the body is not of class >> >> > org.apache.camel.component.bean.BeanInvocation? >> >> > Is there a way to check if body is of the right instance? >> >> > >> >> > Many thanks!!! >> >> > >> >> > Cristiano >> >> > >> >> > >> >> > >> >> > >> >> > >> >> > 2013/7/10 Cristiano Costantini >> >> > >> >> >> Hi All, >> >> >> >> >> >> I have a Camel Proxy that pushes "BeanInvokation" exchanges to a >> camel >> >> >> route, and I want to route these messages depending on the method >> name >> >> that >> >> >> is invoked. >> >> >> >> >> >> Is there any way to extract this information and put into an header >> so >> >> to >> >> >> use it in a ? >> >> >> >> >> >> >> >> >> For example, considering the following XML code: >> >> >> >> >> >> http://camel.apache.org/schema/spring";> >> >> >> > >> >> serviceUrl="direct:entry" /> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> ${header.methodName} == 'myMethod1' >> >> >> >> >> >> >> >> >> >> >> >> ${header.methodName} == 'myMethod2' >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> I'm searching for a solution to be used between the two >> >> tags. >> >> >> >> >> >> I know I can write a processor, but I will have problems deploying >> it to