0

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.

sidd
  • 195
  • 3
  • 20

1 Answers1

0

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.

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • Thanks a TON Claus. that worked. Now I have 2 more requirements Please kindly help me with that as well. Requirement 1: I have to validate this generated XML with an XSD (I have made the XSD for this XML). Requirement 2: I have to send this XML to ActiveMQ queue and I have to do this in real time i.e I have to send the each XML one by one. Please kindly help me. Your inputs would be of immense help to me. – sidd Mar 23 '20 at 05:01
  • All this questions are already answered. [Here](https://stackoverflow.com/questions/15732/whats-the-best-way-to-validate-an-xml-file-against-an-xsd-file) about xsd validation. For sending messages via jms you can use [jms-component](https://camel.apache.org/components/latest/jms-component.html) – c0ld Mar 23 '20 at 07:49
  • @c0ld: I need to do this using Apache camel. – sidd Mar 23 '20 at 08:06
  • @Claus: I have tried varies ways for validator. for e.g;: 1) .to("validator:src/main/resources?fileName=SampleXMLStructure.xsd"); 2) .to("validator:file:SampleXMLStructure.xsd"); 3) .to("validator:SampleXMLStructure.xsd") 4) .to("validator:src/main/resources/SampleXMLStructure.xsd"); but none of them seems to be working. – sidd Mar 23 '20 at 08:07
  • @sidd How it is not working ? Some exception is throwing or what ? – c0ld Mar 23 '20 at 08:37
  • @c0ld : ` from("file:src/main/resources?fileName=data-sample.csv") .process(new MyTransform()) .to("file:src/main/resources/?fileName=emp.xml") .to("validator:SampleXMLStructure.xsd");` This code gives not syntax error but it does not give an exception as the xsd is not in compliance with the xml. What is wrong? – sidd Mar 23 '20 at 09:33
  • @sidd Or your xml is really valid or it's reason to create new question (don't forget specify version of camel). Because comments is not good place to detailed investigation. And btw i create from("file").to("file").to("validator") and it is working for camel 3.0.1 – c0ld Mar 23 '20 at 12:53
  • @c0ld: Fine .. I"ll create a new question. My present question is answered. – sidd Mar 23 '20 at 13:14