2014-04-09 76 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); 

    } 
}