...
The code for this example is as follows
Java Class | Purpose |
---|---|
The JPA entity bean (i.e. a POJO with @Entity) | |
The JAXB2 POJO used to parse the XML | |
The custom Type Converter used to convert a PersonDocument into a CustomerEntity | |
The Camel routing DSL to define the flow from the files to the converter to the JPA endpoint |
The there is the spring configuration file in src/resources/META-INF/services/camel-context.xml which defines the JPA template and tells Camel to look in the org.apache.camel.example.etl package to find its routes.
...
So lets start with the route definition in EtlRoutes
Wiki Markup |
---|
{snippet:id=example|lang=java|url=https://github.com/apache/camel/trunkblob/master/examples/camel-example-etl/src/main/java/org/apache/camel/example/etl/EtlRoutes.java} |
...
We're converting the body of the message to a PersonDocument which since this POJO as an @XmlRootElement annotation from JAXB will kick in the Type Converter to use JAXB to unmarshall the object.
Then we send the message with a PersonDocument body to the JPA endpoint. Notice how this endpoint specifies the expected type. So the Type Converter is gonna try convert the PersonDocument to a CustomerEntity. Here Camel will find the CustomerTransformer class which has an @Converter method
...
Code Block |
---|
mvn compile exec:java
|
Please note that when you run the example for the first time, the converter CustomerTransformer
will not be able to find any entities inside the database, so that along the logs written into the console you should see:
Code Block |
---|
thread #0 - file://src/data] CustomerTransformer INFO Created a new CustomerEntity Customer[userName: james firstName: null surname: null] as no matching persisted entity found.
thread #0 - file://src/data] CustomerTransformer INFO Created a new CustomerEntity Customer[userName: hiram firstName: null surname: null] as no matching persisted entity found.
|
However running the example for a second time, as the entites have been already inserted into the database, the log should now say:
Code Block |
---|
thread #0 - file://src/data] CustomerTransformer INFO Found a matching CustomerEntity Customer[userName: james firstName: James surname: Strachan] having the userName james.
thread #0 - file://src/data] CustomerTransformer INFO Found a matching CustomerEntity Customer[userName: hiram firstName: Hiram surname: Chirino] having the userName hiram.
|
Failing that you can run the Main from inside your IDE if you prefer. Follow the Building instructions to create an Eclipse/IDEA project to import