سؤال

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

}

This is my first camel program. looks like every thing is correct. but the file transfer is not happening.

I added

  • camel conext 2.12.1 jar
  • camel core 2.12.1 jar
  • camel ftp 2.12.1 jar
  • slf4j api 1.7.6 jar
هل كانت مفيدة؟

المحلول

increase the sleep time to get the result correctly.

That 1000 ms is not enough to copy the files from input directory to output directory.

That sleep time specifies a time limit to copy files from input to output. if you increase sleep time context will copy more files from input to output directory

نصائح أخرى

Usually when Camel is used as a standalone application, you should use Main provided by Camel. I have posted the code from their site :

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

Refer http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html for more details.

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

Change this piece of code to give time for camel to move the file.

Your code return some exception?

The problem can be the timeout 1000 is equals 1 second, is a very short time for copy a file, you can try, up the value of timeout or remove.

Follow an example without timeout:

This Class create a 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();

   }
}

This Class run a RouteBuilder

public class Launcher {

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

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

   }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top