Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I invoke a RESTful service through Apache Camel?

I am currently using a HTTP method for invoking some URL which will create a JIRA issue.

Now I want to use Apache Camel, how can I use that?

I need to invoke the following link through Camel:

http://localhost:8080/rest/api/2/project/" + key + /components

As I'm new to Camel, please suggest some solutions and examples too.

Thanks

like image 463
jasmeet24 Avatar asked Apr 16 '12 06:04

jasmeet24


People also ask

How do I invoke a REST service?

Select the operations that you want to include in the external service, and click Next. If the OpenAPI specification was already discovered, you can create a new service or replace the existing one. Click Next. Set the server that contains the properties used to invoke the REST service.

What is CamelContext in Apache Camel?

The CamelContext is the runtime system, which holds everything together as depicted in the figure below. The CamelContext provides access to many useful services, the most notable being components, type converters, a registry, endpoints, routes, data formats, and languages. Contains the components used.


3 Answers

I am using apache camel jetty

CamelContext context = new DefaultCamelContext();
    public void configure(){
           context.addRoutes(new RouteBuilder(){
           from("jetty:localhost:9000/offers")
           .to("direct:getOffers")
           .end();

    }

});

so here when the user will hit http://localhost:9000/offers then the endpoint direct:getOffers will get invoked

so now defining the getOffers end point

context.addRoutes(new RouteBuilder(){
     public void configure(){
          from("direct:getOffers")
          .to("jetty:http://localhost:9008/api/v2.0/offers?  
          bridgeEndpoint=true")
         .end();

     }

});

Here another service is running at 9008 having a rest resource of http://localhost:9008/api/v2.0/offers and this is the resource that i am trying to consume.

so when camel instance starts it registers both the routes then it does the processing as described above

Note Its important to add the option of ?bridgeEndpoint=true for this to work

like image 69
selftaught91 Avatar answered Oct 16 '22 16:10

selftaught91


You could easily use the CXFRS Component; if you need to do it using the HTTP Component for some reason you could easily use that as well:

<setHeader headerName="CamelHttpUri">
      <simple>http://localhost:8080/rest/api/2/project/${header.myKey}/components</simple>
</setHeader>
<inOut uri="http://doesnt.matter.we/override/it/anyways" />

And of course you will need to enrich your message with the myKey header before getting to this part of the route.

like image 43
MahdeTo Avatar answered Oct 16 '22 15:10

MahdeTo


See also this FAQ about using dynamic to endpoints in Camel http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html

Essentially the EIP pattern for this is the recipient list.

So in your case it could also be simplified to as one EIP

<recipientList>
  <simple>http://localhost:8080/rest/api/2/project/${header.myKey}/components</simple>
</recipientList>

Mind the http component in Camel is fully synchronous. If you want to do request/reply over HTTP and avoid having the caller block while waiting for the reply message, then you can use some of the other HTTP components from Camel such as:

  • camel-ahc
  • camel-http4
  • camel-jetty
like image 21
Claus Ibsen Avatar answered Oct 16 '22 16:10

Claus Ibsen