You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Francois Papon <fr...@openobject.fr> on 2019/05/20 03:47:24 UTC

[java-dsl] - RouteBuilder

Hi,

I'm trying to use a RouteDefintion to configure a RouteBuilder and it
doesn't work.

It works by setting the route in the first test but the endpoint is not
publish in the second test...

Any ideas?


@Test
    public void publishHttpCamelContextTest() throws Exception {
        CamelContext camelContext = new DefaultCamelContext();
        Assertions.assertNotNull(camelContext);

        DefaultCamelContext.class.cast(camelContext).setName("camel-labs");
        camelContext.start();
        Assertions.assertTrue(camelContext.getStatus().isStarted());
        System.out.println("camel context started");

        RouteBuilder routeBuilder = new RouteBuilder() {
            @Override
            public void configure() throws Exception {

                from("jetty:http://localhost:9090/labs").
                        id("jetty-example").
                       
setHeader(Exchange.HTTP_RESPONSE_CODE,constant(200)).
                        setBody(constant("it works well!"));
            }
        };
        camelContext.addRoutes(routeBuilder);
        System.out.println("camel routes jetty added ->
http://localhost:9090/labs");
        Thread.sleep(10000L);
    }

    @Test
    public void publishHttpCamelContextWithDefinitionTest() throws
Exception {
        CamelContext camelContext = new DefaultCamelContext();
        Assertions.assertNotNull(camelContext);

        DefaultCamelContext.class.cast(camelContext).setName("camel-labs");
        camelContext.start();
        Assertions.assertTrue(camelContext.getStatus().isStarted());
        System.out.println("camel context started");

        final RouteDefinition definition = new RouteDefinition();
       
definition.from("jetty:http://localhost:9090/labs").id("jetty-example").
                setHeader(Exchange.HTTP_RESPONSE_CODE,constant(200)).
                setBody(constant("it works well!"));

        RouteBuilder routeBuilder = new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                System.out.println(definition);
                configureRoute(definition);
            }
        };
        camelContext.addRoutes(routeBuilder);
        System.out.println("camel routes with definition jetty added ->
http://localhost:9090/labs");
        Thread.sleep(10000L);
    }

regards,

-- 
François
fpapon@apache.org


Re: [java-dsl] - RouteBuilder

Posted by Francois Papon <fr...@openobject.fr>.
Just for sharing, here the code working:

@Test
    public void publishHttpCamelContextWithDefinitionTest() throws
Exception {
        CamelContext camelContext = new DefaultCamelContext();
        Assertions.assertNotNull(camelContext);

        DefaultCamelContext.class.cast(camelContext).setName("camel-labs");
        camelContext.start();
        Assertions.assertTrue(camelContext.getStatus().isStarted());
        System.out.println("camel context started");

        final RouteDefinition definition = new RouteDefinition();
       
definition.from("jetty:http://localhost:9090/labs").id("jetty-example").
                setHeader(Exchange.HTTP_RESPONSE_CODE,constant(200)).
                setBody(constant("it works well!"));

       
DefaultCamelContext.class.cast(camelContext).addRouteDefinition(definition);
        System.out.println("camel routes with definition jetty added ->
http://localhost:9090/labs");
        Thread.sleep(10000L);
    }

regards,

François
fpapon@apache.org

Le 20/05/2019 à 08:47, Francois Papon a écrit :
> Hi Claus,
>
> It works very well now :)
>
> Thanks for your help!
>
> regards,
>
> François
> fpapon@apache.org
>
> Le 20/05/2019 à 08:10, Claus Ibsen a écrit :
>> Hi
>>
>> You should add the definition via camel context api and not try to add
>> it via the route builder.
>>
>>
>> On Mon, May 20, 2019 at 5:47 AM Francois Papon
>> <fr...@openobject.fr> wrote:
>>> Hi,
>>>
>>> I'm trying to use a RouteDefintion to configure a RouteBuilder and it
>>> doesn't work.
>>>
>>> It works by setting the route in the first test but the endpoint is not
>>> publish in the second test...
>>>
>>> Any ideas?
>>>
>>>
>>> @Test
>>>     public void publishHttpCamelContextTest() throws Exception {
>>>         CamelContext camelContext = new DefaultCamelContext();
>>>         Assertions.assertNotNull(camelContext);
>>>
>>>         DefaultCamelContext.class.cast(camelContext).setName("camel-labs");
>>>         camelContext.start();
>>>         Assertions.assertTrue(camelContext.getStatus().isStarted());
>>>         System.out.println("camel context started");
>>>
>>>         RouteBuilder routeBuilder = new RouteBuilder() {
>>>             @Override
>>>             public void configure() throws Exception {
>>>
>>>                 from("jetty:http://localhost:9090/labs").
>>>                         id("jetty-example").
>>>
>>> setHeader(Exchange.HTTP_RESPONSE_CODE,constant(200)).
>>>                         setBody(constant("it works well!"));
>>>             }
>>>         };
>>>         camelContext.addRoutes(routeBuilder);
>>>         System.out.println("camel routes jetty added ->
>>> http://localhost:9090/labs");
>>>         Thread.sleep(10000L);
>>>     }
>>>
>>>     @Test
>>>     public void publishHttpCamelContextWithDefinitionTest() throws
>>> Exception {
>>>         CamelContext camelContext = new DefaultCamelContext();
>>>         Assertions.assertNotNull(camelContext);
>>>
>>>         DefaultCamelContext.class.cast(camelContext).setName("camel-labs");
>>>         camelContext.start();
>>>         Assertions.assertTrue(camelContext.getStatus().isStarted());
>>>         System.out.println("camel context started");
>>>
>>>         final RouteDefinition definition = new RouteDefinition();
>>>
>>> definition.from("jetty:http://localhost:9090/labs").id("jetty-example").
>>>                 setHeader(Exchange.HTTP_RESPONSE_CODE,constant(200)).
>>>                 setBody(constant("it works well!"));
>>>
>>>         RouteBuilder routeBuilder = new RouteBuilder() {
>>>             @Override
>>>             public void configure() throws Exception {
>>>                 System.out.println(definition);
>>>                 configureRoute(definition);
>>>             }
>>>         };
>>>         camelContext.addRoutes(routeBuilder);
>>>         System.out.println("camel routes with definition jetty added ->
>>> http://localhost:9090/labs");
>>>         Thread.sleep(10000L);
>>>     }
>>>
>>> regards,
>>>
>>> --
>>> François
>>> fpapon@apache.org
>>>

Re: [java-dsl] - RouteBuilder

Posted by Francois Papon <fr...@openobject.fr>.
Hi Claus,

It works very well now :)

Thanks for your help!

regards,

François
fpapon@apache.org

Le 20/05/2019 à 08:10, Claus Ibsen a écrit :
> Hi
>
> You should add the definition via camel context api and not try to add
> it via the route builder.
>
>
> On Mon, May 20, 2019 at 5:47 AM Francois Papon
> <fr...@openobject.fr> wrote:
>> Hi,
>>
>> I'm trying to use a RouteDefintion to configure a RouteBuilder and it
>> doesn't work.
>>
>> It works by setting the route in the first test but the endpoint is not
>> publish in the second test...
>>
>> Any ideas?
>>
>>
>> @Test
>>     public void publishHttpCamelContextTest() throws Exception {
>>         CamelContext camelContext = new DefaultCamelContext();
>>         Assertions.assertNotNull(camelContext);
>>
>>         DefaultCamelContext.class.cast(camelContext).setName("camel-labs");
>>         camelContext.start();
>>         Assertions.assertTrue(camelContext.getStatus().isStarted());
>>         System.out.println("camel context started");
>>
>>         RouteBuilder routeBuilder = new RouteBuilder() {
>>             @Override
>>             public void configure() throws Exception {
>>
>>                 from("jetty:http://localhost:9090/labs").
>>                         id("jetty-example").
>>
>> setHeader(Exchange.HTTP_RESPONSE_CODE,constant(200)).
>>                         setBody(constant("it works well!"));
>>             }
>>         };
>>         camelContext.addRoutes(routeBuilder);
>>         System.out.println("camel routes jetty added ->
>> http://localhost:9090/labs");
>>         Thread.sleep(10000L);
>>     }
>>
>>     @Test
>>     public void publishHttpCamelContextWithDefinitionTest() throws
>> Exception {
>>         CamelContext camelContext = new DefaultCamelContext();
>>         Assertions.assertNotNull(camelContext);
>>
>>         DefaultCamelContext.class.cast(camelContext).setName("camel-labs");
>>         camelContext.start();
>>         Assertions.assertTrue(camelContext.getStatus().isStarted());
>>         System.out.println("camel context started");
>>
>>         final RouteDefinition definition = new RouteDefinition();
>>
>> definition.from("jetty:http://localhost:9090/labs").id("jetty-example").
>>                 setHeader(Exchange.HTTP_RESPONSE_CODE,constant(200)).
>>                 setBody(constant("it works well!"));
>>
>>         RouteBuilder routeBuilder = new RouteBuilder() {
>>             @Override
>>             public void configure() throws Exception {
>>                 System.out.println(definition);
>>                 configureRoute(definition);
>>             }
>>         };
>>         camelContext.addRoutes(routeBuilder);
>>         System.out.println("camel routes with definition jetty added ->
>> http://localhost:9090/labs");
>>         Thread.sleep(10000L);
>>     }
>>
>> regards,
>>
>> --
>> François
>> fpapon@apache.org
>>
>

Re: [java-dsl] - RouteBuilder

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

You should add the definition via camel context api and not try to add
it via the route builder.


On Mon, May 20, 2019 at 5:47 AM Francois Papon
<fr...@openobject.fr> wrote:
>
> Hi,
>
> I'm trying to use a RouteDefintion to configure a RouteBuilder and it
> doesn't work.
>
> It works by setting the route in the first test but the endpoint is not
> publish in the second test...
>
> Any ideas?
>
>
> @Test
>     public void publishHttpCamelContextTest() throws Exception {
>         CamelContext camelContext = new DefaultCamelContext();
>         Assertions.assertNotNull(camelContext);
>
>         DefaultCamelContext.class.cast(camelContext).setName("camel-labs");
>         camelContext.start();
>         Assertions.assertTrue(camelContext.getStatus().isStarted());
>         System.out.println("camel context started");
>
>         RouteBuilder routeBuilder = new RouteBuilder() {
>             @Override
>             public void configure() throws Exception {
>
>                 from("jetty:http://localhost:9090/labs").
>                         id("jetty-example").
>
> setHeader(Exchange.HTTP_RESPONSE_CODE,constant(200)).
>                         setBody(constant("it works well!"));
>             }
>         };
>         camelContext.addRoutes(routeBuilder);
>         System.out.println("camel routes jetty added ->
> http://localhost:9090/labs");
>         Thread.sleep(10000L);
>     }
>
>     @Test
>     public void publishHttpCamelContextWithDefinitionTest() throws
> Exception {
>         CamelContext camelContext = new DefaultCamelContext();
>         Assertions.assertNotNull(camelContext);
>
>         DefaultCamelContext.class.cast(camelContext).setName("camel-labs");
>         camelContext.start();
>         Assertions.assertTrue(camelContext.getStatus().isStarted());
>         System.out.println("camel context started");
>
>         final RouteDefinition definition = new RouteDefinition();
>
> definition.from("jetty:http://localhost:9090/labs").id("jetty-example").
>                 setHeader(Exchange.HTTP_RESPONSE_CODE,constant(200)).
>                 setBody(constant("it works well!"));
>
>         RouteBuilder routeBuilder = new RouteBuilder() {
>             @Override
>             public void configure() throws Exception {
>                 System.out.println(definition);
>                 configureRoute(definition);
>             }
>         };
>         camelContext.addRoutes(routeBuilder);
>         System.out.println("camel routes with definition jetty added ->
> http://localhost:9090/labs");
>         Thread.sleep(10000L);
>     }
>
> regards,
>
> --
> François
> fpapon@apache.org
>


-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2