简体   繁体   English

java.net与java.nio

[英]java.net versus java.nio

At what point is it better to switch from java.net to java.nio? 在什么时候从java.net切换到java.nio更好? .net (not the Microsoft entity) is easier to understand and more familiar, while nio is scalable, and comes with some extra nifty features. .net(不是Microsoft实体)更容易理解和更熟悉,而nio是可扩展的,并带有一些额外的漂亮功能。

Specifically, I need to make a choice for this situation: We have one control center managing hardware at several remote sites (each site with one computer managing multiple hardware units (a transceiver, TNC, and rotator)). 具体来说,我需要为这种情况做出选择:我们有一个控制中心在几个远程站点管理硬件(每个站点有一台计算机管理多个硬件单元(收发器,TNC和旋转器))。 My idea was to have write a sever app on each machine that acts as a gateway from the control center to the radio hardware, with one socket for each unit. 我的想法是在每台机器上编写一个服务器应用程序,作为从控制中心到无线电硬件的网关,每个单元有一个插槽。 From my understanding, NIO is meant for one server, many clients, but what I'm thinking of is one client, many servers. 根据我的理解,NIO适用于一个服务器,许多客户端,但我想的是一个客户端,许多服务器。

I suppose a third option is to use MINA, but I'm not sure if that's throwing too much at a simple problem. 我想第三个选择是使用MINA,但我不确定这是否会对一个简单的问题投入太多。


Each remote server will have up to 8 connections, all from the same client (to control all the hardware, and separate TX/RX sockets). 每个远程服务器最多有8个连接,全部来自同一个客户端(用于控制所有硬件和单独的TX / RX套接字)。 The single client will want to connect to several servers at once, though. 但是,单个客户端将希望同时连接到多个服务器。 Instead of putting each server on different ports, is it possible to use channel selectors on the client side, or is it better to go multi-threaded io on the client side of things and configure the servers differently? 不是将每个服务器放在不同的端口上,是否可以在客户端使用通道选择器,或者更好的是在客户端进行多线程io并以不同方式配置服务器?


Actually, since the remote machines serve only to interact with other hardware, would RMI or IDL/CORBA be a better solution? 实际上,由于远程机器仅用于与其他硬件交互,RMI或IDL / CORBA是否是更好的解决方案? Really, I just want to be able to send commands and receive telemetry from the hardware, and not have to make up some application layer protocol to do it. 真的,我只是希望能够从硬件发送命令和接收遥测,而不必编写一些应用程序层协议来完成它。

Avoid NIO unless you have a good reason to use it. 除非您有充分的理由使用它,否则请避免使用NIO。 It's not much fun and may not be as beneficial as you would think . 这并不是很有趣,可能没有您想象的那么有益 You may get better scalability once you are dealing with tens of thousands of connections, but at lower numbers you'll probably get better throughput with blocking IO. 处理成千上万的连接后,您可以获得更好的可扩展性,但是数量越少,阻塞IO的吞吐量就越高。 As always though, make your own measurements before committing to something you might regret. 一如既往,在做出你可能会后悔的事情之前,先进行自己的测量。

Something else to consider is that if you want to use SSL, NIO makes it extremely painful. 还有一点要考虑的是,如果你想使用SSL,NIO会让它非常痛苦。

Scalability will probably drive your choice of package. 可扩展性可能会推动您选择的包。 java.net will require one thread per socket. java.net每个socket需要一个线程。 Coding it will be significantly easier. 编码将更加容易。 java.nio is much more efficient, but can be hairy to code around. java.nio效率更高,但可以编写代码。

I would ask yourself how many connections you expect to be handling. 我会问你自己希望处理多少个连接。 If it's relatively few (say, < 100), I'd go with java.net. 如果它相对较少(比方说,<100),我会选择java.net。

There is almost no reason to write this kind of networking code from scratch now. 现在几乎没有理由从头开始编写这种网络代码。 Packages like netty.io will almost always get you more reliable and flexible code with fewer lines of code than a hand-crafted solution will. netty.io这样的软件包几乎总能让您获得更可靠,更灵活的代码,而且代码行数比手工制作的解决方案更少。 Also, with Netty, you can get SSL support w/o complicating your implementation at all. 此外,使用Netty,您可以获得SSL支持,而不会使您的实现复杂化。 Libraries like netty also obviate the "async vs threads" question almost entirely, gives you good performance, and still lets you tweak the threading model as needed. 像netty这样的库几乎完全消除了“异步vs线程”问题,为您提供了良好的性能,并且仍然允许您根据需要调整线程模型。

The number of connections you're talking about tells me you should use java.net. 您正在谈论的连接数告诉我您应该使用java.net。 Really, there's no reason to complexify your task with non-blocking I/O. 实际上,没有理由将您的任务与非阻塞I / O进行复杂化。 (Unless your remote systems are underpowered, but then why are you using Java on them?) (除非您的远程系统功能不足,但为什么要在它们上使用Java?)

Take a look at Apache's XML-RPC package. 看看Apache的XML-RPC包。 It's easy to use, completely hides the network stuff from you, and works over good ol' HTTP. 它易于使用,完全隐藏了您的网络内容,并且可以在良好的HTTP上运行。 No need to worry about protocol issues ... it'll all look like method calls to you, on both ends. 无需担心协议问题......它们看起来都像是在两端的方法调用。

Given the small number of connections involved, java.net sounds like the right solution. 鉴于涉及的连接数量很少,java.net听起来像是正确的解决方案。

Other posters talked about using XML-RPC. 其他海报讨论了使用XML-RPC。 This is a good choice if the volumes of data being shipped are small, however I have had bad experiences with XML-based protocols when writing inter-process communications that ship large amounts of data (eg large request/responses, or frequent small amounts of data). 如果发送的数据量很小,这是一个不错的选择,但是在编写发送大量数据的进程间通信时(例如大量请求/响应,或频繁的少量数据),我在使用基于XML的协议时遇到了不好的经验数据)。 The cost of XML parsing is typically orders of magnitude higher than more optimised wire formats (eg ASN.1). XML解析的成本通常比更优化的有线格式(例如ASN.1)高几个数量级。

For low volume control applications the simplicity of XML-RPC should outweigh the performance costs. 对于低容量控制应用程序,XML-RPC的简单性应该超过性能成本。 For high volume data communications it may be better to use a more efficient wire protocol. 对于大容量数据通信,使用更有效的线路协议可能更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM