问题
I hit database get 10 employees; on base of each employee i hit another database and fetch some information and concatenate the same.
As per my understanding, It can be done either in .process() or in .enrich() (using aggregator)
.to("jdbc:masterdata?outputClass=com.diavry.integrator.Employee")
.to("log:?level=INFO&showBody=true")
.process(e -> {
List<Employee> eiEmployees = (List<Employee>) e.getIn().getBody(List.class);
for (Employee employee : eiEmployees) {
PreparedStatement statement = otherDbConnection.prepareStatement(sql);
statement.setString(1, employee.getUserid());
statement.setString(2, employee.getCompanyid());
resultSet = statement.executeQuery();
if (resultSet.next()) {
legalUnitName = resultSet.getString(1);
}
employee.setOrgstr_unitname(legalUnitName);
}
})
Now i can do same thing in Aggregator where i can enrich original with above code and return back .
I am not getting difference between two in relation to above use case?
回答1:
Well, the main difference is that you wrote JDBC code in your Processor
(1). Another difference is that you manage the iteration to get detail data for every employee by yourself. That also means that you need to do any error handling by yourself (how to recover if processing aborts in the middle of the iteration etc).
The Camel way to solve this use case is:
- JDBC call to get employees
- Splitter to split the employee list into individual messages (creates "iteration")
- JDBC detail data call per employee
- Further process detail message or aggregate all detail messages, depending on your further processing needs
This is the main magic of Camel! No need to write lots of "transport-level" code. Just write one line of Camel DSL to query a database, ramp up a JMS consumer and any other integration technology you can think of.
And of course all the EIPs is implements that are common integration problems.
(1) Side note: I recommend to drop the low-level interface Processor
in favor of simple Java Beans.
来源:https://stackoverflow.com/questions/60544325/apache-camel-what-is-difference-between-message-translator-and-content-enricher