在 Web 服务器上运行 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/624749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running Java on a Web Server
提问by Vincent
I have written a standalone Java application that I've packaged into a jar file that takes in some command line arguments, does some hardcore computations, and then writes out the result to a file along with some output to the default output stream pointing to where the file with the results are.
我编写了一个独立的 Java 应用程序,我将它打包到一个 jar 文件中,该文件接收一些命令行参数,进行一些核心计算,然后将结果写出到一个文件中,同时将一些输出写到默认输出流中,指向哪里带有结果的文件是。
I now want to create a website around this technology. The idea is that the user can fill in an html form, post it to a webpage, which would then call the Java application, parse the results from the Java app, and display it to the user.
我现在想围绕这项技术创建一个网站。这个想法是,用户可以填写一个 html 表单,将其发布到网页上,然后该网页将调用 Java 应用程序,解析来自 Java 应用程序的结果,并将其显示给用户。
Currently, I am using a little bit of PHP to collect the data from the post request, and then just using an exec call:
目前,我正在使用一点 PHP 从 post 请求中收集数据,然后只使用 exec 调用:
java -jar -Xmx128m myapplication.jar command-line-arguments
这很糟糕吗?
I do have several thousand visits to my website each day and each execution of the Java application can take upwards of 30 seconds to a minute, so I don't want to be overly inefficient. It seems like there would be a better solution than having to call Java directly for every request.
我的网站每天有数千次访问,Java 应用程序的每次执行可能需要 30 秒到一分钟,所以我不想效率太低。似乎有比必须为每个请求直接调用 Java 更好的解决方案。
I keep hearing things like java servlets, beans, tomcat, glassfish, etc., but I don't understand what they are and how they would benefit me. What do these get me? Faster results because the Java JVM doesn't have to be created each time I run the application? Less memory usage? I obviously want it to run as fast as possible with as little memory footprint as possible.
我不断听到诸如 java servlet、bean、tomcat、glassfish 之类的东西,但我不明白它们是什么以及它们如何使我受益。这些对我有什么好处?因为不必每次运行应用程序时都创建 Java JVM,所以获得更快的结果?内存占用少?我显然希望它以尽可能少的内存占用尽可能快地运行。
So, what is the best approach that I can take here? I don't want to do any serious rewriting of my application as there is a lot of code (so rewriting it to C or C++ is out of the question).
Thanks.
那么,我在这里可以采取的最佳方法是什么?我不想对我的应用程序进行任何认真的重写,因为有很多代码(因此将其重写为 C 或 C++ 是不可能的)。
谢谢。
回答by John Ellinwood
Ok, servlets are smalish applications that are designed to run inside of a container. They provide an extension point for you to insert your java code into either a simple servlet container like tomcat, or a more fully featured application server like glassfish. You want to do this because the application server does the heavy lifting of dealing with the http interaction and provides other features like security, logging, session management, error handling, and more (see the servlet specification).
好的,servlet 是设计用于在容器内运行的小型应用程序。它们为您提供了一个扩展点,可以将您的 Java 代码插入到像 tomcat 这样的简单 servlet 容器,或者像 glassfish 这样功能更齐全的应用程序服务器中。您想这样做是因为应用程序服务器承担了处理 http 交互的繁重工作,并提供其他功能,如安全性、日志记录、会话管理、错误处理等(请参阅servlet 规范)。
When you make your application live within an application conatiner (web server with all those other extra features), you can also manage the lifecycle of your application better. You'll be able to start and stop the application without shutting down the web server, redeploy, start more instances, etc. Plus, when you come up with that great second application, its easy to drop it in right next to the first one. Or, you can cluster several machines together for easy redundancy and load balancing, features of the application server.
当您将应用程序置于应用程序容器(具有所有其他额外功能的 Web 服务器)中时,您还可以更好地管理应用程序的生命周期。您将能够启动和停止应用程序而无需关闭 Web 服务器、重新部署、启动更多实例等。此外,当您想出第二个应用程序时,很容易将它放在第一个应用程序旁边. 或者,您可以将多台机器集群在一起,以便轻松实现冗余和负载平衡,这是应用程序服务器的功能。
This is just a start, there are many more features, technologies, and frameworks out there to help you make container based applications. Servlet tutorial.
这只是一个开始,还有更多的功能、技术和框架可以帮助您制作基于容器的应用程序。小程序教程。
回答by Florin
Run your code inside a servlet container.
在 servlet 容器中运行您的代码。
Assuming that you need to keep your website in PHP and as you already have java installed on your machine, simply install a free servlet container (such as Apache Tomcat, or Jetty). Configure to run the servlet container on an unused port. (8080) is their default.
假设您需要将网站保留在 PHP 中,并且您的机器上已经安装了 java,只需安装一个免费的 servlet 容器(例如 Apache Tomcat 或 Jetty)。配置为在未使用的端口上运行 servlet 容器。(8080) 是他们的默认值。
These servlet containers are really java based webservers, just like Apache, however specialized in serving java code.
这些 servlet 容器实际上是基于 Java 的 Web 服务器,就像 Apache,但是专门用于服务 Java 代码。
The most obvious advantage of using a java webserver rather than a new java.exe invocation for each request, is that your java virtual machine (jvm) will always be "hot", up and running. Each new start of the java.exe (jvm) will give you those extra seconds of waste.
对每个请求使用 java 网络服务器而不是新的 java.exe 调用的最明显优势是,您的 java 虚拟机 (jvm) 将始终处于“热”状态,并且正在运行。java.exe (jvm) 的每次新启动都会给您带来额外的浪费。
The second advantage of using a servlet container, is that the container will enable your code to run in a new thread, inside the jvm, for each new request. You will have no problem providing your service to your thousands of users a day. Most likely, your machine will crash if you were to start hundreds of java instances rather than one.
使用 servlet 容器的第二个优点是,对于每个新请求,该容器将使您的代码在 jvm 内的新线程中运行。每天为成千上万的用户提供服务是没有问题的。如果您要启动数百个 Java 实例而不是一个,那么您的机器很可能会崩溃。
Place your code inside a servlet. It really is easy even for a newcomer. You will talk to the servlet via HTTP (doGet or doPost methods of the servlet). Pass the php request form to this servlet and have the servlet give you back whatever: a page, a json object, xml or plain text.
将您的代码放在 servlet 中。即使对于新人来说,这真的很容易。您将通过 HTTP(servlet 的 doGet 或 doPost 方法)与 servlet 对话。将 php 请求表单传递给这个 servlet,让 servlet 返回任何内容:页面、json 对象、xml 或纯文本。
回答by S.Lott
[Do these get me] "Faster results because the Java JVM doesn't have to be created each time I run the application?"
[这些对我有用吗]“更快的结果,因为不必每次运行应用程序时都创建 Java JVM?”
Yes.
是的。
And -- bonus -- you can replace PHP so your entire site is in a single language: Java.
而且——奖励——您可以替换 PHP,这样您的整个站点就使用一种语言:Java。
Further, you can consider revising your use cases so it isn't a painful 30-60 seconds in one shot, but perhaps a series of quicker steps executed interactively with the user.
此外,您可以考虑修改您的用例,这样一次拍摄就不会是 30-60 秒的痛苦,而是可能与用户交互地执行一系列更快的步骤。
回答by pgras
回答by Andy White
You probably don't want to invoke the java app directly from the website. Like you said, if the java process takes 30 seconds to run, your web server is going to get way bogged down, especially if your site is getting pounded.
您可能不想直接从网站调用 Java 应用程序。就像您说的那样,如果 Java 进程需要 30 秒才能运行,那么您的 Web 服务器将陷入困境,尤其是当您的网站受到冲击时。
You may want to look into web-services (and possibly a message queue) for dispatching back-end processing requests. The PHP page could call the web-service on the server, which could then put a processing request on a queue, or just kick off the java app in an asynchronous fashion. You don't want the HTTP request to wait for the java app to finish, because, while it's processing, the user will just have a hung browser, and the HTTP request might timeout.
您可能需要查看用于分派后端处理请求的 Web 服务(可能还有消息队列)。PHP 页面可以调用服务器上的 Web 服务,然后服务器可以将处理请求放入队列,或者以异步方式启动 Java 应用程序。您不希望 HTTP 请求等待 Java 应用程序完成,因为在处理过程中,用户只会有一个挂起的浏览器,并且 HTTP 请求可能会超时。
Once the java app finishes, it could update a database table, which the user could then access from the website.
Java 应用程序完成后,它可以更新数据库表,然后用户可以从网站访问该表。

