camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brad Johnson <brad.john...@mediadriver.com>
Subject Re: Seeking to understand properties
Date Thu, 17 Nov 2016 00:33:30 GMT
Steve,

The unfortunate part is there are a few different properties.  The
propertyplaceholder is a global set of properties and is really how you
configure your bundle/jar from defaults and external properties. These are
different from the properties in the camel context and different from the
Exchange properties. Most of your concerns will be about using the accessor
appropriate to get to the global properties or setting properties on your
message (which are called headers in that context).

An example of how this manifests itself is in a Blueprint.xml file, if you
have a properties placheholder header with default properties or load those
from an external .cfg file, outside the CamelContext you use the "${foo}"
syntax to access the global properties.  Inside the CamelContext you use
"{{foo}}" to indicate that you are not referencing the CamelContext's
properties but the global properties for the bundle. If you use the
"${foo}" syntax inside the CamelContext I believe you are only referencing
the properties that you specifically set on the CamelContext itself.  Hence
the need for different access mechanisms.  Here's a sample:

//External to the conext
         <bean id="myBean" class="com.foo.MyBean">
<property name="foo" value="${properties-placeholder-specified-foovalue}" />
</bean>

<camelContext xmlns="http://camel.apache.org/schema/blueprint">
//Internal to the context.
<route>
<from uri="{{properties-placeholder-specified-uri}}" />
<bean ref="myBean" />
<to uri="{{another-properties-placeholder-speicified-uri}}" />
</route>

The different syntax is simply a scoping specifier.  Inside the
CamelContext if I used the scope like this:

<camelContext xmlns="http://camel.apache.org/schema/blueprint">
//Internal to the context.
<route>
<from uri="${properties-placeholder-specified-uri}" />
<bean ref="myBean" />
<to uri="${another-properties-placeholder-speicified-uri}" />
</route>

It would end up with nulls as nothing has been set on the CamelContext
specifying values for those two keys.

I don't use the CamelContext properties that much so want to defer to Claus
and the documents for certitude.  But if memory serves this also means that
when you set a CamelContext property and your message passes to a new
CamelContext that property does not propagate with it.  That's why I'm
usually using either global properties or I set "header" values (a
different properties map) which will traverse across CamelContexts. A
CamelContext property is used specifically for data that I'm using locally
and not something I think about much.

So I believe the confusion you're seeing is the difference between two
different sets of properties.  Most of the time I'm using headers to pass
meta-data along with my messages or even bundling that meta-data into my
data bean itself in some cases so that the meta-data I care about passes
along the route in my ${body}.




On Wed, Nov 16, 2016 at 5:50 PM, Steve Huston <shuston@riverace.com> wrote:

