如何在 PHP 和 Java 程序之间进行通信? [英] How can I communicate between PHP and a Java program?

查看:32
本文介绍了如何在 PHP 和 Java 程序之间进行通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个经常需要运行计算密集型查询的 Web 应用程序,查询的结果存储在单独的表中.使用 MySQL,这个查询大约需要 500 毫秒(尽可能优化,相信我).为了消除这个瓶颈,我创建了一个 Java 程序,将相关的 DB 数据加载到内存中并自行执行查询;大约需要 8 毫秒(我对此感到有点自豪).我想使用这个 Java 程序来获取结果,如果它失败或不可用,则故障转移到让 PHP 运行 MySQL 查询.

I'm working on a web application that frequently requires a calculation intense query to be run, the results of which are stored in a separate table. Using MySQL, this query takes about 500ms (as optimized as possible, believe me). To eliminate this bottleneck, I've created a Java program that loads the relevant DB data into memory and performs the query itself; it takes about 8ms (something I'm a little bit proud of). I'd like to use this Java program to get the results, and if it fails or is unavailable, failover to having PHP run a MySQL query.

由于将数据加载到 Java 应用程序需要一些时间,因此它将加载一次并作为后台进程继续运行.现在,问题是我如何通过 PHP 与这个 Java 应用程序通信?

Since loading the data into the Java application takes some time, it's going to load it once and remain running as a background process. Now, the question is how do I communicate with this Java application via PHP?

记住:

  • 多个 PHP 实例可能需要同时与此 Java 进程通信.
  • 如果找不到 Java 实例(例如:由于某种原因它崩溃了),PHP 应该使用较旧且速度较慢的 MySQL 方法.
  • 可以接受中间进程,例如 Memcache.
  • 理想情况下,该解决方案能够承受竞争条件.
  • 我最好不要使用 MySQL 作为中介.

我打算使用 Memcache,其中 PHP 将写入一个已知的键并轮询直到该键变为已完成",同时 Java 将轮询该键,一旦发现某些内容就执行该作业并将其设置为已完成".但是,由于两个原因,这不起作用.首先,PHP 和 Java 都使用序列化对象对 Memcache 进行读/写,并且无法改变这一点,而且我不希望 Java 对 PHP 对象进行反序列化,反之亦然——这太混乱了.其次,这不符合 ACID——如果队列建立起来就会出现竞争条件.

I was going to use Memcache, where PHP would write to a known key and poll until that key changed to "completed", meanwhile Java would poll that key and once it found something perform the job and set it to "completed". However, this wouldn't work for two reasons. First, both PHP and Java read/write to Memcache using serialized objects, and there's no way to change that, and I don't want Java to unserialize PHP objects and vice/versa -- it's too messy. Second, this is not ACID compliant -- if a queue built up there would be race conditions.

现在,我坚持轮询 MySQL选择"以查看作业是否不在队列中,这远非最佳解决方案,因为轮询时间需要更慢,因此 MySQL 无法获取ping 太频繁了.我需要一个更好的解决方案!

For now, I'm stuck with polling MySQL "selects" to see if a job is off the queue or not, which is far from an optimal solution because the poll time will need to be slower so MySQL doesn't get pinged too frequently. I need a better solution!

谢谢.

呃.看起来我将在 Java 中使用某种我不熟悉的 SocketServer.一个例子可能会有所帮助:)

推荐答案

我在 Java 端使用套接字服务器,在 PHP 端使用 PHP 套接字.效果很好.

I'm using socket server on the Java end, and PHP sockets on the PHP end. Works great.

无需使用 PHP/Java 桥使事情变得过于复杂,也无需创建 Web 服务器的开销.

There's no need to overcomplicate things with PHP/Java bridge, and no need for overhead of creating a web server.

套接字工作得很好,我什至有点惭愧我什至问这个问题才开始工作.

Sockets work great, and I'm actually a bit ashamed I even asked the question to begin work.

这篇关于如何在 PHP 和 Java 程序之间进行通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