I am relatively new to Camel and i’ve been struggling with a problem with a simple route that uses MyBatis to “feed” an ActiveMQ queue.
My route is as follows:
public class SearchItemProductionRouteRoute extends SpringRouteBuilder {
@Override
public void configure() throws Exception {
from("timer://pollTheDatabase?delay=5000")
.to("mybatis:selectSearchItem?statementType=SelectList&consumer.useIterator=true&consumer.onConsume=updateProcessingSearchItem")
.to("bean:searchItemProcesser?method=process")
.to("activemq:searchitemqueue");
}
}
The mybatis query is working fine. The query itself brings 4 records from the database. My need is that each row should become a message in the queue, but instead, i get 1 message with all the 4 rows in it.
The searchItemProcessor just prints de body of the message (thats how i know that the message contains all 4 records).
These are the queries that the above route uses:
<select id="selectSearchItem" resultMap="result" parameterType="java.util.HashMap">
SELECT * FROM SEARCH_REQUEST_ITEM SRI WHERE SRI.STATUS = '1'
</select>
<update id="updateProcessingSearchItem">
UPDATE SEARCH_REQUEST_ITEM SET STATUS = 2,
UPDATEDIN=SYSDATE, UPDATEDBY='XDRBATCH'
WHERE ID = #{ID}
</update>
If anyone can shed some light over this i’ll be thankful!
EDIT:
Just found one workaround using the Splitter EIP. First i created this class:
public class XdrMessageSplitterBean {
@SuppressWarnings({ "rawtypes", "unchecked" })
public List<HashMap> splitBody(Object body) {
return (List<HashMap>) body;
}
}
Then add it to the route:
public void configure() throws Exception {
from("timer://pollTheDatabase?delay=5000")
.to("mybatis:selectSearchItem?statementType=SelectList&consumer.useIterator=true&consumer.onConsume=updateProcessingSearchItem")
.split().method("xdrMessageSplitterBean", "splitBody")
.to("bean:searchItemProcesser?method=process")
.to("activemq:searchitemqueue");
}
The bean must be declared in the camel-context.xml file:
<bean id="xdrMessageSplitterBean" name="xdrMessageSplitterBean"
class="package.of.bean.XdrMessageSplitterBean" />
It works, but does not feel right. If anyone has any suggestion it will be very welcomed.
You can consume from mybatis directly, and specify the polling frequency. You dont need the timer for that. There is a delay option you can use to set = 5000 for 5 seconds frequency.