栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Spring Boot中从Java设置TCPConnectionFactory或SSLServerSocketFactory

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Spring Boot中从Java设置TCPConnectionFactory或SSLServerSocketFactory

@SpringBootApplicationpublic 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("foorn".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")
在类上使用。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/369988.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号