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

南楼画角 提交于 2020-04-18 12:35:16

问题


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:

  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.



来源:https://stackoverflow.com/questions/60544325/apache-camel-what-is-difference-between-message-translator-and-content-enricher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!