简体   繁体   English

运行Java应用程序和PHP

[英]Running Java application & PHP

I have a java program that has a healthy Java API, but I want build a primitive interface between my java application and a php script as those are the requirements of my project. 我有一个具有健康Java API的java程序,但我希望在我的java应用程序和php脚本之间构建一个原始接口,因为这些是我项目的要求。

My first attempt was to write a PHP script that ran an passthru function to run the jar. 我的第一次尝试是编写一个运行passthru函数的PHP脚本来运行jar。 ie

passthru("java -jar myjarfile param1 param2 param3")

This worked but proved to be quite slow because the jar file had to be launched and executed etc. 这有效,但事实证明它很慢,因为jar文件必须启动和执行等。

My next attempt was to create a servlet on Tomcat7 and interface it with PHP by usin the curl() command. 我的下一个尝试是在Tomcat7上创建一个servlet,并通过curl()命令与PHP连接。 ie

curl(http://myserver/mywebapp/myservlet?p1=param1&p2=param2&p3=param3);

This had excellent performance , but the servlet was very unstable and crashed after about 5 minutes (i was loading the server with about 1 request every 10 seconds) 这有很好的性能,但servlet非常不稳定并且在大约5分钟后崩溃(我每10秒钟加载服务器大约1个请求)

I come to Stack Overflow asking: am i doing this right? 我来Stack Overflow问:我这样做对吗? Is there a better way? 有没有更好的办法? How can I have my java program running in a jvm and interact with it using PHP? 如何让我的java程序在jvm中运行并使用PHP与它交互?

Thanks 谢谢

There is a world of difference between the Java method of handling things and the PHP method of handling things. 处理事物的Java方法和处理事物的PHP方法之间存在着天壤之别。

PHP basically runs every script from beginning to end for each request, which amounts to a very imperative programming technique. PHP基本上为每个请求从头到尾运行每个脚本,这相当于一种非常重要的编程技术。 Java, on the other hand, typically handles stuff by modules that remain in memory for many more than one request. 另一方面,Java通常通过模块处理内容,这些模块保留在内存中以用于多个请求。 To integrate the two, you need to consider more than the "function calls", you need to consider how those two environments can be meshed cleanly. 要集成这两者,您需要考虑的不仅仅是“函数调用”,您需要考虑如何将这两个环境干净地进行网格划分。

Launching the java per PHP request is asking Java to behave like PHP. 每个PHP请求启动java就要求Java像PHP一样运行。 In other words, you are going to discard most of the best reasons to use Java by making it work like PHP. 换句话说,通过使其像PHP一样工作,您将放弃使用Java的大多数最佳理由。 Instead, consider setting up a Tomcat (or something similar) instance and passing a request from one to the other. 相反,请考虑设置Tomcat(或类似的)实例并将请求从一个传递到另一个。 In other words, have the PHP make a web request to a Java environment, which handles things without complete buildup and teardown of the Java interpreter (which is how PHP handles things). 换句话说,让PHP向Java环境发出Web请求,该环境处理事务而没有完全构建和拆除Java解释器(这是PHP处理事务的方式)。

I'm assuming that because you attempted to use a JAR you can have the PHP and Java on the same machine. 我假设因为你试图使用JAR,你可以在同一台机器上安装PHP和Java。 You may find this document on Java integration in PHP quite exciting. 您可能会发现这篇关于PHP中Java集成的文档非常令人兴奋。 Note that I have never used it, I only know it exists. 请注意,我从未使用它,我只知道它存在。 Be sure to read the introduction. 一定要阅读介绍。

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

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