从Camel会话中检索响应

时间:2015-07-14 06:40:24

标签: java csv apache-camel

我正在使用Camel处理CSV到对象创建示例。

我已经创建了一个bean,其中我有一个方法将执行所有操作,该方法将返回List。

我的问题是我如何能够在骆驼之外获得List。

源代码:

public class Person {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.firstName + "===" + this.lastName;
    }
}



public class UnMarshallingTest {

    public static void main(String[] args) throws Exception {
        DefaultCamelContext context = new DefaultCamelContext();
        // append the routes to the context
        context.addRoutes(new UnMarshallingTestRoute());

        CSVToPerson csvToPerson = new CSVToPerson();
        SimpleRegistry reg = new SimpleRegistry();
        reg.put("csvToPerson", csvToPerson);
        context.setRegistry(reg);
        context.start();
        Thread.sleep(3000);
        System.out.println("Done");
        context.stop();

    }

}




public class UnMarshallingTestRoute extends RouteBuilder {


    public void configure() throws Exception {
        from("file:/home/viral/Projects/camel/cxfJavaTest/src/data?noop=true")           
            .unmarshal().csv()
            .beanRef("csvToPerson", "process");
    }
}



public class CSVToPerson {

    public List<Person> process(List<List> csvRows) {
        List<Person> oList = new ArrayList<Person>();
        System.out.println("called");
        for (List csvRow : csvRows) {
            Person person = new Person();
            person.setFirstName((String) csvRow.get(0));
            person.setLastName((String) csvRow.get(1));
            oList.add(person);
            // doSomethingTo(person);
            System.out.println("++++++++++++++++++++++++++");
            System.out.println(person);
            System.out.println("++++++++++++++++++++++++++");
        }
        System.out.println("End");
        return oList;
    }
}

我只有Camel Context Object,我怎么能从上下文对象中获取List。

1 个答案:

答案 0 :(得分:0)

我找到了上述问题的答案。

public class UnMarshallingTest {

    public static void main(String[] args) throws Exception {
        DefaultCamelContext context = new DefaultCamelContext();
        // append the routes to the context
        context.addRoutes(new UnMarshallingTestRoute());

        //CSVToPerson csvToPerson = new CSVToPerson();
        //SimpleRegistry reg = new SimpleRegistry();
        //reg.put("csvToPerson", csvToPerson);
        //context.setRegistry(reg);

        context.start();

        ConsumerTemplate ct = context.createConsumerTemplate();

        List data = (List<Person>)ct.receiveBody("seda:foo", 5000);

        System.out.println("=================================");
        System.out.println("Return Value::::" + data);
        System.out.println("=================================");

        Thread.sleep(3000);

        context.stop();

    }
}

public class UnMarshallingTestRoute extends RouteBuilder {


    public void configure() throws Exception {
        from("file:/home/viral/Projects/camel/cxfJavaTest/src/data?noop=true")
          .split(body().tokenize("\n")).streaming()
            .unmarshal().csv()
            .bean(CSVToPerson.class).to ("seda:foo") ;
    }
}

对于消费响应,我使用了seda。然后在执行路线后我消耗了相同的东西,然后我得到List<Person>