You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Oliver Doepner <od...@gmail.com> on 2017/08/02 16:47:05 UTC

Full example of configuring endpoints in pure Java (without uri strings)

Hello,

I am trying to use Camel using the pure Java approach (Java DSL, no DI
framework, no URI Strings).

My approach so far is like the simplistic sample below, but I am not sure
if it uses proper practices.

Can you please let me know if/what problems you see with this approach?
Is there maybe documentation somewhere with a full "pure Java" example?

Thank you
Oliver


import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.cxf.CxfComponent;
import org.apache.camel.component.cxf.CxfEndpoint;
import org.apache.camel.main.Main;
import org.apache.camel.main.MainListenerSupport;

public class CamelExample {

    public static void main(String... args) throws Exception {

        final Main main = new Main();

        main.addMainListener(new MainListenerSupport() {
            @Override
            public void configure(CamelContext context) {
                context.addComponent("cxf", new CxfComponent());

                final CxfEndpoint webService = new CxfEndpoint();
                webService.setCamelContext(context);
                webService.setAddress("http://service/entity");
                webService.setServiceClass(MyService.class);
                webService.setDefaultOperationName("test");
                // etc

                Processor requestPreparation = new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception
{
                        exchange.getIn().setBody(new MyServiceRequest());
                    }
                };

                main.addRouteBuilder(
                        new RouteBuilder() {
                            @Override
                            public void configure() throws Exception {
                                from("direct:start")
                                        .process(requestPreparation)
                                        .to(webService);
                            }
                        }
                );
            }
        });

        main.run();
    }

    /* web service related classes stubbed out to keep example code simple
*/

    private static final class MyService {};
    private static final class MyServiceRequest {};

}



--
Oliver Doepner
Software Developer
IMP Solutions, Halifax, NS
Phone 1-902-482-1615



-- 
Oliver Doepner
Halifax, Nova Scotia
http://oliver.doepner.net/
Buchstabensalat : ßäöüÄÖÜ

Re: Full example of configuring endpoints in pure Java (without uri strings)

Posted by Oliver Doepner <od...@gmail.com>.
Hello Claus,

Thanks a lot for your helpful response.

Currently I just set the CamelContext on the endpoints right after I create
them and that seems to take care of the wiring between endpoint and
component.

Regarding the code example I included in my previous email to this list, I
had to make a small change to make it work:

Instead of main.addRouteBuilder(...), I now use context.addRoutes(...).
I also had to wrap those calls in a try-catch, which is fine, I guess.

So something like:

try {

    context.addRoutes(new MyRouteBuilder(...));
    context.addRoutes(new MyOtherRouteBuilder(...));

// Some Camel 2.x APIs still use "throws Exception". Apparently this might
change in Camel 3.x and beyond:
//
http://camel.apache.org/camel-30-ideas.html#Camel3.0-Ideas-AvoidthrowsExceptiononenduserAPI
} catch (Exception e) {
    throw new IllegalStateException(e);
}



On Fri, Aug 4, 2017 at 8:42 AM, Claus Ibsen <cl...@gmail.com> wrote:

> Hi
>
> Yeah you can run Camel standalone with that main class or do it by
> yourself.
>
> One comment is that when you create the CxfEndpoint then you need to
> set the CxfComponent on this instance as well.
>
> Ideally you create the endpoints via the uri syntax, or via the component.
>
> For some camel components then they rely on the component for the
> endpoints,producers,consumers etc such as JMS, Jetty, and possible
> CXF.
>
> So a good rule of thumb is to create endpoints via their components.
> Afterwards you can tweak then via their getter/setters.
>
> In the future we will have some tooling to auto generate a builder
> style Java DSL for the endpoints so you can configure them in a type
> safe manner via java code. That is on the roadmap for 3.x.
>
> On Wed, Aug 2, 2017 at 6:47 PM, Oliver Doepner <od...@gmail.com> wrote:
> > Hello,
> >
> > I am trying to use Camel using the pure Java approach (Java DSL, no DI
> > framework, no URI Strings).
> >
> > My approach so far is like the simplistic sample below, but I am not sure
> > if it uses proper practices.
> >
> > Can you please let me know if/what problems you see with this approach?
> > Is there maybe documentation somewhere with a full "pure Java" example?
> >
> > Thank you
> > Oliver
> >
> >
> > import org.apache.camel.CamelContext;
> > import org.apache.camel.Exchange;
> > import org.apache.camel.Processor;
> > import org.apache.camel.builder.RouteBuilder;
> > import org.apache.camel.component.cxf.CxfComponent;
> > import org.apache.camel.component.cxf.CxfEndpoint;
> > import org.apache.camel.main.Main;
> > import org.apache.camel.main.MainListenerSupport;
> >
> > public class CamelExample {
> >
> >     public static void main(String... args) throws Exception {
> >
> >         final Main main = new Main();
> >
> >         main.addMainListener(new MainListenerSupport() {
> >             @Override
> >             public void configure(CamelContext context) {
> >                 context.addComponent("cxf", new CxfComponent());
> >
> >                 final CxfEndpoint webService = new CxfEndpoint();
> >                 webService.setCamelContext(context);
> >                 webService.setAddress("http://service/entity");
> >                 webService.setServiceClass(MyService.class);
> >                 webService.setDefaultOperationName("test");
> >                 // etc
> >
> >                 Processor requestPreparation = new Processor() {
> >                     @Override
> >                     public void process(Exchange exchange) throws
> Exception
> > {
> >                         exchange.getIn().setBody(new MyServiceRequest());
> >                     }
> >                 };
> >
> >                 main.addRouteBuilder(
> >                         new RouteBuilder() {
> >                             @Override
> >                             public void configure() throws Exception {
> >                                 from("direct:start")
> >                                         .process(requestPreparation)
> >                                         .to(webService);
> >                             }
> >                         }
> >                 );
> >             }
> >         });
> >
> >         main.run();
> >     }
> >
> >     /* web service related classes stubbed out to keep example code
> simple
> > */
> >
> >     private static final class MyService {};
> >     private static final class MyServiceRequest {};
> >
> > }
> >
> >
> >
> > --
> > Oliver Doepner
> > Software Developer
> > IMP Solutions, Halifax, NS
> > Phone 1-902-482-1615
> >
> >
> >
> > --
> > Oliver Doepner
> > Halifax, Nova Scotia
> > http://oliver.doepner.net/
> > Buchstabensalat : ßäöüÄÖÜ
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2
>



