'How to combine two messages with data retrieved from the MySQL db in Apache Camel?

I'm currently working on an integration project. I have to get some data from the MySQL database and them combine them using Apache Camel. In the database I've got two tables, materials and packages. They are in the one-to-many relation, one material can contain multiple packages. I've already figured out how to get data from the database and save them to json file, but I have no idea how to combine those two messages into one. I've read about Aggregations but I don't really get them. This is my first usage of Apache Camel and I don't really know what sould I do now. The code for those routes looks like this:

public class InputAdapter{

public static void main(String[] args) throws Exception {

    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/Packages");
    dataSource.setUsername("uname");
    dataSource.setPassword("passwd");
    SimpleRegistry registry = new SimpleRegistry();
    registry.bind("dataSource", dataSource);

    CamelContext context = new DefaultCamelContext(registry);
    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() {
            from("timer://foo?repeatCount=1")
                    .setBody(constant("SELECT * FROM material;"))
                    .to("jdbc:dataSource")
                    .marshal().json(true)
                    .to("file:/some/path/to/file?fileName=materials.json");
            from("timer://foo?repeatCount=1")
                    .setBody(constant("SELECT * FROM package;"))
                    .to("jdbc:dataSource")
                    .marshal().json(true)
                    .to("file:/some/path/to/file?fileName=packages.json");
        }
    });

    context.start();
    Thread.sleep(10000);
    context.stop();
}
}

And the model for Material and a Package are just private properties with getters and setters:

public class Material {
private int id;
private int number;
private enumType type;
private String name;
private String description;
private boolean is_deleted;
private List<Package> packageList = new ArrayList<>();

public enum enumType {
    A1, A2, A3, B1, B2, B3, Z1, Z2, Z3;
}

public int getId() {
    return this.id;
}

... some getters
}

public List<Package> getPackageList() {
    return this.packageList;
}

public void setId(int id) {
    this.id = id;
}

... some setters

public void setPackageList(List<Package> packages) {
    this.packageList = packages;
}
}

Can someone give me a hint what sould I do now? Please help me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source