在 SPRING 服务器中使用 JAVA NIO 框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3758970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:16:28  来源:igfitidea点击:

Using JAVA NIO framework in SPRING server

javaspringnettynioapache-mina

提问by Clark Ku

I'm implementing an hybrid server application that mixes a Web-Servlet and a plain Java application.

我正在实现一个混合服务器应用程序,它混合了一个 Web-Servlet 和一个普通的 Java 应用程序。

The java application manages thousands of sockets for remote devices, while the Web application interacts with the user to set/read the state of any socket. Java NIO, or Apache-MINA vs Jboss-Netty, seems to be good options for the sockets application.

Java 应用程序为远程设备管理数千个套接字,而 Web 应用程序与用户交互以设置/读取任何套接字的状态。Java NIO 或Apache-MINA 与 Jboss-Netty似乎是套接字应用程序的不错选择。

The first question is, can I run both applications (Servlet with web interface + JAVA NIO application) in the same server?I'am using now Tomcat for the Servlet and a plain procrun daemon for the socket-application

第一个问题是,我可以在同一台服务器上运行两个应用程序(带有 Web 界面的 Servlet + JAVA NIO 应用程序)吗?我现在为 Servlet 使用 Tomcat,为套接字应用程序使用一个普通的 procrun 守护进程

I don't know if Spring is suitable for this combination, since I haven't seen any information about using NIO in Spring.

我不知道Spring是否适合这种组合,因为我没有看到任何关于在Spring中使用NIO的信息。

The second question is, how can both applications communicate between them?For the moment I'am using RMI but I wonder if there is a better solution.

第二个问题是,两个应用程序如何在它们之间进行通信?目前我正在使用 RMI,但我想知道是否有更好的解决方案。

回答by Abe

You can definitely run a NIO socket server and a web server in the same jvm using Spring. I have done it using Grails (which is a wrapper over spring anyway). I start the tcp server in the bootstrap class of Grails.

您绝对可以使用 Spring 在同一个 jvm 中运行 NIO 套接字服务器和 Web 服务器。我已经使用 Grails 完成了它(无论如何它都是 spring 的包装器)。我在 Grails 的引导类中启动了 tcp 服务器。

For normal spring web app, you can startthe socket server listening at a particular port, say 8090 and the web server at say 8080 using spring framework lifecycle listeners or post processors.

对于普通的 Spring Web 应用程序,您可以使用 Spring 框架生命周期侦听器或后处理器在特定端口(例如 8090)和 Web 服务器(例如 8080)上启动侦听端口服务器。

Make your socket server to be a spring bean and use the init-method as shown below to actually start the socket server. Ths spring framework will automatically call it on bean instantiation.

将您的套接字服务器设为 spring bean,并使用如下所示的 init 方法实际启动套接字服务器。这个 spring 框架会在 bean 实例化时自动调用它。

An example configuration for a netty server could be like below:

netty 服务器的示例配置可能如下所示:

<bean id="tcpServer" class="netty.NettyTCPServer"
        init-method="createServerBootstrap" destroy-method="stopServer">
        <property name="pipelineFactory" ref="pipelineFactory"></property>
</bean>

<bean id="pipelineFactory" class="netty.HandshakePipelineFactory">
        <lookup-method name="createHandshakeHandler" bean="handshakeHandler" />
        <property name="stringDecoder" ref="stringDecoder"></property>
        <property name="stringEncoder" ref="stringEncoder"></property>
        <property name="nulEncoder" ref="nulEncoder"></property>
        <property name="frameSize" value="256"></property>
</bean>