Introducing Spring Integration

Spring Integration

Recently, we worked on a project that required frequent polling of database table, processing each row, and writing output to different database table(s). The data tables are incoming and outgoing sms messages, and load would get quite high sometimes. We also had to support load balancing. Since our services are already done with spring – the choice was to try Spring Integration.

So what is Spring Integration? “Spring Integration provides an extension of the Spring programming model to support Enterprise Integration Patterns. It enables lightweight messaging within Spring-based applications and supports integration with external systems via declarative adapters.”

Basic concepts when designing Spring Integration applications are Messages (java objects with metadata), Channels (pipes that messages go trough) and Message Endpoints.
Message endpoints can be various – like Transformer (transforms data), Router(route data), Filter (filters data), Splitter (splits messages), Aggregator (inverse of Splitter), Service Activator (connecting messages to Services) and most important Channel Adapters.

Channel Adapters connects Message Channels to “external” world. They can be inbound (create new messages) and outbound (consumes messages). The choice of connection type is pretty big – like AMQP, JDBC, Web Services, FTP, File, HTTP, TCP, UDP, JMS, Mail, MongoDb, RMI, XML, XMPP, Stream and even Twitter.

Spring Integration Sample

Spring Integration Sample

Back to our application. Its very easy to add polling of database table. Just add Jdbc Inbound Channel Adapter that will poll database table with query that you specify (load balancing is working as we are using oracles “select for update nowait skip locked” syntax which skips rows with read lock). Specify properties like channel (name of channel where will message go), data-source, row-mapper (converts table row to java objects), update (query that is executed after table is read – you want to delete row once it is processed or put some flag) and fixed-rate (polling interval). There is also possibility to add transaction support , or define cron expression instead of fixed rate polling, but we didn’t need that. What we needed is to add Thread pool with Spring Task Executor, and combine this with Spring Integration by setting message channel as ExecutorChannel


	<int-jdbc:inbound-channel-adapter query="${db.select.userrequest.query}"
		channel="smsInboundChannel" data-source="dataSource" row-mapper="userRequestMapper"
		update="${db.delete.userrequest.query}"  max-rows-per-poll="${db.poller.maxrowsperpoll}" >
			<int:poller fixed-rate="${db.poller.fixedrate}"  >
			</int:poller>
	</int-jdbc:inbound-channel-adapter>


That's it. Now we just needed to make a Spring Integration chain. Add splitter to separate messages per row. Add Service Activator to process message. Finally add Jdbc Outbound Channel Adapter to write to database.The possibilities are numerous. And since everything is done using Spring concepts, the code is easily maintainable and testable.

About Samir Čauš

Samir Čauš is working as a Java developer at Five Minutes Ltd.
This entry was posted in Blog. Bookmark the permalink.

7 comments

  1. Thank you for the article. It is useful for me. Could you please share the complete code example?

  2. Hi Ramaraj,

    Unfortunately I can not give you access to code as this was done for our client and includes proprietary data…
    You should check Spring Integration samples hosted on Github https://github.com/SpringSource/spring-integration-samples/ , I found them rather useful.

    Samir

  3. I just like the helpful info you supply to your articles. I’ll bookmark your blog and test again right here regularly. I am quite sure I will be told lots of new stuff proper here! Good luck for the next!

    bySamir Mahmudzade | Official WebPage | SM ProCom | SM Production | SM Company | Samir Mahmudzade | S2M47
  4. Hi Samir,

    Just wanted to check if I deploy my poller application on muliple JVMs with oracles “select for update nowait skip locked” syntax.
    Will the poller on multiple JVMs not try to access previously read and unlocked rows of the polled DB?

    Any help would be much appreciated.

    Thanks,
    Ravi.

  5. Hey Samir,

    Great piece of info. Can you please provide example of splitter in this context. who will use userRequestMapper?

  6. I’ve learn a few good stuff here. Certainly worth bookmarking for revisiting.

    I wonder how so much attempt you set to make this kind of excellent
    informative website.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>