I am working on CSV to Object creation Example with the use of Camel.
I have create a bean in which i have a method with will do all operation, and that method will return List.
My Question is how i am able to get List outside the Camel.
Source Code:
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;
}
}
I have Only Camel Context Object how i am able to get List from context Object.
I found one answer for above question.
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") ;
}
}
For Consuming response I have use seda. And then after executing route I have consume same, and I get List<Person>
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.