骆驼Hello World计划

时间:2014-04-09 08:20:55

标签: java apache-camel

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

}

这是我的第一个骆驼计划。看起来每件事都是正确的。但文件传输没有发生。

我添加了

  • camel conext 2.12.1 jar
  • camel core 2.12.1 jar
  • camel ftp 2.12.1 jar
  • slf4j api 1.7.6 jar

4 个答案:

答案 0 :(得分:2)

增加sleep时间以正确获得结果。

1000 ms不足以将文件从输入目录复制到输出目录。

sleep时间指定将文件从输入复制到输出的时间限制。如果增加sleep时间上下文会将更多文件从输入复制到输出目录

答案 1 :(得分:2)

通常当Camel用作standalone应用程序时,您应使用Main提供的Camel。我已经从他们的网站发布了代码:

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

答案 2 :(得分:0)

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

更改此段代码,为camel提供移动文件的时间。

答案 3 :(得分: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();

   }
}

此类运行RouteBuilder

public class Launcher {

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

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

   }
}