2017-10-13 96 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

相關問題