Suche…


Jdbc Inbound Adapter - XML-Konfiguration

Im offiziellen Referenzdokument heißt es:

Die Hauptfunktion eines Inbound Channel Adapters besteht darin, eine SQL SELECT-Abfrage auszuführen und die Ergebnismenge als Nachricht anzuzeigen. Die Nachrichtennutzlast ist die gesamte Ergebnismenge, ausgedrückt als Liste. Die Arten der Elemente in der Liste hängen von der verwendeten Zeilenzuordnungsstrategie ab. Die Standardstrategie ist ein generischer Mapper, der nur eine Map für jede Zeile im Abfrageergebnis zurückgibt.

Jdbc-Inbound-Adapter

  • Quellcode

    public class Application {
       static class Book {
          String title;
          double price;
    
          Book(String title, double price) {
              this.title = title;
              this.price = price;
          }
    
          double getPrice() {
              return price;
          }
    
          String getTitle() {
              return title;
          }
    
          @Override
          public String toString() {
              return String.format("{title: %s, price: %s}", title, price);
          }
        }
    
        static class Consumer {
          public void consume(List<Book> books) {
              books.stream().forEach(System.out::println);
          }
        }
    
        static class BookRowMapper implements RowMapper<Book> {
    
          @Override
          public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
              String title = rs.getString("TITLE");
              double price = rs.getDouble("PRICE");
              return new Book(title, price);
          }
        }
    
        public static void main(String[] args) {
          new ClassPathXmlApplicationContext(
                  "classpath:spring/integration/stackoverflow/jdbc/jdbc.xml");
        }
    }
    
  • XML-Konfigurationsdatei

    <?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/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
           http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
        <jdbc:embedded-database id="dataSource" type="H2">
            <jdbc:script location="classpath:spring/integration/stackoverflow/jdbc/schema.sql"/>
        </jdbc:embedded-database>
        <bean id="bookRowMapper"
              class="spring.integration.stackoverflow.jdbc.Application$BookRowMapper"/>
    
        <int:channel id="channel"/>
    
        <int-jdbc:inbound-channel-adapter id="jdbcInbound"
                                          channel="channel"
                                          data-source="dataSource"
                                          query="SELECT * FROM BOOKS"
                                          row-mapper="bookRowMapper">
            <int:poller fixed-rate="1000"/>
        </int-jdbc:inbound-channel-adapter>
    
        <int:outbound-channel-adapter id="outbound" channel="channel" method="consume">
            <bean class="spring.integration.stackoverflow.jdbc.Application$Consumer"/>
        </int:outbound-channel-adapter>
    </beans>
    
  • schema.sql

    CREATE TABLE BOOKS (
      TITLE VARCHAR(20) NOT NULL,
      PRICE DOUBLE      NOT NULL
    );
    
    INSERT INTO BOOKS(TITLE, PRICE) VALUES('book1', 10);
    INSERT INTO BOOKS(TITLE, PRICE) VALUES('book2', 20);
    
  • Zusammenfassung:

    • jdbcInbound : Ein Jdbc-Kanaladapter. Es führt das SQL SELECT * FROM BOOKS und konvertiert die Ergebnismenge über den Bean bookRowMapper in List<Book> . Zum Schluss wird diese Buchliste an den Kanal channel .
    • channel : Übertragen Sie die Nachricht
    • outbound : ein generischer Outbound-Adapter. siehe Generic Inbound und Outbound Channel Adapter

Jdbc Outbound Channel Adapter - XML-Konfiguration

In der Referenzdokumentation zur Spring Integration heißt es:

Der Outbound Channel Adapter ist die Umkehrung des Inbound: Seine Aufgabe ist es, eine Nachricht zu verarbeiten und sie zum Ausführen einer SQL-Abfrage zu verwenden. Die Nachrichten-Payload und -Header sind standardmäßig als Eingabeparameter für die Abfrage verfügbar.

Geben Sie hier die Bildbeschreibung ein

  • Java-Code

    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-Konfigurationsdatei

    <?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
    );
    
  • Sie können die Tabelle BOOKS beobachten und sehen, dass die Datensätze eingefügt werden. Oder Sie können einen int-jdbc:inbound-channel-adapter schreiben, um die Tabelle BOOKS zu zählen, und Sie können feststellen, dass die Anzahl der Zähler kontinuierlich wächst.

  • Zusammenfassung:

    • inbound : Ein generischer Inbound-Adapter, mit dem das Book Objekt als Nachrichtennutzlast abgerufen und an den Channel- channel .
    • channel : wird zum Übertragen der Nachricht verwendet.
    • jdbcOutbound : Ein jdbc-Outbound-Adapter. Er empfängt die Nachricht mit dem Book Typ und bereitet dann den Abfrageparameter vor :title und :price über das sqlParameterSource Bean mit SpEL wie payload.title und payload.price , um den Titel und den Preis aus der Nachrichtennutzlast payload.price .


Modified text is an extract of the original Stack Overflow Documentation
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow