Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

...

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
xmlxml
titleSetting up database
xml
<!-- 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.

xml
Code Block
xml
titleSetting up Hibernate
xml
  <!-- 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.

xml
Code Block
xml
titleCamel application
xml
  <!-- 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

xml
Code Block
xml
titleHibernate mapping file to Order pojo
xml
<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>

...

Code Block
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Apache Camel 2.11.0 (CamelContext: camel-1) started in 0.836 seconds
[mel-1) thread #0 - timer://foo] generateOrder-route            INFO  Inserted new order 1
[camel.examples.hibernate.Order] processOrder-route             INFO  Processed order id 1 item 222 of 2 copies of ActiveMQ in Action
[mel-1) thread #0 - timer://foo] generateOrder-route            INFO  Inserted new order 2
[camel.examples.hibernate.Order] processOrder-route             INFO  Processed order id 2 item 111 of 2 copies of Camel in Action
...

See Also