In this tutorial we will learn how to persist some data on a Database using a Camel route which is based on the Java Persistence API defined in a project.

The steps to complete this tutorial are:

  1. Let's start from a maven archetype
  2. Create the entity bean
  3. Configure the persistence.xml
  4. Configure the camel-context
  5. Define the camel route
  6. enjoy running it !

1. Camel Maven archetype

There are a lot of archetypes to start with camel. This one configures a camel project based on java:

org.apache.camel.archetype/camel-archetype-java/2.13.2

Using Eclipse: File > New Maven Project

2. Create the entity bean

The class Person represents the entity bean that will be inserted into the database:

@XmlRootElement
@XmlType
@Entity(name="Person")
public class Person {
	private Long id;
private String name;
private String surname;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getSurname() {
	return surname;
}
public void setSurname(String surname) {
	this.surname = surname;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
    return id;
}

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


}

To help jaxb to create the JAXBContext we add the file jaxb.index in the model package with this single line inside the model package:

Person

3. Configure the persistence.xml

In the META-INF folder we add the following persistence.xml file with the jdbc properties to connect to the database:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="persistenceUnit"
        transaction-type="RESOURCE_LOCAL">

         <properties>

            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />
            <property name="hbm2ddl.auto" value="create" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        </properties>

    </persistence-unit>
    
    
</persistence>

4. Configure the camel-context

The following file, using spring, configures a camel context. A simple java route is added to the camel context.

In the camel context we add the jpa component. It needs also an EntityManagerFactory and a JpaTransactionManager.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camelContext trace="true" xmlns="http://camel.apache.org/schema/spring">
    <routeBuilder ref="myroute"/>
</camelContext>
    <bean id="myroute" class="com.mycompany.camel.java.jpa.MyRouteBuilder"/>
    <bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
       <property name="entityManagerFactory" ref="entityManagerFactory"/>
       <property name="transactionManager" ref="jpaTxManager"/>
    </bean>
    <bean id="jpaTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="persistenceUnit"/>
    </bean>
</beans>

5. Define the camel route

The camel route is defined by MyRouteBuilder class in the configure method.

public void configure() {
    	org.apache.camel.converter.jaxb.JaxbDataFormat jxb = new JaxbDataFormat("com.mycompany.camel.java.model");
        from("file:src/data?noop=true")
        .unmarshal(jxb)
        .to("jpa:com.mycompany.camel.java.model.Person");
        
    }

A JaxbDataFormat transforms data from xml to a Person entity and the jpa component inserts it in the database.

6. Enjoy running it

Before running it a database table has to be create.

The hibernate3:hbm2ddl is the maven plugin we are looking for!

mvn hibernate3:hbm2ddl generates the sql create table:

[INFO] writing generated schema to file: /home/marco/FuseWorkshopWorkspace/camel-java-jpa/target/hibernate3/sql/schema.ddl

    drop table if exists Person;

    create table Person (
        id bigint not null auto_increment,
        name varchar(255),
        surname varchar(255),
        primary key (id)
    );

Then camel plugin runs the route:

mvn clean  hibernate3:hbm2ddl compile camel:run

The trace="true" option on the camel context make camel to trace the exchange transported in the route:

