从 Camel Session 中检索响应

Retrieve Response from Camel Session

我正在使用 Camel 编写 CSV 到对象创建示例。

我创建了一个 bean,其中我有一个方法将执行所有操作,该方法将 return 列出。

我的问题是如何在 Camel 之外获取列表。

源代码:

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 上下文对象,我如何从上下文对象中获取列表。

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

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>.