How do I setup TCPConnectionFactory or SSLServerSo

2019-07-01 10:26发布

I am new to Spring Boot but have been requested by my job to implement a small web service using spring boot.

The web service needs to accept SSL TCP connections (an external system will connect to my web service using a custom protocol - NOT HTTP). Also, I would like to handle these connections in a background task (or multiple background tasks).

After looking at the official documentation (http://docs.spring.io/spring-integration/reference/html/ip.html), I still don't understand (where do I place all that XML). When I asked on SO about where to place that XML, I was answered that this is a very old method of configuration and should not be used anymore.

What would be the "up-to-date" way to do this ?

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-07-01 11:08
@SpringBootApplication
public class So43983296Application implements CommandLineRunner {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(So43983296Application.class, args);
        Thread.sleep(10_000);
        context.close();
    }

    @Autowired
    private DefaultTcpNetSSLSocketFactorySupport ssl;

    @Override
    public void run(String... args) throws Exception {
        Socket socket = ssl.getSocketFactory().createSocket("localhost", 1234);
        socket.getOutputStream().write("foo\r\n".getBytes());
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String result = br.readLine();
        System.out.println(result);
        br.close();
        socket.close();
    }

    @Bean
    public TcpNetServerConnectionFactory scf() {
        TcpNetServerConnectionFactory scf = new TcpNetServerConnectionFactory(1234);
        DefaultTcpNetSSLSocketFactorySupport tcpSocketFactorySupport = tcpSocketFactorySupport();
        scf.setTcpSocketFactorySupport(tcpSocketFactorySupport);
        // Add custom serializer/deserializer here; default is ByteArrayCrLfSerializer
        return scf;
    }

    @Bean
    public DefaultTcpNetSSLSocketFactorySupport tcpSocketFactorySupport() {
        TcpSSLContextSupport sslContextSupport = new DefaultTcpSSLContextSupport("classpath:test.ks",
                "classpath:test.truststore.ks", "secret", "secret");
        DefaultTcpNetSSLSocketFactorySupport tcpSocketFactorySupport =
                new DefaultTcpNetSSLSocketFactorySupport(sslContextSupport);
        return tcpSocketFactorySupport;
    }

    @Bean
    public TcpInboundGateway inGate() {
        TcpInboundGateway inGate = new TcpInboundGateway();
        inGate.setConnectionFactory(scf());
        inGate.setRequestChannelName("upperCase");
        return inGate;
    }

    @ServiceActivator(inputChannel = "upperCase")
    public String upCase(byte[] in) {
        return new String(in).toUpperCase();
    }

}

If you prefer XML configuration for Spring Integration, add it to a spring configuration xml file and use @ImportResource("my-context.xml") on the class.

查看更多
登录 后发表回答