spring-integration
Jdbc-Integration
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.
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 SQLSELECT * FROM BOOKSund konvertiert die Ergebnismenge über den BeanbookRowMapperinList<Book>. Zum Schluss wird diese Buchliste an den Kanalchannel. -
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.
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
BOOKSbeobachten und sehen, dass die Datensätze eingefügt werden. Oder Sie können einenint-jdbc:inbound-channel-adapterschreiben, um die TabelleBOOKSzu zählen, und Sie können feststellen, dass die Anzahl der Zähler kontinuierlich wächst.Zusammenfassung:
-
inbound: Ein generischer Inbound-Adapter, mit dem dasBookObjekt 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 demBookTyp und bereitet dann den Abfrageparameter vor:titleund:priceüber dassqlParameterSourceBean mit SpEL wiepayload.titleundpayload.price, um den Titel und den Preis aus der Nachrichtennutzlastpayload.price.
-

