Ricerca…


Jdbc Inbound Adapter - configurazione xml

Nel documento di riferimento ufficiale , dice:

La funzione principale di un adattatore di canale in entrata è eseguire una query SQL SELECT e trasformare il set di risultati in un messaggio. Il carico utile del messaggio è l'intero set di risultati, espresso come Elenco, e i tipi di elementi nell'elenco dipendono dalla strategia di mappatura delle righe utilizzata. La strategia predefinita è un mapper generico che restituisce solo una mappa per ogni riga nel risultato della query.

Jdbc Inbound Adapter

  • Codice sorgente

    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");
        }
    }
    
  • file di configurazione 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/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);
    
  • Sommario:

    • jdbcInbound : un adattatore per canale in entrata Jdbc. Esegue SQL SELECT * FROM BOOKS e converte il set di risultati in List<Book> tramite il bean bookRowMapper . Infine, invia questo elenco di libri al canale del channel .
    • channel : trasferisci il messaggio
    • outbound : un adattatore in uscita generico. vedi Adattatore canale in entrata e uscita generico

Adattatore canale in uscita Jdbc - configurazione xml

Nel documento di riferimento sull'integrazione di primavera , dice:

L'Adattatore canale in uscita è l'inverso dell'ingresso: il suo ruolo è quello di gestire un messaggio e utilizzarlo per eseguire una query SQL. Il carico utile del messaggio e le intestazioni sono disponibili per impostazione predefinita come parametri di input per la query ...

inserisci la descrizione dell'immagine qui

  • Codice 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");
        }
    }
    
  • file di configurazione 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
    );
    
  • Puoi osservare la tabella BOOKS e puoi vedere i record inseriti. Oppure puoi scrivere un int-jdbc:inbound-channel-adapter in int-jdbc:inbound-channel-adapter per contare la tabella BOOKS e puoi scoprire che il numero di conteggio cresce in continuazione.

  • Sommario:

    • inbound : un adattatore in entrata generico utilizzato per ottenere l'oggetto Book come payload del messaggio e inviarlo al canale del channel .
    • channel : usato per trasferire il messaggio.
    • jdbcOutbound : un adattatore in uscita JDBC, che riceve il messaggio con Book tipo e quindi preparare il parametro di query :title e :price via sqlParameterSource bean utilizzando SPEL come payload.title e payload.price per ottenere il titolo e prezzo di formare il messaggio payload.


Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow