JDBC
Enterprise applications require some sort of interaction with a database; Spring Integration provides solution to address this with ease. The JDBC channel adapters support sending and receiving messages via database queries. Spring JDBC supports HyperSQL databse by default. We will configure the base “datasource” bean using this database and using “jdbc” Spring namespace as shown in Listing 22-52 below.
Listing 22-52. Declaring datasource bean in Spring configuration file
1 2 3 4 5 |
<jdbc:embedded-database id="datasource"> … </jdbc:embedded-database> |
Listing 22-53 below shows maven dependency required for integrating JDBC using Spring Integration.
Listing 22-53. Maven dependency required
1 2 3 4 5 6 7 8 9 |
<dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-jdbc</artifactId> <version>${spring-integration-version}</version> </dependency> |
Listing 22-54 below shows configuration of main Spring Integration namespace and JDBC related namespace in the Spring configuration file (jdbc-integration-context.xml).
Listing 22-54. Spring Integration Namespace declaration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" xmlns:jdbc="http://www.springframework.org/schema/integration/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsd http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-2.1.xsd"> … </beans> |
Listing 22-55 shows configuring JDBC inbound channel adapter that uses a poller and datasource configured in previous code listing.
Listing 22-55. Configuring JDBC inbound channel adapter with a poller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<beans …> … <int:channel id="inboundChannel"/> <jdbc:inbound-channel-adapter query="..." channel="inboundChannel" data-source="datasource" update="..."> <int:poller fixed-rate="1000"> <int:transactional/> </int:poller> </jdbc:inbound-channel-adapter> … </beans> |
Listing 22-56 below shows configuring JDBC outbound channel adapter that uses datasource configured earlier.
Listing 22-56. Configuring JDBC outbound channel adapter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<beans …> … <int:channel id="inputChannel"/> <int:channel id="targetChannel"/> <jdbc:outbound-channel-adapter channel="inputChannel" query="…" data-source="datasource"/> <jdbc:inbound-channel-adapter channel="targetChannel" data-source="datasource" query="…" update="…"> <int:poller fixed-rate="1000"> <int:transactional/> </int:poller> </jdbc:inbound-channel-adapter> … </beans> |
The outbound Gateway is like a combination of the outbound and inbound adapters: its role is to handle a message and use it to execute a SQL query and then respond with the result sending it to a reply channel. Listing 22-57 shows configuring JDBC outbound gateway.
Listing 22-57. Configuring JDBC outbound gateway in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<beans …> … <int:channel id="inputChannel"/> <int:channel id="targetChannel"/> <jdbc:outbound-gateway update="…" request-channel="inputChannel" reply-channel="targetChannel" data-source="datasource" /> … </beans> |
If you would like to execute stored procedure and stored function, plain JDBC support will not suffice. Spring Integration providers following components to support this as follows:
- Stored Procedure Inbound Channel Adapter (Listing 22-58)
- Stored Procedure Outbound Channel Adapter (Listing 22-59)
- Stored Procedure Outbound Gateway (Listing 22-60)
Listing 22-58. Configuring Stored Procedure Inbound Channel Adapter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<beans …> … <jdbc:stored-proc-inbound-channel-adapter channel="" stored-procedure-name="" data-source="" auto-startup="true" id="" ignore-column-meta-data="false" is-function="false" max-rows-per-poll="" skip-undeclared-results="" </jdbc:stored-proc-inbound-channel-adapter> … </beans> |
Listing 22-59. Configuring Stored Procedure Outbound Channel Adapter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<beans …> … <jdbc:stored-proc-outbound-channel-adapter channel="" stored-procedure-name="" data-source="" auto-startup="true" id="" ignore-column-meta-data="false" order="" return-value-required="false" sql-parameter-source-factory="" use-payload-as-parameter-source=""> </jdbc:stored-proc-outbound-channel-adapter> … </beans> |
Listing 22-60. Configuring Stored Procedure Outbound Gateway
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<beans …> … <jdbc:stored-proc-outbound-gateway id=" " data-source="datasource" request-channel=" " skip-undeclared-results="true" stored-procedure-name=" " expect-single-result="true"> </jdbc:stored-proc-outbound-gateway> … </beans> |
Page Visitors: 13000

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Hi,
Thanks for this blog and information sharing!
I have one question from Spring Integration. Just thought of checking with you.
I have SimplewebserviceOutBoundGateway. I need to send a document / attachment to the webservice. How will i achieve that?
This is my current gateway configuration:
Please let me know, if you can throw some light in this area.
Thanks in Advance!
Hi,
Thank you for your information sharing.
I have one doubt,could you please suggest me the correct frameworks to address that issue.
My requirement is I would like to get and process files using spring and camel.
Thanks,
Madhu