简体   繁体   English

两个 Java 文件之间的通信

[英]Communication Between Two Java Files

I have a Minecraft server that is configured to change the color of my LED light strips based on what events occur in the game, however, this server only works when ran on my computer.我有一个 Minecraft 服务器,它配置为根据游戏中发生的事件更改我的 LED 灯条的颜色,但是,该服务器仅在我的计算机上运行时才有效。 This is because it requires the use of an Arduino to control the LED light strips.这是因为它需要使用 Arduino 来控制 LED 灯条。

I was wondering if there was any way to communicate through two different jar files so that, when an event happens in Minecraft, it just tells my computer through a whole different file.我想知道是否有任何方法可以通过两个不同的 jar 文件进行通信,这样,当 Minecraft 中发生事件时,它只会通过一个完全不同的文件告诉我的计算机。

For example: I use a Minecraft server hoster like Server.pro.例如:我使用像 Server.pro 这样的 Minecraft 服务器托管程序。 I detect whenever a player gets damage.每当玩家受到伤害时,我都会检测到。 When a player gets damaged I want to be able to notify my computer from the host.当播放器损坏时,我希望能够从主机通知我的计算机。 So like this:所以像这样:

onDamage() {
     Print_to_other_computer(“damaged”);
}

How could I do something like that?我怎么能做那样的事情? My problem is that I have no idea where to start in communication between these two files.我的问题是我不知道从哪里开始在这两个文件之间进行通信。 Maybe a database?也许是数据库?

There are plenty of options:有很多选择:

  1. Create an API on your computer's "jar file" (webservice) and call that API from your Minecraft application.在您计算机的“jar 文件”(Web 服务)上创建一个 API 并从您的 Minecraft 应用程序中调用该 API。 That interface can be REST, SOAP, RMI or any other modern technology.该接口可以是 REST、SOAP、RMI 或任何其他现代技术。 (REST is the best option) (REST 是最好的选择)
  2. Enable a queue between the two applications.在两个应用程序之间启用队列。 ActiveMQ for example.以 ActiveMQ 为例。 Then you have a publisher-consumer pattern.然后你就有了一个发布者-消费者模式。
  3. You can also use an event based framework like Axon Framework您还可以使用基于事件的框架,例如 Axon Framework
  4. You can also use a database between the applications.您还可以在应用程序之间使用数据库。
  5. ...... ……

Using a REST interface between the two applications is easiest and probably the best option.在两个应用程序之间使用 REST 接口是最简单的并且可能是最好的选择。 The best way to do that is to specify an open api specification (YAML file) and then generate the server and client side.最好的方法是指定一个开放的 api 规范(YAML 文件),然后生成服务器端和客户端。

https://www.baeldung.com/java-openapi-generator-server https://www.baeldung.com/java-openapi-generator-server

https://blog.mimacom.com/using-the-openapi-generator-for-spring-boot/ https://blog.mimacom.com/using-the-openapi-generator-for-spring-boot/

The question is really very broad.这个问题真的很广泛。 And with this answer, I'll just try to focus a bit on concepts and overview.有了这个答案,我将尝试将重点放在概念和概述上。 It is too hard to go in debt because there are too many options. go 负债太难了,因为有太多的选择。

Files vs Processes文件与进程

Files consist of bytes.文件由字节组成。 They can be used to encode many different kinds of content.它们可用于对许多不同类型的内容进行编码。 Bytes can encode text, music, video, logic, ...字节可以编码文本、音乐、视频、逻辑……

A text file that can contain executable logic written in a programming language.可以包含用编程语言编写的可执行逻辑的文本文件。 In most cases, you would need to compile those text files first, to create a 'binary' or 'executable' which can then be executed by the operating system.在大多数情况下,您需要先编译这些文本文件,以创建一个“二进制”或“可执行文件”,然后操作系统可以执行这些文件。

A program or application consists of binaries.程序或应用程序由二进制文件组成。 Binaries can be executed.可以执行二进制文件。 A running instance of a binary/executable is called a 'process'.二进制/可执行文件的运行实例称为“进程”。

Binaries that perform background tasks are often called "services".执行后台任务的二进制文件通常称为“服务”。 Services are often scheduled to run when the operating system starts/stops.服务通常安排在操作系统启动/停止时运行。

Protocols协议

When a process starts, it can read and write data to a terminal.当一个进程启动时,它可以读写数据到终端。 It does so using 3 different streams.它使用 3 个不同的流来做到这一点。 A stdin, stdout, stderr.标准输入、标准输出、标准错误。 This is the most basic way for applications to communicate to a user.这是应用程序与用户通信的最基本方式。 And it can also be used to communicate to other applications.它还可以用于与其他应用程序通信。

