2017-10-13 68 views
0
获取数据

我是Apache Camel的新成员。我试着去这个文件复制到文件夹:https://www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xmlCamel从http

我得到的错误:

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(); 


     } 

} 

所以我知道我必须为它定义一个路线,但我应该在哪里把它定义(在什么文件??),以及如何定义?

更新代码2017年10月13日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(); 


     } 

} 

回答

1

对于配置在骆驼的路由器,你可以看看由骆驼here用在GitHub上的代码一起提供很好的例子。此外,您的路由不是有效的,首先,uri端点sysntax是错误的,其次http端点只能用作消费者而不是生产者。

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

为你的情况看看here。基本上你需要做这样的

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"); 

,然后调用直接端点

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

,或者您可以使用定时器作为解释here和您的路线是这样的

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

我没有任何名为“模板”的变量。你的意思是context.sendBody(“direct:start”,null); ? – Solo

+0

您可以使用ProducerTemplate template = context.createProducerTemplate()创建一个简单的ProducerTemplate; – Shailendra

+0

我用模板更新了代码(请参阅我的第一篇文章),然后我得到这个错误: 线程“main”org.apache.camel.CamelExecutionException中的异常:在Exchange上执行期间发生异常:Exchange [消息:[Body is null ]] – Solo

相关问题