Consuming a RESTful web service using Apache Camel

2019-08-10 23:22发布

问题:

I am trying to consume a restful Web service using camel.

For that I am configuring dynamic endpoint url as the RESTful url is created at the runtime. Everytime I am checking if the particular endpoint url is registered as a route in my camel context using following method of CamelContext class.

Endpoint hasEndpoint(String uri);

In this case, if the endpoint is not registered then I add a route to my camel context using a custom Route Builder.

I am using camel HTTP component for this. This is working fine for me as of now.

However, I believe performance wise this is not good as everytime I have to check if a route is registered with the camel context and if not then register the same before making the webservice call.

Can some body please tell me if there is a better way to consume RESTful Web services in camel?

I also want to know if the RESTful webservice I am consuming uses OAuth 2.0 protocol, do I need to change anything in my code as I am just consuming it?

Regards, Nilotpal


Thanks for your reply.

I am checking if the route is already exists to make sure I don't end up adding duplicate route(s) to the camel context.

Regarding long lived routes and route dynamics, can u please explain a bit regarding this? How do I implement route dynamics?

It would also be helpful if you could point me to some CXF-RS producer example.. I read the documentation of CXFRS but could not understand it clearly.

Thanks Nilotpal

回答1:

Exactly why do you need to check if the route is registred or not before making the call? You should perhaps setup a more long lived route and route dynamic towards resfull resources.

As for Rest with camel, I think the HTTP component does a great job, but there are higher level components to use as well, more designed for REST. CXFRS and Restlet, producer examples for restlet can be found in the Apache Camel source unit tests, such as this RestletProducerGetTest.java.

As for oAuth 2.0, Camel has some oAuth support built-in, especially for google. Look for the gauth component. There is even a tutorial, however it might not be aligned with your case, it still might give some background so you could solve your issues: http://camel.apache.org/tutorial-oauth.html



回答2:

CamelContext context = new DefaultCamelContext();

My Aim

I am trying to intercept the incoming request and based on the ip of the incoming request i want to invoke dynamic endpoint of get offers

context.addRoutes(new RouteBuilder(){
     public void configure(){

     from("jetty:localhost:9000/offers")
     .process(new Processor(){
         public void process(Exchange exchange) throws Exception {

            //getting the request object
            HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class);
            //Extracting information from the request
            String requestIP=req.getRemoteAddr();

            /**
             * After getting the ip address i do necessay processing
             * and then add a property to exchange object.
             * Destination ip address is the address to which i want to   
             * send my request
             */

           exchange.setProperty("operatorAddress",destinationIpAddress);


       }
      })
     .to("direct:getOffers")
     .end();
}
});

Now i will invoke the getOffers endpoint

so first i will register it

context.addRoutes(new RouteBuilder(){
     public void configure(){
         from("direct:getOffers")
         .toD("jetty:${property.operatorAddress}/api/v2.0/offers? 
          bridgeEndpoint=true")
        .end();

    }
});

so we can access the operatorAddress property of exchange object as

${property.operatorAddress}

also when we have dynamic routes then we need to call

.toD()   and not .to()