fatherazrael
fatherazrael

Reputation: 5957

Apache Camel: What is difference between Message Translator and Content Enricher with Example?

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?

Upvotes: 0

Views: 674

Answers (1)

burki
burki

Reputation: 7005

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:

  1. JDBC call to get employees
  2. Splitter to split the employee list into individual messages (creates "iteration")
  3. JDBC detail data call per employee
  4. 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.

Upvotes: 1

Related Questions