You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by nicolasduminil <ni...@simplex-software.fr> on 2016/05/31 10:01:43 UTC

Camel delta-spike: How to share properties between conexts ?

Hello,

I'm using delta-spike to manage camel routes as I've seen in the provided
CDI samples. I have the following route builder:

public class MyRouteBuilder extends RouteBuilder
{
  @Override
  @ContextName("...")
  public void configure()
  {
    from("ftp:{{ftp.path}}").log("Uploading file
${file:name}").to("file:{{file.path}}").log("Uploaded file ${file:name}
complete.");
  }

  @Produces
  @Named("properties")
  public PropertiesComponent properties(PropertiesParser parser)
  {
    PropertiesComponent component = new PropertiesComponent();
    component.setPropertiesParser(parser);
    return component;
  }
}

and I have the following class:

public class DeltaSpikeParser extends DefaultPropertiesParser {
  @Override
  public String parseProperty(String key, String value, Properties
properties) {
    return ConfigResolver.getPropertyValue(key);
  }
}

The {{...}} get injected from the apache-deltaspike.properties file.

In my unit tests I need to use the same property file, for example:

public class UnitTests extends CamelTestSupport
{
  @Override
  protected RouteBuilder createRouteBuilder()
  {
    return new RouteBuilder()
    {
      public void configure()
      {
        from("direct:start").to("ftp:{{ftp.path}}");
      }
    };
  }

But here {{ftp.path}} is not defined any more. How could i do this ?

Many thanks in advance,

Nicolas



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-delta-spike-How-to-share-properties-between-conexts-tp5783288.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel delta-spike: How to share properties between conexts ?

Posted by nicolasduminil <ni...@simplex-software.fr>.
Hi Antonin,

Yes, of course, my bad. It works with non-static as expected.

Many thanks for your help.

Kind regards,

Nicolas



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-delta-spike-How-to-share-properties-between-contexts-tp5783288p5783300.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel delta-spike: How to share properties between conexts ?

Posted by Antonin Stefanutti <an...@stefanutti.fr>.
Hi Nicolas,

Have you tried with a non-static member:

@Inject
@ConfigProperty(name = "ftp.path")
private String myProp;

Injection of static field is not allowed.

It should work without duplicating your logic.

Antonin

> On 31 May 2016, at 14:37, nicolasduminil <ni...@simplex-software.fr> wrote:
> 
> @Inject
>  @ConfigProperty(name = "ftp.path")
>  private static String myProp;


Re: Camel delta-spike: How to share properties between conexts ?

Posted by nicolasduminil <ni...@simplex-software.fr>.
Hi Antonin,

Many thanks for your reply. I have already seen these examples. My question
was concerning the access to the properties. I modified my unit test as
follows:

@RunWith(CamelCdiRunner.class)
public class UnitTests extends CamelTestSupport
{
  @Inject
  @ConfigProperty(name = "ftp.path")
  private static String myProp;
  @Inject
  private CamelContext context;

  @Test
  public void test()
  {
    System.out.println ("*** myProp: " + myProp);
    assertEquals(context.getStatus(), ServiceStatus.Started);
  }
} 

running the test I get:

*** myProp: null

So, in the route builder class I can see the properties in
apache-deltaspike.properties but not in the unit test. Hence, my question
was what am I supposed to do here as I don't want to duplicate the code in
public PropertiesComponent properties(PropertiesParser parser).

Kind regards,

Nicolas





--
View this message in context: http://camel.465427.n5.nabble.com/Camel-delta-spike-How-to-share-properties-between-contexts-tp5783288p5783297.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel delta-spike: How to share properties between conexts ?

Posted by Antonin Stefanutti <an...@stefanutti.fr>.
Hi Nicolas,

In your unit tests, you should use Camel CDI test runner instead of extending CamelTestSupport. This is used in the example that illustrates CDI and DeltaSpike configuration properties so that you can have a look at the accompanying test class. See [2] for more information.

[1]: https://github.com/apache/camel/tree/master/examples/camel-example-cdi-properties
[2]: http://camel.apache.org/cdi-testing.html#CDITesting-CamelCDITest

Antonin

> On 31 May 2016, at 12:01, nicolasduminil <ni...@simplex-software.fr> wrote:
> 
> Hello,
> 
> I'm using delta-spike to manage camel routes as I've seen in the provided
> CDI samples. I have the following route builder:
> 
> public class MyRouteBuilder extends RouteBuilder
> {
>  @Override
>  @ContextName("...")
>  public void configure()
>  {
>    from("ftp:{{ftp.path}}").log("Uploading file
> ${file:name}").to("file:{{file.path}}").log("Uploaded file ${file:name}
> complete.");
>  }
> 
>  @Produces
>  @Named("properties")
>  public PropertiesComponent properties(PropertiesParser parser)
>  {
>    PropertiesComponent component = new PropertiesComponent();
>    component.setPropertiesParser(parser);
>    return component;
>  }
> }
> 
> and I have the following class:
> 
> public class DeltaSpikeParser extends DefaultPropertiesParser {
>  @Override
>  public String parseProperty(String key, String value, Properties
> properties) {
>    return ConfigResolver.getPropertyValue(key);
>  }
> }
> 
> The {{...}} get injected from the apache-deltaspike.properties file.
> 
> In my unit tests I need to use the same property file, for example:
> 
> public class UnitTests extends CamelTestSupport
> {
>  @Override
>  protected RouteBuilder createRouteBuilder()
>  {
>    return new RouteBuilder()
>    {
>      public void configure()
>      {
>        from("direct:start").to("ftp:{{ftp.path}}");
>      }
>    };
>  }
> 
> But here {{ftp.path}} is not defined any more. How could i do this ?
> 
> Many thanks in advance,
> 
> Nicolas
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-delta-spike-How-to-share-properties-between-conexts-tp5783288.html
> Sent from the Camel - Users mailing list archive at Nabble.com.