3 Replies Latest reply on Mar 31, 2015 2:20 AM by grgrzybek

    How to invoke a rest service from Camel routes

    szarekar

      Hi,

       

      I want to invoke http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=RHT from my camel

      project

       

       

        <camelContext trace="false" xmlns="http://camel.apache.org/schema/spring">

          <route id="my_Sample_Camel_Route_with_CXF">

           

               <from uri="cxfrs:http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=RHT"/>   

                <to uri="file:target/messages/quote"/>

          </route>

      </camelContext>

       

       

      Gives me error - ...

      Caused by: javax.ws.rs.NotFoundException

              at org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean.checkResources(AbstractJAXRSFactoryBean.java:322)

              at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:152)

              ... 33 more



      This is my pom,xml file


        <dependencies>

          <dependency>

            <groupId>org.apache.camel</groupId>

            <artifactId>camel-core</artifactId>

            <version>2.12.0.redhat-610379</version>

          </dependency>

          <dependency>

            <groupId>org.apache.camel</groupId>

            <artifactId>camel-spring</artifactId>

            <version>2.12.0.redhat-610379</version>

          </dependency>

         

           <dependency>

          <groupId>org.apache.camel</groupId>

          <artifactId>camel-cxf</artifactId>

           <version>2.12.0.redhat-610379</version>

          </dependency>

         

         <dependency>

           <groupId>javax.ws.rs</groupId>

           <artifactId>javax.ws.rs-api</artifactId>

           <version>2.0</version>

        </dependency>

       

        <dependency>

           <groupId>javax.ws.rs</groupId>

           <artifactId>javax.ws.rs-api</artifactId>

           <version>2.0-m01</version>

        </dependency>

        • 1. Re: How to invoke a rest service from Camel routes
          grgrzybek

          Hello!

          <from uri="cxfrs:http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=RHT""/> instructs Camel to set up a CXF endpoint (server component) listening to incoming HTTP/Rest requests, so you won't get "fetch stock info about RHT and copy it to directory of choice" behaviour.

          So in order to get stock info and copy it (as is) to directory, do the following:

          <camelContext trace="false" xmlns="http://camel.apache.org/schema/spring">
            <route id="my_Sample_Camel_Route_with_CXF">
              <from uri="direct:getstockinfo" />
              <setHeader headerName="CamelHttpMethod">
                <constant>GET<constant>
              </setHeader>
              <to uri="http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=RHT">
              <to uri="file:target/messages/quote"/>
            </route>
          </camelContext>
          

          and from Java code, send any message to direct:getstockinfo

          Why manual triggerring? If you'd like automatic fetching of stock data, you'd end up with a loop that fetches XML and writes do directory. Please read about polling consumers here. This may help you set up configuration which polls for changes in the stockquote service.

          best regards
          Grzegorz Grzybek

          1 of 1 people found this helpful
          • 2. Re: How to invoke a rest service from Camel routes
            szarekar

            Thanks Grzegorz

            This is what i am trying to achieve (call :http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=RHT from camel endpoint )

            But it throws error

             

            Call http://localhost:9090/route/customerservice/customers/GOOG from REST UI

             

            This invokes my

            public class CustomerServiceRoute extends RouteBuilder {

                @Override

                public void configure() throws Exception {

                    getContext().setTracing(true);

                    from("cxfrs://http://localhost:9090/route?resourceClasses=com.fusesource.samples.CustomerServiceResource")     

                            .to("cxfrs://http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=GOOG")

                            .to("file:target/goog");

                }

            }

             

             

            Class CustomerServiceResource

             

            @Path("/customerservice/")

            public class CustomerServiceResource {

                @Context

                private UriInfo uriInfo;

             

                public CustomerServiceResource() {

                }

                @GET

                @Path("/customers/{id}/")

                @Produces("text/xml")

                public Customer getCustomer(@PathParam("id") String id) {

                    return null;

                }

             

            }

             

            CAMEL.XML

             

            <cxf:rsServer id="rsServer" address="http://localhost:9090/route"

                              serviceClass="com.fusesource.samples.CustomerServiceResource"/>

                <bean id="customerServProcessor" class="com.fusesource.samples.CustomerServiceProcessor"/>

             

                <bean id="routeBuilder" class="com.fusesource.samples.CustomerServiceRoute"/>

                <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

                    <routeBuilder ref="routeBuilder"/>

                </camelContext>

             

             

                 #############################Getting error on REST UI CLIENT########################

             

            System.InvalidOperationException: GetQuote/customerservice/customers/GOOG Web Service method name is not valid.

               at System.Web.Services.Protocols.HttpServerProtocol.Initialize()

               at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

             

            ################################ ERROR ON JBOSS DEVELOPMENT STUDIO CONSOLE##############

             

            qtp453346759-15 ERROR [org.apache.camel.processor.DefaultErrorHandler] - Failed delivery for (MessageId: ID-BMC4000LJVCNP12-57546-1426087062332-0-1 on ExchangeId: ID-BMC4000LJVCNP12-57546-1426087062332-0-2). Exhausted after delivery attempt: 1 caught: javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error

             

             

            Message History

            ---------------------------------------------------------------------------------------------------------------------------------------

            RouteId              ProcessorId          Processor                                                                        Elapsed (ms)

            [route1            ] [route1            ] [cxfrs://http://localhost:9090/route?resourceClasses=com.fusesource.samples.Cus] [       591]

            [route1            ] [setHeader1        ] [setHeader[CamelFileName]                                                      ] [        19]

            [route1            ] [to1               ] [cxfrs://http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=GOOG       ] [       569]

             

             

            Stacktrace

            ---------------------------------------------------------------------------------------------------------------------------------------

            javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error

              at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

              at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructo

            • 3. Re: How to invoke a rest service from Camel routes
              grgrzybek

              Looks like you didn't set the method to GET:

                  <setHeader headerName="CamelHttpMethod">
                    <constant>GET<constant>
                  </setHeader>
              

              regards

              Grzegorz