In a similar way, you can also use streams to communicate through other channels.以类似的方式,您也可以使用流通过其他渠道进行通信。

When different applications communicate to each other they need to do so in a structured way by exchanging messages.当不同的应用程序相互通信时,它们需要通过交换消息以结构化的方式进行。 The structure needs to respect a so-called "communication protocol".该结构需要尊重所谓的“通信协议”。 Sometimes protocols depend on other protocols.有时协议依赖于其他协议。

On a higher level, applications communicating with each other, often want to provide a set of commands, or want to share a set of functions.在更高的层次上,相互通信的应用程序通常想要提供一组命令,或者想要共享一组功能。 Remotely executing code is often called "RPC" (remote procedure calls).远程执行代码通常称为“RPC”(远程过程调用)。 A set of those shared functions are often called an "API".一组这些共享功能通常称为“API”。

Often the provided functions can be categorized in 4 categories: create/read/update/delete actions, abbreviated as "CRUD".通常提供的功能可以分为 4 类:创建/读取/更新/删除操作,缩写为“CRUD”。

Network communication网络通讯

When processes are located on different computers, they need to communicate over an internal (eg an office network) or public network (eg the internet)当进程位于不同的计算机上时,它们需要通过内部(例如办公室网络)或公共网络(例如 Internet)进行通信

Most networks today use the ethernet protocol (ie RJ45 cables or wifi), and on top of that, they use IP addresses to unique identify each computer.今天的大多数网络都使用以太网协议(即 RJ45 电缆或 wifi),最重要的是,它们使用 IP 地址来唯一标识每台计算机。 The communication is usually done using TCP (which is a kind of negotiation where one end listens for an incoming connection on a pre-determined port, the other connects to it. Then both sides can send/receive messages).通信通常使用 TCP 完成(这是一种协商,一端侦听预定端口上的传入连接,另一端连接到该端口。然后双方可以发送/接收消息)。 Together this is called "TCP/IP".这一起被称为“TCP/IP”。 Most modern communication protocols are based on TCP/IP (although there are other popular ones).大多数现代通信协议都基于 TCP/IP(尽管还有其他流行的协议)。

TCP/IP describes how devices create connections, and how they send bytes to each other but does not dictate how those bytes are organized. TCP/IP 描述了设备如何创建连接,以及它们如何相互发送字节,但没有规定这些字节是如何组织的。 The content itself also needs a protocol.内容本身也需要协议。 And historically, there have been many, some are encoded and some are text-based.从历史上看,有很多,有些是编码的,有些是基于文本的。

Webbrowsers use the HTTP protocol for their communication.网络浏览器使用 HTTP 协议进行通信。 That in itself is a protocol for communication between 2 applications.这本身就是两个应用程序之间通信的协议。 ie a webbrowser and a webserver.即一个网络浏览器和一个网络服务器。 However, the HTTP protocol can also be a foundation for other communication protocols, such as REST.但是,HTTP 协议也可以作为其他通信协议的基础,例如 REST。

REST is a webservice protocol for communication between different applications. REST 是一个用于不同应用程序之间通信的 Web 服务协议。 And probably the most popular one today.并且可能是当今最受欢迎的一款。

Realtime communication实时通讯

In many cases it matters how communication is triggered, which events cause the communication to start.在许多情况下,如何触发通信以及哪些事件导致通信开始很重要。

eg in a protocol like REST there is a client and a server.例如,在像 REST 这样的协议中,有一个客户端和一个服务器。 The server never contacts the client first.服务器从不先联系客户端。 It is the client who initiates the communication.发起通信的是客户端。

Some protocols keep an active connection between a client and a server and allow data to be pushed in both directions.一些协议在客户端和服务器之间保持活动连接,并允许双向推送数据。

Sometimes a message requires an immediate response.有时,一条消息需要立即响应。 By contrast, on other occasions requests are stored in a "queue" and processed at a later stage.相比之下,在其他情况下,请求存储在“队列”中并在稍后阶段进行处理。 (sync vs async communication). (同步与异步通信)。

Some prefer to communicate through a 3rd party which acts as that buffer, a so called "messaging bus".有些人更喜欢通过充当缓冲区的第 3 方进行通信,即所谓的“消息总线”。 (see: kafka, rabbitMQ, JMS for java, ...). (参见:kafka、rabbitMQ、java 的 JMS,...)。 Those technologies are better suited for async communication, and less practical for sync communication.这些技术更适合异步通信,而对于同步通信不太实用。 They are great for loosely coupling systems.它们非常适合松耦合系统。

Conclusion结论

  • Files don't communicate, processes or services do.文件不通信,进程或服务可以。

  • The choice of a protocol is a hard one because there are many options.协议的选择很困难,因为有很多选择。

  • There are no one-fits-all solutions.没有万能的解决方案。

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

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