2014-04-09 88 views
2
package com.camel; 

import org.apache.camel.CamelContext; 
import org.apache.camel.Exchange; 
import org.apache.camel.Processor; 
import org.apache.camel.builder.RouteBuilder; 
import org.apache.camel.impl.DefaultCamelContext; 

public class FirstRoute { 
public static void main(String args []) throws Exception{ 
    CamelContext context = new DefaultCamelContext(); 
    context.addRoutes(new RouteBuilder() { 

     @Override 
     public void configure() throws Exception { 
      from("file:C:\\workspace\\input?noop=true").process(new  strong textProcessor() { 

       @Override 
       public void process(Exchange arg0) throws Exception { 
       System.out.println("hello camel!"); 
       } 
      }).to("file:C:\\workspace\\output").end();   
     } 
    }); 
    context.start(); 
    Thread.sleep(1000); 
    context.stop(); 
} 

} 

這是我的第一個駱駝計劃。看起來每件事都是正確的。但文件傳輸沒有發生。駱駝Helloworld計劃

我加

  • 駱駝conext 2.12.1罐子
  • 駱駝核心2.12.1罐子
  • 駱駝FTP 2.12.1罐子
  • SLF4J API 1.7.6罐子
+0

「喂駱駝」 是沒有得到priinted在控制檯中,也文件傳輸沒有發生。太惱人了,我無法做一個小程序。請讓我知道爲什麼。多謝你們! – bks4line

+0

我加了Log4j 1.2.15和slf4j-log4j 1.7.6。有用。但有時它永遠不會工作。駱駝演技更加奇特。有時文件被傳輸,但系統沒有發生。很混亂! – bks4line

回答

2

增加sleep時間以正確獲得結果。

1000 ms不足以將文件從輸入目錄複製到輸出目錄。

sleep時間指定將文件從輸入複製到輸出的時間限制。如果增加sleep時間上下文將與輸入複製多個文件輸出目錄

+0

是的,這確實解決了問題,謝謝! – bks4line

2

通常當Camel用作standalone的應用程序,你應該使用Camel提供Main。我已經發布的代碼從他們的網站:

public class MainExample { 

    private Main main; 

    public static void main(String[] args) throws Exception { 
     MainExample example = new MainExample(); 
     example.boot(); 
    } 

    public void boot() throws Exception { 
     // create a Main instance 
     main = new Main(); 
     // enable hangup support so you can press ctrl + c to terminate the JVM 
     main.enableHangupSupport(); 
     // bind MyBean into the registery 
     main.bind("foo", new MyBean()); 
     // add routes 
     main.addRouteBuilder(new MyRouteBuilder()); 

     // run until you terminate the JVM 
     System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n"); 
     main.run(); 
    } 

    private static class MyRouteBuilder extends RouteBuilder { 
     @Override 
     public void configure() throws Exception { 
      from("timer:foo?delay=2000") 
       .process(new Processor() { 
        public void process(Exchange exchange) throws Exception { 
         System.out.println("Invoked timer at " + new Date()); 
        } 
       }) 
       .beanRef("foo"); 
     } 
    } 

    public static class MyBean { 
     public void callMe() { 
      System.out.println("MyBean.calleMe method has been called"); 
     } 
    } 
} 

參考http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html瞭解更多詳情。

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

改變這段代碼給駱駝移動文件的時間。

0

你的代碼返回一些異常?

問題可能是超時1000是等於1秒,是複製文件的時間非常短暫,您可以嘗試將超時值或刪除值。

的榜樣沒有超時:

這個類來創建一個RouteBuilder

public class CamelRoute extends RouteBuilder { 

    @Override 
    public void configure() throws Exception { 

     from("file:/opt/files-camel?noop=true") 
     .routeId("file-in") 
     .choice() 
     .when(header(Exchange.FILE_NAME).endsWith(".xml")) 
      .to("file:/opt/files-camel/xml?noop=true") 
     .when(header(Exchange.FILE_NAME).endsWith(".txt")) 
      .to("file:/opt/files-camel/txt?noop=true") 
     .end() 
     .end(); 

    } 
} 

本A類運行RouteBuilder

public class Launcher { 

    public static void main(String... args) throws Exception { 

     Main main = new Main(); 
     main.addRouteBuilder(new CamelRoute()); 
     main.run(args); 

    } 
}