Francesco
Francesco

Reputation: 1792

Standalone Apache Camel application doesn' run

I'm on this problem: can't get my apache camel batch run. Here is the code:

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;

public class Launch {

private Main main;

public static void main(String[] args) {
    Launch l = new Launch();
    System.out.println(System.getProperty("from") +" -> "+System.getProperty("to"));
    try {
        l.execute();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void execute() throws Exception {
    main = new Main();
    main.enableHangupSupport();
    main.addRouteBuilder(new FromFileToFile());
    main.run();
}

private static class FromFileToFile extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        onException(Exception.class).handled(true).process(new Processor() {
            public void process(Exchange arg0) throws Exception {
                arg0.getException().printStackTrace();
            }
        });
        from(System.getProperty("from") + "")
                .filter(body().contains("DOTHIS"))
                .process(new Processor() {

                    public void process(Exchange arg0) throws Exception {
                        System.out.println(arg0.getIn().getBody()
                                .toString());
                    }
                }).to(System.getProperty("to"))
                .to(System.getProperty("to") + ".BAK");
        }
    }
}

I don't want to use the Thread.sleep(...) workaround. I simply copied and modified the source stuff posted on this official docs page. When I run my dummy program using Eclipse, application simply hangs. I can't figure out what's wrong.

Upvotes: 2

Views: 174

Answers (2)

Francesco
Francesco

Reputation: 1792

It was a problem about path. I passed arguments as options like this: file://Users/francesco/.. As I'm using windows I must specify uri like this file:///C:/Users/francesco/.. The batch doesn't hangs, it continues to poll directory for new files to consumes.

Upvotes: 1

jnupponen
jnupponen

Reputation: 745

Your application doesn't probably hang, it just won't do anything. :)

You have defined filter that checks if Camel Message body contains word "DOTHIS". When you consume file with File consumer the body will be of type GenericFile. Then when your filter checks for that string it surely won't find it since the body is not string.

Solution: Convert file body to string first and then your filter will work and you get the result you were expecting. Conversion can be done like this

from(System.getProperty("from") + "")        
    .convertBodyTo(String.class, "UTF-8")
    .filter(body().contains("DOTHIS"))

You might also want to increase logging level so you can get the grasp of what's going on in your route.

Upvotes: 1

Related Questions