我已将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;
}
}
}
答案 0 :(得分:2)
不确定您的问题在哪里,但您只需从null
返回echo()
即可。在这种情况下,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;
}
这是可能的,因为后台replyTimeout
的{{1}}选项。默认情况下为MessagingTemplate
。之后,1 sec
只会将sendAndReceiveMessage()
返回给来电者。
您可以在null
上调整此选项。