-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Streaming support for JdbcOutboundGateway
#10061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright 2002-2022 the original author or authors. | ||
| * Copyright 2002-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
|
|
@@ -18,6 +18,8 @@ | |
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.function.Consumer; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import javax.sql.DataSource; | ||
|
|
||
|
|
@@ -36,6 +38,7 @@ | |
| * @author Gunnar Hillert | ||
| * @author Artem Bilan | ||
| * @author Gary Russell | ||
| * @author Jiandong Ma | ||
| * | ||
| * @since 2.0 | ||
| */ | ||
|
|
@@ -53,6 +56,10 @@ public class JdbcOutboundGateway extends AbstractReplyProducingMessageHandler { | |
|
|
||
| private Integer maxRows; | ||
|
|
||
| private boolean queryForStream = false; | ||
|
|
||
| private Consumer<Object> streamConsumer; | ||
|
|
||
| /** | ||
| * Construct an instance based on the provided {@link DataSource} and update SQL. | ||
| * @param dataSource the {@link DataSource} for execution. | ||
|
|
@@ -158,7 +165,6 @@ public void setRequestPreparedStatementSetter(MessagePreparedStatementSetter req | |
| } | ||
|
|
||
| /** | ||
| /** | ||
| * Set a {@link SqlParameterSourceFactory} for select query. | ||
| * @param sqlParameterSourceFactory the {@link SqlParameterSourceFactory} to use. | ||
| */ | ||
|
|
@@ -175,6 +181,26 @@ public void setRowMapper(RowMapper<?> rowMapper) { | |
| this.poller.setRowMapper(rowMapper); | ||
| } | ||
|
|
||
| /** | ||
| * Set whether to use {@code queryForStream} for message polling. | ||
| * @param queryForStream the {@code queryForStream} to use. | ||
| * @since 7.0.0 | ||
| */ | ||
| public void setQueryForStream(boolean queryForStream) { | ||
| this.queryForStream = queryForStream; | ||
| } | ||
|
|
||
| /** | ||
| * Set stream consumer for processing {@code Stream} elements. | ||
| * This consumer is only applicable when {@code queryForStream} is set to true. | ||
| * @param streamConsumer the {@link Consumer to use}. | ||
| * @since 7.0.0 | ||
| */ | ||
| public void setStreamConsumer(Consumer<Object> streamConsumer) { | ||
| Assert.notNull(streamConsumer, "'streamConsumer' cannot be null"); | ||
| this.streamConsumer = streamConsumer; | ||
| } | ||
|
|
||
| @Override | ||
| public String getComponentType() { | ||
| return "jdbc:outbound-gateway"; | ||
|
|
@@ -186,6 +212,10 @@ protected void doInit() { | |
| Assert.notNull(this.poller, "If you want to set 'maxRows', then you must provide a 'selectQuery'."); | ||
| this.poller.setMaxRows(this.maxRows); | ||
| } | ||
| if (this.queryForStream) { | ||
| Assert.notNull(this.streamConsumer, "If you want to use 'queryForStream', then you must provide a 'streamConsumer'."); | ||
| this.poller.setMaxRows(0); // select all records in stream mode. | ||
| } | ||
|
|
||
| BeanFactory beanFactory = getBeanFactory(); | ||
| if (this.handler != null) { | ||
|
|
@@ -218,7 +248,15 @@ protected Object handleRequestMessage(Message<?> requestMessage) { | |
| sqlQueryParameterSource = this.sqlParameterSourceFactory.createParameterSource(list); | ||
| } | ||
| } | ||
| list = this.poller.doPoll(sqlQueryParameterSource); | ||
| if (this.queryForStream) { | ||
| try (Stream<?> stream = this.poller.doPollForStream(sqlQueryParameterSource)) { | ||
| stream.forEach(this.streamConsumer); | ||
| } | ||
| return Collections.emptyList(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the effort, but this is not correct. Having a I general I suggest to close this PR since we are not going the way you suggest. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ohh, make sense, thank you for making me think in messaging way, I was wrong from the beginning. |
||
| } | ||
| else { | ||
| list = this.poller.doPoll(sqlQueryParameterSource); | ||
| } | ||
| } | ||
| Object payload = list; | ||
| if (list.size() == 1 && (this.maxRows == null || this.maxRows == 1)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| create table item(id int,status int); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here given Object type for
Consumer, user have to do the type cast , which I am not very satisfied with this, because when setRowMapper, they already give return type.I don't have a good way. I tried to put
?, but looks it can not be applied to Stream<?> .