Spring Integration TcpInboundGateway发送条件回复

时间:2016-01-21 17:57:15

标签: spring-integration

我已将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;    

    }
    }

}

1 个答案:

答案 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上调整此选项。