You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by toomanyedwards <to...@gmail.com> on 2015/05/07 17:04:18 UTC

How to bind context properties to method parameters?

Hi all,
  There are clearly defined ways to bind header and exchange property values
to method parameters via annotations as doc'd here:
http://camel.apache.org/parameter-binding-annotations.html.  My question is
what is the best practice to bind *context* properties to method parameters. 
Currently, I'm copying context properties to exchange properties before
invoking my methods so that I can access their values via parameter binding
(as exchange properties), but that definitely feels hacky to me.  Is there a
better way to bind context properties to method parameters?  If not, is
there a best practice to work around this? 

Thank!

-edward



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-bind-context-properties-to-method-parameters-tp5766819.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to bind context properties to method parameters?

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

You can likely bind using the @Simple language and refer to the
properties with an expression in the likes of something:
camelContext.properties['foo']

http://camel.apache.org/simple


On Thu, May 7, 2015 at 6:53 PM, toomanyedwards <to...@gmail.com> wrote:
> Yes, we want to be able to bind to the properties on the CamelContext.  The
> reason we want to do this is that we have config information that is
> effectively immutable for the life of the CamelContext  (timeouts, db config
> info, etc.) and our routes depend on this info.  When our routes execute we
> want to validate that all the preconditions for config data (Message
> headers, Exchange properties and Context properties) needed by the route are
> met.  To do this we have a bean for each of our routes that implements a
> "validateRoutePreconditons" method and we invoke this method via camel bean
> at the beginning of the route execution.  Example validator beans:
>
> public class RouteAValidatorBean
> {
>
>         public void validateRoutePreconditions(
>                 @Header(NAME_OF_HEADER_NEEDED_BY_ROUTE_A)String
> valueOfHeaderNeededByRouteA
>             )
>         {
>             // Check if needed header value is set and if not throw
> exception
>         }
> }
>
> public class RouteBValidatorBean
> {
>
>         public void validateRoutePreconditions(
>                 @Property(NAME_OF_EXCHANGE_PROPERTY_NEEDED_BY_ROUTE_B)String
> valueOfExchangePropertyNeededByRouteB
>             )
>         {
>             // Check if needed exchange property value is set and if not
> throw exception
>         }
> }
>
>
> We'd like to be able validate that the required camelcontext properties are
> set in a similar fashion.  I guess one option would be to have a parameter
> of type CamelContext on our validate method and then Camel should
> dynamically bind this right?  Then in our validator method we'd have to
> explicitly get the properties we care about from the context.  Not quite as
> clean as being able to just bind context properties that we care about (as
> we can do for headers and exchange properties), but might be the best
> solution.  Is there a better approach to this?  Are CamelContext properties
> the appropriate place to cache static config info that is immutable or is
> there a preferred way to do this?
>
> Thanks for your insight.
>
> -e
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-bind-context-properties-to-method-parameters-tp5766819p5766828.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/

Re: How to bind context properties to method parameters?

Posted by toomanyedwards <to...@gmail.com>.
Yes, we want to be able to bind to the properties on the CamelContext.  The
reason we want to do this is that we have config information that is
effectively immutable for the life of the CamelContext  (timeouts, db config
info, etc.) and our routes depend on this info.  When our routes execute we
want to validate that all the preconditions for config data (Message
headers, Exchange properties and Context properties) needed by the route are
met.  To do this we have a bean for each of our routes that implements a
"validateRoutePreconditons" method and we invoke this method via camel bean
at the beginning of the route execution.  Example validator beans:

public class RouteAValidatorBean
{

	public void validateRoutePreconditions(
                @Header(NAME_OF_HEADER_NEEDED_BY_ROUTE_A)String
valueOfHeaderNeededByRouteA
	    )
	{
            // Check if needed header value is set and if not throw
exception
        }
}

public class RouteBValidatorBean
{

	public void validateRoutePreconditions(
                @Property(NAME_OF_EXCHANGE_PROPERTY_NEEDED_BY_ROUTE_B)String
valueOfExchangePropertyNeededByRouteB
	    )
	{
            // Check if needed exchange property value is set and if not
throw exception
        }
}


We'd like to be able validate that the required camelcontext properties are
set in a similar fashion.  I guess one option would be to have a parameter
of type CamelContext on our validate method and then Camel should
dynamically bind this right?  Then in our validator method we'd have to
explicitly get the properties we care about from the context.  Not quite as
clean as being able to just bind context properties that we care about (as
we can do for headers and exchange properties), but might be the best
solution.  Is there a better approach to this?  Are CamelContext properties
the appropriate place to cache static config info that is immutable or is
there a preferred way to do this?

Thanks for your insight.

-e



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-bind-context-properties-to-method-parameters-tp5766819p5766828.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to bind context properties to method parameters?

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Are you talking about properties stored on CamelContext that is hardly
in use at all? Or which properties are you talking about?

On Thu, May 7, 2015 at 5:04 PM, toomanyedwards <to...@gmail.com> wrote:
> Hi all,
>   There are clearly defined ways to bind header and exchange property values
> to method parameters via annotations as doc'd here:
> http://camel.apache.org/parameter-binding-annotations.html.  My question is
> what is the best practice to bind *context* properties to method parameters.
> Currently, I'm copying context properties to exchange properties before
> invoking my methods so that I can access their values via parameter binding
> (as exchange properties), but that definitely feels hacky to me.  Is there a
> better way to bind context properties to method parameters?  If not, is
> there a best practice to work around this?
>
> Thank!
>
> -edward
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-bind-context-properties-to-method-parameters-tp5766819.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/