JVM如何能够对用户的请求做出响应。

huangapple go评论144阅读模式
英文:

How it is possible for JVM, to react on user's requests

问题

让我们以嵌入在某个Tomcat容器中的Spring应用程序为例。我知道Spring在其内部使用Servlets,但我想要理解它在JVM内部是如何工作的。有什么直接与JVM进行通信吗?

独立的“静态”应用程序情况很简单,因为代码只编译一次,字节码由JVM解释/由JIT编译,类被加载,且与外部世界没有交互 - 我们可以说应用程序在启动时“被调用一次”。

但是,当涉及到可能与用户交互的应用程序,例如在循环中运行,使用Spring等,JVM如何知道某些方法是通过请求例如“localhost:8080/users”被调用的?JVM在某个端口上监听吗?从套接字读取数据吗?哪个组件负责这一点?

我会非常感谢您能从应用程序开始的地方,用户请求,到与JVM的交互整个路径进行解释,并说明在其中发生了什么。

英文:

Let's take spring application that is embedded in some tomcat container for example. I know that spring uses servlets under it's mask but I want to understand how it works internally, from JVM point of view. What communicates directly with JVM?

Standalone "static" application case is simple because code is compiled once, bytecode is interpreted by JVM/compiled by JIT, class are loaded and there is no interaction with the outside world - we can say that application is "invoked once" when started.

But when it comes to application that can have some interaction with user and so on, running in loop, using i.e. spring, how JVM knows that some methods were invoked by requesting i.e. "localhost:8080/users"? JVM is listening on some port? Reading from socket? Which component is responsible for it?

I would be very grateful for writing the whole path from the start of the application, user request to the interaction with JVM and what is happening on it.

答案1

得分: 1

首先,你的问题非常广泛,不适合在StackOverflow上提问。

简而言之,JVM在这里与任何其他操作系统进程没有特殊之处。您(或您的供应商)编写了一个侦听在_socket_上的Java/C/Go/Python/任何其他代码。

当有传入的网络请求时,操作系统会“通知”该进程(您正在运行的程序),有新数据可供解释,以及以任何所需的方式做出反应(例如将用户数据保存到数据库中并返回适当的响应)。

用于与操作系统特定功能(如 sockets)交互的Java/JVM代码是用本地代码(即C/C++)编写的,并由JVM通过包装器(“本地方法”)提供给您的Java程序。
PlainSocketImpl.java 类为例,其中有一堆本地方法,这些方法在相应的 PlainSocketImpl.c 本地代码中实现:https://github.com/AdoptOpenJDK/openjdk-jdk14/blob/master/src/java.base/unix/native/libnet/PlainSocketImpl.c
(例如 socketAccept 实现:https://github.com/AdoptOpenJDK/openjdk-jdk14/blob/master/src/java.base/unix/native/libnet/PlainSocketImpl.c)

我建议您找一些关于一般 socket 编程的良好资源;您可能会发现以下资源有用:

英文:

First, your question is very broad and not suitable for StackOverflow.

In short, the JVM is no special from any other operating system process here. You (or your vendor) writes a Java/C/Go/Python/whatever code which listens on a socket.

When an incoming network request comes, the operating system "notifies" the process (your running program) that there are new data so it can interpret it and react in whatever way it wants (such as saving user data into a database and returning a proper response).

The Java/JVM code for interacting with operating system specific facilities like sockets are written in native code (that is C/C++) and provided by JVM to your Java program via wrappers ("native methods").
As an example there's an internal PlainSocketImpl.java class with a bunch of native methods implemented in the corresponding PlainSocketImpl.c native code: https://github.com/AdoptOpenJDK/openjdk-jdk14/blob/master/src/java.base/unix/native/libnet/PlainSocketImpl.c
(e.g. the socketAccept implementation: https://github.com/AdoptOpenJDK/openjdk-jdk14/blob/master/src/java.base/unix/native/libnet/PlainSocketImpl.c)

I suggest you find a good resource on general socket programming; you may find these useful:

huangapple
  • 本文由 发表于 2020年3月15日 22:50:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/60694147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定