客户端/服务器通信的 Java 标准
用于客户端/服务器或 P2P 通信的“官方”Java API 是什么? Java RMI?其他一些网络API?
这个官方网络 API 是 SE 和 EE 两者的标准吗?
我确信答案是特定于上下文的,所以让我们看几个实例:
- 您在 2 台机器上安装了 2 个 swing 客户端并连接到同一网络(或 Internet),并且您希望其中任何一个能够向对方发送一个原语,例如整数 4,或一些 POJO,例如“Widget”对象
- 与上面的 #1 相同,但在 Swing 客户端和完全兼容的 Java EE 后端之间(实现托管 bean、应用程序服务器,整个九码)
我没有想到具体的应用程序,我只是想知道Java世界中客户端-客户端和客户端-服务器通信的“规范”是什么。
What is the "official" Java API for client/server or P2P communication? Java RMI? Some other networking API??
Is this official networking API the standard for both SE and EE?
I'm sure the answer is very context-specific, so let's take a look at a few instances:
- You have 2 swing clients installed on 2 machines and connected to the same network (or the Internet), and you want either of them to send the other a primitive, such as the integer 4, or some POJO, like a "Widget" object
- Same as #1 above, but between a Swing client and a fully-compliant Java EE back-end (implementing managed beans, app servers, the whole nine yards)
I don't have a specific application in mind, I'm just wondering what are the "norms" for client-client and client-server communication in the world of Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果受 Java 束缚不是问题,那么当涉及到客户端和服务器解决方案“交换”数据时(尤其是当数据是 Java 类时,这可能很难/需要太多精力来表示为文本),RMI 是一个非常抽象的解决方案数据)。只需确保您的对象实现了 Serialized,并且几乎任何内容都可以通过线路传输。
如果这不符合你的要求,并且你想放弃原始的网络内容,客户端-服务器套接字框架 Netty< /a> 是一个不错的选择。
If being bound by Java isn't a problem, RMI is a pretty abstracted solution when it comes to the client and server solution "exchanging" data (especially when the data is Java classes which might be difficult/too much effort to represent as textual data). Just make sure your object implements Serializable and almost anything can be transmitted over the wire.
If this doesn't fit your bill and you want to drop down the raw networking stuff, the client-server socket framework Netty is a pretty good choice.
J2SE 中不存在最官方的网络 API,所有 J2SE API 都是官方的,因为它们都受到 Sun(现在的 Oracle)的支持。
也就是说,您应该根据以下标准选择 API:
例如,在两个客户端之间,简单的基于文本的协议足以传递 POJO,例如使用 Apache MINA 或 Google 协议缓冲区。
这也适用于客户端和服务器之间。
对 Zac 在评论中提出的问题的回应:
There's no such thing as the most official networking API in J2SE, all J2SE APIs are official in the sense they are supported by Sun (now Oracle).
That said, you should choose your API based on following criteria:
For example, between two clients simple text-based protocol will suffice for passing POJOs, for example using Apache MINA or Google protocol buffers.
This will work between client and server as well.
Response to Zac's questions in comment:
对于 P2P,Sun 一度非常努力地推动 JXTA。
我不敢使用RMI进行P2P通信。
For P2P, Sun at one point pushed JXTA pretty hard.
I wouldn't dare to use RMI for P2P communication.
rmi 几乎是标准的 java 到 java 协议。它是内置的并且使用起来非常简单。大多数 j2ee 后端也使用 RMI 进行通信,尽管这不是唯一的可能性。
rmi is pretty much the standard java to java protocol. it's built in and very simple to use. most j2ee backends also communicate using rmi, although that's not the only possibility.
J2SE 最常见的可能是 RMI 或原始套接字。
J2EE 使用每个人(服务器和客户端)都订阅的消息传递总线,这与 RMI 风格的解决方案有很大不同(尽管在最低级别的实现可能仍然依赖于 RMI)。它有助于自动化冗余和故障转移。如果你需要这个功能,我相信它也可以在 SE 中使用。
我已经有一段时间没有使用 J2EE 了,所以这可能已经改变了,但我对此表示怀疑。消息传递系统是J2EE 的核心组件。
J2SE the most common is probably RMI or raw sockets.
J2EE uses a messaging bus that everyone (servers and clients) subscribes to which is quite different from rmi style solutions (although at the lowest level an implementation may still rely on RMI). It helps automate redundancy and failover. If you need this functionality I believe it can be used in SE as well.
I haven't used J2EE for quite a while now, so this may have changed, but I doubt it. The messaging system was a core component of J2EE.