Spring Integretion- JDBC Support

1. JDBC Support

Spring Integration 相关的JDBC Support 使用帮助

1.1 Overview

Maven依赖

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-jdbc</artifactId>
    <version>5.1.2.RELEASE</version>
</dependency>

Oracle DB依赖

<dependency>
	<groupId>com.oracle</groupId>
	<artifactId>ojdbc8</artifactId>
	<version>12.2.0.1</version>
</dependency>

2. 相关组件详细描述

2.1 Inbound Channel Adapter

使用帮助

2.2 Outbound Channel Adapter

2.3 Outbound Gateway

2.4 Stored Procedure Inbound Channel Adapter

2.5 Stored Procedure Outbound Channel Adapter

2.6 Stored Procedure Outbound Gateway

3. Demo 程序

3.1 si01-jdbcpoll

3.1.1 简介

监听数据库表, 每隔一定时间读取数据库数据。

3.1.2 配置及代码

数据库表 LOG_TMP:

CREATE TABLE "SGMSOA_SVCGOV"."LOG_TMP" (
    "UUID"   VARCHAR2(50 BYTE) NOT NULL ENABLE,
    "TAG"    VARCHAR2(5 BYTE) DEFAULT 0,
    CONSTRAINT "LOG_TMP_PK" PRIMARY KEY ( "UUID" )
);

pom.xml dependency

<dependencies>
	<dependency>
		<groupId>org.springframework.integration</groupId>
		<artifactId>spring-integration-jdbc</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-integration</artifactId>
	</dependency>
	<dependency>
		<groupId>com.oracle</groupId>
		<artifactId>ojdbc8</artifactId>
		<version>12.2.0.1</version>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

si.xml

<?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-jdbc="http://www.springframework.org/schema/integration/jdbc"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:int="http://www.springframework.org/schema/integration"
	xsi:schemaLocation="http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-5.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-5.1.xsd">

	<bean id="msg_printer"
		class="com.cai.test.si01jdbcpoll.services.MessagePrinter"></bean>

	<int-jdbc:inbound-channel-adapter
		id="inbound" channel="jdbcChannel"
		query="select * from LOG_TMP where ROWNUM &lt;=2"
		update="update LOG_TMP set tag=1 where uuid in (:uuid)"
		data-source="jdbcTemplate">
		<int:poller fixed-rate="10000" max-messages-per-poll="3">
			<int:transactional />
		</int:poller>
	</int-jdbc:inbound-channel-adapter>

	<int:channel id="jdbcChannel"></int:channel>

	<int:service-activator ref="msg_printer"
		input-channel="jdbcChannel" method="printConsole"></int:service-activator>
</beans>

data-source=“jdbcTemplate”: 其中jdbcTemplate由 SpringBoot 自动设置并注入,
Springboot配置如下:

spring.datasource.driver-class-name: oracle.jdbc.OracleDriver
spring.datasource.url: jdbc:oracle:thin:@//192.168.1.101:1521/QADB
spring.datasource.username: si_testuser
spring.datasource.password: password

入口程序 Si01JdbcpollApplication.java

@SpringBootApplication
@ImportResource("si.xml")
public class Si01JdbcpollApplication {

	public static void main(String[] args) {
		SpringApplication.run(Si01JdbcpollApplication.class, args);
	}
}

MessagePrinter.java

public class MessagePrinter {

	public String print(Message<?> message) {
		return "From inbound gateway: 1 message worked";
	}

	public void printConsole(Message<?> message) {
		String timeStamp = "[" + new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()) + "] ";
		System.out.println(timeStamp + "Message printConsole: " + message);
	}
}

3.1.3 测试及输出

从LOG_TMP中查询 tag为0的数据, 10秒中poll一次, 一次最多取2条, 每次poll最多执行3次。

Under normal circumstances, you would likely not want to set the poller’s max-messages-per-poll property when you use the JDBC inbound channel adapter. Its default value is 1, which means that the JDBC inbound channel adapter’s receive() method is executed exactly once for each poll interval.

这样配置下, 每次poll(10s间隔)从数据库中可以取出的tag为0的数据最多为 2*3=6条。 如果数据大于6条, 会在下一个poll周期进行处理。
13条数据的情况下, 输出结果如下:

[2019.04.01.15.50.20] Message printConsole: GenericMessage [payload=... 2 条
[2019.04.01.15.50.20] Message printConsole: GenericMessage [payload=... 2 条
[2019.04.01.15.50.20] Message printConsole: GenericMessage [payload=... 2 条

[2019.04.01.15.50.29] Message printConsole: GenericMessage [payload=... 2 条
[2019.04.01.15.50.29] Message printConsole: GenericMessage [payload=... 2 条
[2019.04.01.15.50.29] Message printConsole: GenericMessage [payload=... 2 条

[2019.04.01.15.50.39] Message printConsole: GenericMessage [payload=... 1 条
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值