Jdbc 出站通道适配器 - xml 配置

Spring Integration Reference Docuement 中 ,它说:

出站通道适配器是入站的反向:它的作用是处理消息并使用它来执行 SQL 查询。默认情况下,消息有效负载和标头可用作查询的输入参数…

StackOverflow 文档

  • Java 代码

    public class OutboundApplication {
        static class Book {
            String title;
            double price;
    
            Book(String title, double price) {
                this.title = title;
                this.price = price;
            }
    
            public double getPrice() {
                return price;
            }
    
            public String getTitle() {
                return title;
            }
    
        }
    
        static class Producer {
            public Book produce() {
                return IntStream.range(0, 3)
                        .mapToObj(i -> new Book("book" + i, i * 10))
                        .collect(Collectors.toList())
                        .get(new Random().nextInt(3));
            }
        }
    
        public static void main(String[] args) {
            new ClassPathXmlApplicationContext(
                    "classpath:spring/integration/stackoverflow/jdbc/jdbc-outbound.xml");
        }
    }
    
  • 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:jdbc="http://www.springframework.org/schema/jdbc"
           xmlns:int="http://www.springframework.org/schema/integration"
           xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/jdbc
           http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
           http://www.springframework.org/schema/integration
           http://www.springframework.org/schema/integration/spring-integration.xsd
           http://www.springframework.org/schema/integration/jdbc
           http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd">
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="url" value="jdbc:h2:tcp://localhost/~/booksystem"/>
            <property name="username" value="sa"/>
            <property name="password" value=""/>
            <property name="driverClassName" value="org.h2.Driver"/>
        </bean>
        <jdbc:initialize-database>
            <jdbc:script location="classpath:spring/integration/stackoverflow/jdbc/schema.sql"/>
        </jdbc:initialize-database>
        <int:channel id="channel"/>
        <int:inbound-channel-adapter channel="channel" method="produce" >
            <bean class="spring.integration.stackoverflow.jdbc.OutboundApplication$Producer"/>
            <int:poller fixed-rate="1000"/>
        </int:inbound-channel-adapter>
        <int-jdbc:outbound-channel-adapter id="jdbcOutbound"
                                           channel="channel"
                                           data-source="dataSource"
                                           sql-parameter-source-factory="sqlParameterSource"
                                           query="INSERT INTO BOOKS(TITLE, PRICE) VALUES(:title, :price)"/>
        <bean id="sqlParameterSource"   class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
            <property name="parameterExpressions">
                <map>
                    <entry key="title" value="payload.title"/>
                    <entry key="price" value="payload.price"/>
                </map>
            </property>
        </bean>
    </beans>
    
  • schema.sql 文件

    DROP TABLE IF EXISTS BOOKS;
    CREATE TABLE BOOKS (
      TITLE VARCHAR(20) NOT NULL,
      PRICE DOUBLE      NOT NULL
    );
    
  • 你可以观察 BOOKS 表,并可以看到插入的记录。或者你可以写一个 int-jdbc:inbound-channel-adapter 来计算 BOOKS 表,你会发现计数值不断增长。

  • 摘要:

    • inbound:一个通用的入站适配器,用于将 Book 对象作为消息有效负载并将其发送到通道 channel
    • channel:用于传递信息。
    • jdbcOutbound:一个 jdbc 出站适配器,它接收带有 Book 类型的消息,然后使用像 payload.titlepayload.price 这样的 SpEL 通过 sqlParameterSource bean 准备查询参数:title:price,从而获得消息有效负载的标题和价格。