Spring integration Spring集成TcpInboundGateway发送条件应答
我已将Spring integration Spring集成TcpInboundGateway发送条件应答,spring-integration,Spring Integration,我已将TcpInboundGateway配置为接收来自客户端的请求,我的配置如下。因此,根据下面的配置,每个请求的客户机都会得到响应,但我想要的是,只有在特定条件为真时,响应才会被发回,而不是每次,在配置中需要做哪些更改 @SpringBootApplication @IntegrationComponentScan public class SpringIntegrationApplication extends SpringBootServletInitializer{ publ
TcpInboundGateway
配置为接收来自客户端的请求,我的配置如下。因此,根据下面的配置,每个请求的客户机都会得到响应,但我想要的是,只有在特定条件为真时,响应才会被发回,而不是每次,在配置中需要做哪些更改
@SpringBootApplication
@IntegrationComponentScan
public class SpringIntegrationApplication extends SpringBootServletInitializer{
public static void main(String[] args) throws IOException {
ConfigurableApplicationContext ctx = SpringApplication.run(SpringIntegrationApplication.class, args);
System.in.read();
ctx.close();
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringIntegrationApplication.class);
}
private static Class<SpringIntegrationApplication> applicationClass = SpringIntegrationApplication.class;
@Bean
TcpNetServerConnectionFactory cf(){
TcpNetServerConnectionFactory connectionFactory=new TcpNetServerConnectionFactory(8765);
return connectionFactory;
}
@Bean
TcpInboundGateway tcpGate(){
TcpInboundGateway gateway=new TcpInboundGateway();
gateway.setConnectionFactory(cf());
gateway.setRequestChannel(requestChannel());
return gateway;
}
@Bean
public MessageChannel requestChannel(){
return new DirectChannel();
}
@MessageEndpoint
public class Echo {
@ServiceActivator(inputChannel="requestChannel")
public byte[] echo(byte[] in,@SuppressWarnings("deprecation") @Header("ip_address") String ip){
byte[] rawbytes = gosDataSerivce.byteArrayToHex(in,ip);//Process bytes and returns result
return rawbytes;
}
}
}
@springboot应用程序
@集成组件扫描
公共类SpringIntegrationApplication扩展了SpringBootServletInitializer{
公共静态void main(字符串[]args)引发IOException{
ConfigurableApplicationContext ctx=SpringApplication.run(SpringIntegrationApplication.class,args);
System.in.read();
ctx.close();
}
@凌驾
受保护的SpringApplicationBuilder配置(SpringApplicationBuilder应用程序){
返回application.sources(SpringIntegrationApplication.class);
}
私有静态类applicationClass=SpringIntegrationApplication.Class;
@豆子
TcpNetServerConnectionFactory cf(){
TcpNetServerConnectionFactory connectionFactory=新的TcpNetServerConnectionFactory(8765);
返回连接工厂;
}
@豆子
TcpInboundGateway tcpGate(){
TcpInboundGateway网关=新的TcpInboundGateway();
gateway.setConnectionFactory(cf());
setRequestChannel(requestChannel());
返回网关;
}
@豆子
public MessageChannel requestChannel(){
返回新的DirectChannel();
}
@消息端点
公共类回声{
@ServiceActivator(inputChannel=“requestChannel”)
公共字节[]回显(在@SuppressWarnings(“弃用”)@头(“ip_地址”)字符串ip中的字节[]){
byte[]rawbytes=gosDataSerivce.bytearraytochex(in,ip);//处理字节并返回结果
返回原始字节;
}
}
}
不确定问题出在哪里,但您只需从echo()返回null
。在这种情况下,ServiceActivatingHandler
不关心并停止工作。仅仅因为requiresReply=false
另一方面,TcpInboundGateway
也不关心null
:
Message<?> reply = this.sendAndReceiveMessage(message);
if (reply == null) {
if (logger.isDebugEnabled()) {
logger.debug("null reply received for " + message + " nothing to send");
}
return false;
}
Message reply=this.send和receivemessage(Message);
if(reply==null){
if(logger.isDebugEnabled()){
logger.debug(“收到“+消息+”无需发送”的空回复”);
}
返回false;
}
这是可能的,因为MessagingTemplate
的replyTimeout
选项位于后台。默认值为1秒
。之后,sendAndReceiveMessage()
只会将null
返回给调用者
您可以在TcpInboundGateway
上调整此选项。如果没有异步切换,您可以安全地将应答超时设置为0-直到线程返回网关,时钟才会启动。您也可以使用网关来代替任意消息传递。我的用例是请求/应答、非阻塞且应具有多线程支持。这可以使用协作通道适配器实现吗