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,從而獲得訊息有效負載的標題和價格。