如何在现有Spring Integration流程的中间添加以下流程:
来自emp表的查询。它返回例如3排。将这3行插入dept表,然后继续之前运行的流程。
Splitter是手动配置来实现它的。但是,<int:aggregator />
之后流量不会继续。
EMP-context.xml中
...
<int:splitter apply-sequence="false" /> <!-- parent splitter -->
...
<jdbc:outbound-gateway
query="SELECT * FROM emp"
row-mapper="empMapper" data-source="myDataSource"
max-rows-per-poll="100000" />
<int:service-activator ref="myService" method="process" />
<!-- <int:splitter /> child splitter commented out -->
<jdbc:outbound-gateway
update="INSERT INTO dept (name, dept)
VALUES(:headers[name], :headers[dept])"
data-source="myDataSource" />
<int:aggregator />
<!-- flow is not executed after aggregator -->
<jdbc:outbound-gateway
update="INSERT INTO other_table (mycol)
VALUES(:headers[mycol])"
data-source="myDataSource" />
...
MyService.java
public List<Message<String>> process(List<EmpRecord> records, @Headers MessageHeaders headers) {
List<Message<String>> result = new ArrayList<Message<String>>();
UUID correlationId = UUID.randomUUID();
int sequenceNumber = 0;
int sequenceSize = records.size();
for (EmpRecord record : records) {
msg = MessageBuilder
.withPayload("null")
.copyHeaders(headers) // headers contains mycol key that I want to preserve to use after aggregator.
.setHeader("name", record.getName())
.setHeader("dept", record.getDept())
.pushSequenceDetails(correlationId, ++sequenceNumber, sequenceSize)
.build();
result.add(msg);
}
return result;
}
编辑:添加了堆栈跟踪。
请注意,上述代码中提到的标题,类和表名称与实际实现不同。所以,我已根据上面的代码修改了堆栈跟踪。
06:43:35.171 DEBUG [main][org.springframework.jdbc.core.JdbcTemplate] Executing prepared SQL query
06:43:35.171 DEBUG [main][org.springframework.jdbc.core.JdbcTemplate] Executing prepared SQL statement [SELECT * FROM emp]
06:43:35.171 DEBUG [main][org.springframework.integration.jdbc.JdbcOutboundGateway] handler 'org.springframework.integration.handler.MessageHandlerChain#2$child#0.handler' sending reply Message: [Payload=[com.model.EmpRecord@9a18a0, com.model.EmpRecord@c6e1ec, com.model.EmpRecord@11a5fd0, com.model.EmpRecord@8890da]][Headers={mycol=c1}]
06:43:35.171 DEBUG [main][org.springframework.integration.handler.ServiceActivatingHandler] ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@1de007d] received message: [Payload=[com.model.EmpRecord@9a18a0, com.model.EmpRecord@c6e1ec, com.model.EmpRecord@11a5fd0, com.model.EmpRecord@8890da]][Headers={mycol=c1}]
06:43:35.171 INFO [main][com.service.MyServiceActivator] START :: process
06:43:35.171 INFO [main][com.service.MyServiceActivator] END :: process
06:43:35.171 DEBUG [main][org.springframework.integration.handler.ServiceActivatingHandler] handler 'ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@1de007d]' sending reply Message: [Payload=null][Headers={name=john, dept=BE, mycol=c1}]
06:43:35.171 DEBUG [main][org.springframework.integration.jdbc.JdbcOutboundGateway] org.springframework.integration.handler.MessageHandlerChain#2$child#3.handler received message: [Payload=null][Headers={name=john, dept=BE, mycol=c1}]
06:43:35.187 DEBUG [main][org.springframework.jdbc.core.JdbcTemplate] Executing prepared SQL update
06:43:35.187 DEBUG [main][org.springframework.jdbc.core.JdbcTemplate] Executing prepared SQL statement [INSERT INTO dept (name, dept) VALUES(?, ?)]
06:43:35.187 DEBUG [main][org.springframework.jdbc.core.JdbcTemplate] SQL update affected 1 rows
06:43:35.187 DEBUG [main][org.springframework.integration.jdbc.JdbcOutboundGateway] handler 'org.springframework.integration.handler.MessageHandlerChain#2$child#3.handler' sending reply Message: [Payload={UPDATED=1}][Headers={name=john, dept=BE, mycol=c1}]
06:43:35.187 DEBUG [main][org.springframework.integration.aggregator.AggregatingMessageHandler] org.springframework.integration.handler.MessageHandlerChain#2$child#4.handler received message: [Payload={UPDATED=1}][Headers={name=john, dept=BE, mycol=c1}]
06:43:35.187 DEBUG [main][org.springframework.integration.aggregator.AbstractCorrelatingMessageHandler] Handling message with correlationKey [cc7b7fc3-8d7e-bf15-240f-9e893ab7f5cc]: [Payload={UPDATED=1}][Headers={name=john, dept=BE, mycol=c1}]
06:43:35.171 DEBUG [main][org.springframework.integration.handler.ServiceActivatingHandler] handler 'ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@1de007d]' sending reply Message: [Payload=null][Headers={name=john, dept=BE, mycol=c1}]
06:43:35.171 DEBUG [main][org.springframework.integration.jdbc.JdbcOutboundGateway] org.springframework.integration.handler.MessageHandlerChain#2$child#3.handler received message: [Payload=null][Headers={name=john, dept=BE, mycol=c1}]
06:43:35.218 DEBUG [main][org.springframework.jdbc.core.JdbcTemplate] Executing prepared SQL update
06:43:35.218 DEBUG [main][org.springframework.jdbc.core.JdbcTemplate] Executing prepared SQL statement INSERT INTO dept (name, dept) VALUES(?, ?))]
06:43:35.218 DEBUG [main][org.springframework.jdbc.core.JdbcTemplate] SQL update affected 1 rows
06:43:35.218 DEBUG [main][org.springframework.integration.jdbc.JdbcOutboundGateway] handler 'org.springframework.integration.handler.MessageHandlerChain#2$child#3.handler' sending reply Message: [Payload={UPDATED=1}][Headers={name=ravi, dept=SE, mycol=c1}]
06:43:35.218 DEBUG [main][org.springframework.integration.aggregator.AggregatingMessageHandler] org.springframework.integration.handler.MessageHandlerChain#2$child#4.handler received message: [Payload={UPDATED=1}][Headers={name=ravi, dept=SE, mycol=c1}]
06:43:35.218 DEBUG [main][org.springframework.integration.aggregator.AbstractCorrelatingMessageHandler] Handling message with correlationKey [fc91e0bc-87a3-251f-87fd-fc3901285cde]: [Payload={UPDATED=1}][Headers={name=ravi, dept=SE, mycol=c1}]
答案 0 :(得分:1)
我看到了原因的根源!
由于您从MyServiceActivator
返回List<Message<String>>
,AbstractReplyProducingMessageHandler
将其确定为Iterable<Message<?>>
并将其逐个发送到output-channel
。所以<splitter>
没有看到任何要拆分的东西,并按原样发送它们 - 一条输入消息,一条输出消息。从<aggregator>
collect
groups
看不到<splitter>
。这就是为什么它为每个输入生成几条消息 - 只是因为它们位于不同的sequenceDetails
。
要解决您的问题,您应该摆脱public List<Message<String>> process(List<EmpRecord> records, @Headers MessageHeaders headers) {
List<Message<String>> result = new ArrayList<Message<String>>();
UUID correlationId = UUID.randomUUID();
int sequenceNumber = 0;
int sequenceSize = records.size();
for (EmpRecord record : records) {
msg = MessageBuilder
.withPayload("null")
.copyHeaders(headers) // headers contains mycol key that I want to preserve to use after aggregator.
.setHeader("name", record.getName())
.setHeader("dept", record.getDept())
.setHeader("correlationId", correlationId)
.setHeader("sequenceNumber", ++sequenceNumber)
.setHeader("sequenceSize", sequenceSize)
.build();
result.add(msg);
}
return result;
}
并手动填充{{1}}:
{{1}}