-- 
Oliver Doepner
Halifax, Nova Scotia
http://oliver.doepner.net/
Buchstabensalat : ßäöüÄÖÜ

Re: Full example of configuring endpoints in pure Java (without uri strings)

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

Yeah you can run Camel standalone with that main class or do it by yourself.

One comment is that when you create the CxfEndpoint then you need to
set the CxfComponent on this instance as well.

Ideally you create the endpoints via the uri syntax, or via the component.

For some camel components then they rely on the component for the
endpoints,producers,consumers etc such as JMS, Jetty, and possible
CXF.

So a good rule of thumb is to create endpoints via their components.
Afterwards you can tweak then via their getter/setters.

In the future we will have some tooling to auto generate a builder
style Java DSL for the endpoints so you can configure them in a type
safe manner via java code. That is on the roadmap for 3.x.

On Wed, Aug 2, 2017 at 6:47 PM, Oliver Doepner <od...@gmail.com> wrote:
> Hello,
>
> I am trying to use Camel using the pure Java approach (Java DSL, no DI
> framework, no URI Strings).
>
> My approach so far is like the simplistic sample below, but I am not sure
> if it uses proper practices.
>
> Can you please let me know if/what problems you see with this approach?
> Is there maybe documentation somewhere with a full "pure Java" example?
>
> Thank you
> Oliver
>
>
> import org.apache.camel.CamelContext;
> import org.apache.camel.Exchange;
> import org.apache.camel.Processor;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.component.cxf.CxfComponent;
> import org.apache.camel.component.cxf.CxfEndpoint;
> import org.apache.camel.main.Main;
> import org.apache.camel.main.MainListenerSupport;
>
> public class CamelExample {
>
>     public static void main(String... args) throws Exception {
>
>         final Main main = new Main();
>
>         main.addMainListener(new MainListenerSupport() {
>             @Override
>             public void configure(CamelContext context) {
>                 context.addComponent("cxf", new CxfComponent());
>
>                 final CxfEndpoint webService = new CxfEndpoint();
>                 webService.setCamelContext(context);
>                 webService.setAddress("http://service/entity");
>                 webService.setServiceClass(MyService.class);
>                 webService.setDefaultOperationName("test");
>                 // etc
>
>                 Processor requestPreparation = new Processor() {
>                     @Override
>                     public void process(Exchange exchange) throws Exception
> {
>                         exchange.getIn().setBody(new MyServiceRequest());
>                     }
>                 };
>
>                 main.addRouteBuilder(
>                         new RouteBuilder() {
>                             @Override
>                             public void configure() throws Exception {
>                                 from("direct:start")
>                                         .process(requestPreparation)
>                                         .to(webService);
>                             }
>                         }
>                 );
>             }
>         });
>
>         main.run();
>     }
>
>     /* web service related classes stubbed out to keep example code simple
> */
>
>     private static final class MyService {};
>     private static final class MyServiceRequest {};
>
> }
>
>
>
> --
> Oliver Doepner
> Software Developer
> IMP Solutions, Halifax, NS
> Phone 1-902-482-1615
>
>
>
> --
> Oliver Doepner
> Halifax, Nova Scotia
> http://oliver.doepner.net/
> Buchstabensalat : ßäöüÄÖÜ



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