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

发布于 2024-09-24 18:03:44 字数 537 浏览 3 评论 0原文

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

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

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

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

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

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

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.

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衬遮猫 2024-10-01 18:03:44

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

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

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

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>

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.

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

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.

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

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文