...
In the camel-context.xml
file in the src/main/resources/META-INF/spring
folder we have the Spring XML file to setup and configure the database and Hibernate, as well the CamelContext.
Code Block |
---|
| xml |
---|
| xml |
---|
title | Setting up databasexml |
---|
|
<!-- this is the JDBC data source which uses an in-memory only Apache Derby database -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:memory:orders;create=true"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
|
And in the same file we setup Hibernate. At first we have the Camel Hibernate component, and then a number of beans to setup transactions. And then the last bean setup the Hibernate session factory where we refer to the data source and the hibernate mapping files, and any other hibernate configurations we may need.
Code Block |
---|
| xml |
---|
| xml |
---|
title | Setting up Hibernatexml |
---|
|
<!-- setup the Camel hibernate component -->
<bean id="hibernate" class="org.apacheextras.camel.component.hibernate.HibernateComponent">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="transactionStrategy" ref="springTransactionStrategy"/>
</bean>
<!-- setup hibernate and spring to use transaction -->
<bean id="springTransactionStrategy" class="org.apacheextras.camel.component.hibernate.SpringTransactionStrategy">
<constructor-arg ref="sessionFactory"/>
<constructor-arg ref="transactionTemplate"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<!-- setup Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- here we define the hibernate mapping files we use -->
<property name="mappingResources">
<list>
<value>Order.hbm.xml</value>
</list>
</property>
<!-- and here we have additional hibernate options -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.DerbyTenSevenDialect
hibernate.hbm2ddl.auto=create
</value>
</property>
</bean>
|
And then in the same file we setup our Camel application. At first we have a orderBean that we use in the routes to generate new orders and process orders as well.
Code Block |
---|
| xml |
---|
| xml |
---|
title | Camel applicationxml |
---|
|
<!-- order bean is our business logic bean that creates new orders -->
<bean id="orderBean" class="org.apacheextras.camel.examples.hibernate.OrderBean"/>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<!-- route that generate new orders and insert them in the database -->
<route id="generateOrder-route">
<from uri="timer:foo?period=5s"/>
<transform>
<method ref="orderBean" method="generateOrder"/>
</transform>
<to uri="hibernate:org.apacheextras.camel.examples.hibernate.Order"/>
<log message="Inserted new order ${body.id}"/>
</route>
<!-- route that process the orders by picking up new rows from the database
and when done processing then update the row to mark it as processed -->
<route id="processOrder-route">
<from uri="hibernate:org.apacheextras.camel.examples.hibernate.Order?delay=1s"/>
<to uri="bean:orderBean?method=processOrder"/>
<log message="${body}"/>
</route>
</camelContext>
|
...
The Order pojo is mapped to Hibernate using the mapping file Order.hbm.xml
which is located in the src/main/resources
directory of the example
Code Block |
---|
| xml |
---|
| xml |
---|
title | Hibernate mapping file to Order pojoxml |
---|
|
<hibernate-mapping>
<class name="org.apacheextras.camel.examples.hibernate.Order" schema="orders" table="incoming_order">
<id name="id">
<generator class="native"/>
</id>
<property name="item" length="8"/>
<property name="amount" length="4"/>
<property name="description" length="50"/>
</class>
</hibernate-mapping>
|
...