本文介绍了如何在 Spring Boot 中从 Java 设置 TCPConnectionFactory 或 SSLServerSocketFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring Boot 的新手,但我的工作要求我使用 Spring Boot 实现一个小型 Web 服务.

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

Web 服务需要接受 SSL TCP 连接(外部系统将使用自定义协议 - 而不是 HTTP)连接到我的 Web 服务.另外,我想在后台任务(或多个后台任务)中处理这些连接.

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).

看了官方文档(http://docs.spring.io/spring-integration/reference/html/ip.html),我还是不明白(我把所有的 XML 放在哪里).当我在 SO 上询问该 XML 的放置位置时,我回答说这是一种非常古老的配置方法,不应再使用.

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 ?

推荐答案

@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();
    }

}

如果您更喜欢 Spring Integration 的 XML 配置,请将其添加到 Spring 配置 xml 文件并在类上使用 @ImportResource("my-context.xml").

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.

这篇关于如何在 Spring Boot 中从 Java 设置 TCPConnectionFactory 或 SSLServerSocketFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 02:28