【发布时间】:2019-09-25 13:55:45
【问题描述】:
我目前有一个 TcpInboundGateway 接收消息,对消息进行一些处理,然后返回适当的响应,所有这些都是 TcpInboundGateway 应该的。
但是,我很好奇这个TcpInboundGateway 是否可以配置为它会立即响应原始请求但继续处理请求并发送后处理响应?
将此立即响应视为对发送者收到消息的确认。
可能的解决方案:
在查看了这个post 之后,我想出了一个我认为可行的解决方案。
@Configuration
@EnableIntegration
public class Configuration {
@Bean
public AbstractServerConnectionFactory serverConnectionFactory() {
return new TcpNetServerConnectionFactory(2002);
}
@Bean
public TcpReceivingChannelAdapter inboundAdapter(AbstractServerConnectionFactory serverConnectionFactory) {
TcpReceivingChannelAdapter inboundAdapter = new TcpReceivingChannelAdapter();
inboundAdapter.setConnectionFactory(serverConnectionFactory);
inboundAdapter.setOutputChannelName("sendAcknowledgement");
return inboundAdapter;
}
@MessageEndpoint
public class InboundMessageHandler {
@Autowired
private OutboundMessageGateway gateway;
@ServiceActivator(inputChannel="sendAcknowledgement", outputChannel="doProcessing")
public Message<String> initialAck(Message<String> message) {
gateway.send("ACK", message.getHeaders().get(IpHeaders.CONNECTION_ID).toString());
return message;
}
@ServiceActivator(inputChannel="doProcessing", outputChannel="sendResponse")
public Message<String> mockDelay(Message<String> message) throws InterruptedException {
return message;
}
}
@MessagingGateway(defaultRequestChannel="sendResponse")
public interface OutboundMessageGateway {
void send(@Payload String message, @Header(IpHeaders.CONNECTION_ID) String connectionId);
}
@Bean
@ServiceActivator(inputChannel="sendResponse")
public TcpSendingMessageHandler outboundAdapter(AbstractServerConnectionFactory serverConnectionFactory) {
TcpSendingMessageHandler outboundAdapter = new TcpSendingMessageHandler();
outboundAdapter.setConnectionFactory(serverConnectionFactory);
return outboundAdapter;
}
}
【问题讨论】:
-
看看this,听起来协作通道适配器可能是实现这一目标的途径......寻找任何可用的示例。
标签: java spring spring-boot tcp spring-integration