Solo
Solo

Reputation: 579

Camel fetch data from http

Im new to Apache Camel. Im trying to copy this file into a folder: https://www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml

I get the error:

Exception in thread "main" java.lang.UnsupportedOperationException: Cannot consume from http endpoint

package route;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.log4j.BasicConfigurator;

public class CurrencyRoute {

        public static void main(String args[])  throws Exception {
            // Log 4j
            BasicConfigurator.configure();

            // Create camel context
            CamelContext context = new DefaultCamelContext();


            // New route
            context.addRoutes(new RouteBuilder() {
                public void configure() {
                    from("https://www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml")
                            .log("Read file")
                            .to("file:src/main/resources/data/inbox");
                }
            });

            // start the route and let it do its work
            context.start();
            Thread.sleep(10000);

            // stop the CamelContext
            context.stop();


        }

}

So I know I have to define a route for it, but where should I define it (in what file??), and how should it be defined?

Updated code 13.10.2017 13:06

package route;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.log4j.BasicConfigurator;

public class CurrencyRoute {

        public static void main(String args[])  throws Exception {
            // Log 4j
            BasicConfigurator.configure();

            // Create camel context
            CamelContext context = new DefaultCamelContext();

            // Template
            ProducerTemplate template = context.createProducerTemplate();

            // New route
            context.addRoutes(new RouteBuilder() {
                public void configure() {
                    from("direct:start").setHeader(Exchange.HTTP_METHOD, constant("GET"))
                            .to("https://www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml")
                            .to("file:src/main/resources/data/inbox/?autoCreate=true");
                }
            });

            // SendBody
            template.sendBody("direct:start", null);

            // start the route and let it do its work

            context.start();
            Thread.sleep(10000);

            // stop the CamelContext
            context.stop();


        }

}

Upvotes: 0

Views: 1328

Answers (1)

Shailendra
Shailendra

Reputation: 9102

For configuring routers in camel you can take a look at excellent examples provided by Camel here along with code on github. Also your route is not a valid, firstly the uri endpoint sysntax is wrong, secondly a http or http4 endpoint can only be used as a producer not consumer.

You can only produce to endpoints generated by the HTTP component. Therefore it should never be used as input into your camel Routes.

For your case take a look here. Basically you need to do something like this

from("direct:start").setHeader(Exchange.HTTP_METHOD, constant("GET")) 
.to("https://www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml")
.to("file:src/main/resources/data/inbox/?autoCreate=true");

and then call the direct endpoint

template.sendBody("direct:start", null);

or you may make use of a timer as explained here and your route as something like this

from(timer).to(http).to(file);

Upvotes: 1

Related Questions