> Yow - that works!
>
> Thanks, Claus! I understand the IoC aspect of it now.
>
> I still don't understand why getProperty("name") can't work when
> resolvePropertyPlaceholders("{{name}}") will.
> In my mind they should be going to the same set of name/value to look up
> 'name' but they're not. Why is that?
>
> Thank you very much,
> -Steve
>
> > -----Original Message-----
> > From: Claus Ibsen [mailto:claus.ibsen@gmail.com]
> > Sent: Wednesday, November 16, 2016 4:25 PM
> > To: users@camel.apache.org
> > Subject: Re: Seeking to understand properties
> >
> > That is because @PropertyInject is IoC injected by Camel or Spring when
> you
> > use it to setup the beans. But in your unit test you create that class
> yourself
> > with the new constructor, then its not dependency injection for such
> things.
> >
> > Instead use Camel to create it with
> >
> > return context().getInjector().newInstance(MyRouteBuilder.class);
> >
> >
> >
> > On Wed, Nov 16, 2016 at 6:51 PM, Steve Huston <shuston@riverace.com>
> > wrote:
> > > Thank you for the responses thus far. I still have some confusion and
> an
> > observation.
> > >
> > > First, I find it very confusing and non-intuitive that the
> "properties" in
> > CamelContext are apparently not the "properties" in property placeholder.
> > From my reading of the various doc pages it seemed like property
> > placeholders are ways to get values from the properties substituted into
> > routes. But it seems like maybe there are more than one set, or type, of
> > properties. Is this true?
> > >
> > > My observation... in my test below I added @PropertyInject("datafile")
> on
> > the line above "private String filename;" and that _did_ get the value
> set via
> > useOverridePropertiesWithPropertiesComponent(). However, if I add the
> > same @PropertyInject to a member in the MyRouteBuilder class, that _does
> > not_ get the value.
> > >
> > > Can someone please help me understand what's going on here?
> > >
> > > Thanks,
> > > -Steve
> > >
> > >> -----Original Message-----
> > >> From: Claus Ibsen [mailto:claus.ibsen@gmail.com]
> > >> Sent: Wednesday, November 16, 2016 9:59 AM
> > >> To: users@camel.apache.org
> > >> Subject: Re: Seeking to understand properties
> > >>
> > >> Hi
> > >>
> > >> context().getProperty("dataFile");
> > >>
> > >> is not property placeholders. Its just generic key/value pairs on
> > >> CamelContext. See the javadoc of the API.
> > >>
> > >> For property placeholders take a look at
> > >> http://camel.apache.org/using- propertyplaceholder.html
> > >>
> > >> On Tue, Nov 15, 2016 at 11:58 PM, Steve Huston <shuston@riverace.com>
> > >> wrote:
> > >> > I am struggling to understand something I fear is very basic, but
I
> > >> > just don't
> > >> see it. Hoping someone can help me understand this.
> > >> >
> > >> > Properties... I can use them in route definitions, but I don't seem
> > >> > to be able
> > >> to obtain their values in regular Java code.
> > >> >
> > >> > Example (using Camel 2.16.3):
> > >> >
> > >> > I have a route builder:
> > >> >
> > >> > public class MyRouteBuilder extends RouteBuilder {
> > >> >     public void configure() {
> > >> >         from("file:{{dataFile}}?noop=true")
> > >> >          .to("file:target/messages/others");
> > >> >     }
> > >> > }
> > >> >
> > >> > And a unit test:
> > >> >
> > >> > @RunWith(JUnit4.class)
> > >> > public class UnitTest extends CamelTestSupport {
> > >> >
> > >> >         private String fileName;
> > >> >
> > >> >             @Override
> > >> >             protected RouteBuilder createRouteBuilder() {
> > >> >                 return new MyRouteBuilder();
> > >> >             }
> > >> >
> > >> >             @Override
> > >> >             public Boolean ignoreMissingLocationWithPrope
> rtiesComponent()
> > {
> > >> >                 return true;
> > >> >             }
> > >> >
> > >> >             @Override
> > >> >             protected Properties
> > >> useOverridePropertiesWithPropertiesComponent() {
> > >> >                     Properties override = new Properties();
> > >> >                     override.put("dataFile", "overridden-file");
> > >> >                     return override;
> > >> >             }
> > >> >
> > >> >             @Test
> > >> >                 public void testPropertyGetting() throws Exception
{
> > >> >                 fileName = context().getProperty("dataFile");
> > >> >                 assert(fileName != null);
> > >> >             }
> > >> > }
> > >> >
> > >> > When I run this test, the output includes:
> > >> >
> > >> > INFO  Route: route1 started and consuming from:
> > >> > Endpoint[file://overridden-file?noop=true]
> > >> >
> > >> > So the override property set above did get filled into the route
> > definition.
> > >> > However, when I try to get the property in the
> > >> > testPropertyGetting() method, the value is null
> > >> >
> > >> > Why???
> > >> >
> > >> > -Steve
> > >> >
> > >>
> > >>
> > >>
> > >> --
> > >> Claus Ibsen
> > >> -----------------
> > >> http://davsclaus.com @davsclaus
> > >> Camel in Action 2: https://www.manning.com/ibsen2
> >
> >
> >
> > --
> > Claus Ibsen
> > -----------------
> > http://davsclaus.com @davsclaus
> > Camel in Action 2: https://www.manning.com/ibsen2
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message