[.camel.java.jpa.MainApp.main()] SpringCamelContext             INFO  Apache Camel 2.10.0 (CamelContext: camel-1) started in 0.318 seconds
[1) thread #1 - file://src/data] Tracer                         INFO  ID-localhost-localdomain-47554-1425079065546-0-2 >>> (route1) from(file://src/data?noop=true) -->  <<< Pattern:InOnly, Headers:{CamelFileRelativePath=message1.xml, CamelFileParent=src/data, CamelFileLastModified=Fri Feb 27 18:24:56 CET 2015, breadcrumbId=ID-localhost-localdomain-47554-1425079065546-0-1, CamelFileAbsolutePath=/home/marco/FuseWorkshopWorkspace/camel-java-jpa/src/data/message1.xml, CamelFilePath=src/data/message1.xml, CamelFileName=message1.xml, CamelFileLength=123, CamelFileNameOnly=message1.xml, CamelFileAbsolute=false}, BodyType:org.apache.camel.component.file.GenericFile, Body:[Body is file based: GenericFile[message1.xml]]
[1) thread #1 - file://src/data] Tracer                         INFO  ID-localhost-localdomain-47554-1425079065546-0-2 >>> (route1)  --> jpa://com.mycompany.camel.java.model.Person <<< Pattern:InOnly, Headers:{CamelFileName=message1.xml, CamelFilePath=src/data/message1.xml, CamelFileAbsolute=false, CamelFileNameOnly=message1.xml, CamelFileAbsolutePath=/home/marco/FuseWorkshopWorkspace/camel-java-jpa/src/data/message1.xml, CamelFileParent=src/data, CamelFileLength=123, CamelFileLastModified=Fri Feb 27 18:24:56 CET 2015, CamelFileRelativePath=message1.xml, breadcrumbId=ID-localhost-localdomain-47554-1425079065546-0-1}, BodyType:com.mycompany.camel.java.model.Person, Body:<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
    <name>Marco</name>
    <surname>Battaglia</surname>
</person>

[1) thread #1 - file://src/data] Tracer                         INFO  ID-localhost-localdomain-47554-1425079065546-0-4 >>> (route1) from(file://src/data?noop=true) -->  <<< Pattern:InOnly, Headers:{CamelFileLastModified=Fri Feb 27 18:24:56 CET 2015, CamelFilePath=src/data/message2.xml, CamelFileAbsolutePath=/home/marco/FuseWorkshopWorkspace/camel-java-jpa/src/data/message2.xml, CamelFileName=message2.xml, breadcrumbId=ID-localhost-localdomain-47554-1425079065546-0-3, CamelFileNameOnly=message2.xml, CamelFileAbsolute=false, CamelFileLength=132, CamelFileRelativePath=message2.xml, CamelFileParent=src/data}, BodyType:org.apache.camel.component.file.GenericFile, Body:[Body is file based: GenericFile[message2.xml]]
[1) thread #1 - file://src/data] Tracer                         INFO  ID-localhost-localdomain-47554-1425079065546-0-4 >>> (route1)  --> jpa://com.mycompany.camel.java.model.Person <<< Pattern:InOnly, Headers:{CamelFileAbsolute=false, CamelFileLastModified=Fri Feb 27 18:24:56 CET 2015, CamelFileRelativePath=message2.xml, CamelFileParent=src/data, breadcrumbId=ID-localhost-localdomain-47554-1425079065546-0-3, CamelFileAbsolutePath=/home/marco/FuseWorkshopWorkspace/camel-java-jpa/src/data/message2.xml, CamelFileLength=132, CamelFilePath=src/data/message2.xml, CamelFileNameOnly=message2.xml, CamelFileName=message2.xml}, BodyType:com.mycompany.camel.java.model.Person, Body:<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
    <name>Francesco</name>
    <surname>Marchioni</surname>
</person>

6.a) You can also use another mvn plugin in order to run camel using java directly.

1) Change the import in the Main class:

import org.apache.camel.spring.Main

in order to make spring to manage the route

2) remove the line "main.addRouteBuilder(new MyRouteBuilder());"

Spring will automatically add the route to the camel context using the camel-context.xml file.

3) Execute:

mvn clean hibernate3:hbm2ddl compile exec:java

6.b) How to read from database?

Exercise: Try it:

public void configure() {
    
org.apache.camel.converter.jaxb.JaxbDataFormat jaxb = new JaxbDataFormat("com.mycompany.camel.java.model");
        from("jpa:com.mycompany.camel.java.model.Person?consumer.query=select o from com.mycompany.camel.java.model.Person o where o.name='Marco'")
        .marshal(jaxb)
        .log("${body}");
           
    }

The complete code is here: https://github.com/mastertheintegration/camel-lesson

0
0
0
s2smodern