0
votes

I have a csv file with around 20 records. My aim it to read the csv file record-by-record convert it to XML and send the XML as a message to ActiveMQ queue. I am doing this using Apache Camel.

First Step: read the csv file and convert it to XML using Apache Camel.

Below is the code:

    CamelContext _ctx = new DefaultCamelContext(); 
            _ctx.addRoutes(new RouteBuilder() {

                public void configure() throws Exception {
                    System.out.println("Inside configure method.");
                    from("file:src/main/resources/data-sample.csv")
                    .process(new MyTransform())
                    .to("file:src/main/resources/fileName=emp.xml");
                }

            });

            _ctx.start();
             Thread.sleep(4000);
            _ctx.stop();

class MyTransform implements Processor {

    public void process(Exchange exchange) throws Exception {

        System.out.println("In Process method");

        String myString = exchange.getIn().getBody(String.class);
        String[] lineSeparator = myString.split(System.getProperty("line.separator"));
        StringBuffer sb = new StringBuffer();

        for (String lineData : lineSeparator){

            String[] commaSeparator = lineData.split(",");
            sb.append("<equityFeeds>");
            sb.append("<externalTransactionId>" + commaSeparator[0].toString() + "</externalTransactionId>");
            sb.append("<clientId>" + commaSeparator[1].toString() + "</clientId>");
            sb.append("<securityId>" + commaSeparator[2].toString() + "</securityId>");
            sb.append("<transactionType>" + commaSeparator[3].toString() + "</transactionType>");
            sb.append("<transactionDate>" + commaSeparator[4].toString() + "</transactionDate>");
            sb.append("<sourceSystem>" + commaSeparator[5].toString() + "</sourceSystem>");
            sb.append("<priorityFlag>" + commaSeparator[6].toString() + "</priorityFlag>");
            sb.append("</equityFeeds>");

        }

        System.out.println("MyProcessor complete");
        exchange.getIn().setBody(sb.toString());

    }   

}   

In the output it prints only

Inside configure method.

The process method is not being called. Why? What is wrong? Please guide me. It's so annoying. Please Help.

1

1 Answers

0
votes

Just say from("file:src/main/resources/") as its the starting directory. If you want to pickup only a specific file, then say from("file:src/main/resources?fileName=data-sample.csv")

Also you application only runs for 4 seconds as you only sleep for that.