简体   繁体   English

在Java和PHP之间共享内存?

[英]Sharing memory between Java and PHP?

Can someone provide me links or snippets where a PHP process writes to memory and a Java process reads from the shared memory? 有人能为我提供链接或片段,其中PHP进程写入内存并且Java进程从共享内存中读取吗?

Thanks for the wonderful answers. 谢谢你的精彩答案。

Edited question : Suppose i create a shared memory in php like this 编辑问题:假设我在这样的PHP中创建一个共享内存

<?php
$shm_key = ftok(__FILE__, 't');
$shm_id = shmop_open($shm_key, "c", 0644, 100);
$shm_bytes_written = shmop_write($shm_id, $my_string, 0);
?>

Now is there some method by which i can pass the value of $shm_id and then read from it in java. 现在有一些方法可以传递$shm_id的值,然后在java中读取它。

If you don't need synchronized interaction between Java and PHP - I would use memcached , membase or some other type of memory key store. 如果你不需要Java和PHP之间的同步交互 - 我会使用memcachedmembase或其他类型的内存密钥库。

Another way, for huge amount of data stream, is using Unix named pipe (FIFO). 对于大量数据流,另一种方法是使用Unix命名管道(FIFO)。 It is common way in IPC (Inter Process Communication). 这是IPC(进程间通信)的常用方法。 First create the pipe as normal file using mkfifo command. 首先使用mkfifo命令将管道创建为普通文件。 Add some reasonable access rights. 添加一些合理的访问权限。 In PHP open the pipe with r+ mode as normal file and write, finally close. 在PHP中打开管道,使用r+模式作为普通文件并写入,最后关闭。 On Java side you keep it open as normal file and read continuously by FileInputStream with blocking read / readline or nonblocking NIO. 在Java方面,你把它打开正常文件,并通过连续读取FileInputStream与阻塞read / readline或非阻塞的NIO。

In comparison to SHM, you don't have to play with JNI, shared memory synchronization, ring buffer implementations, locking and memory leaks. 与SHM相比,您不必使用JNI,共享内存同步,环形缓冲区实现,锁定和内存泄漏。 You get simple read/write and FIFO queue at lowest development cost. 您可以以最低的开发成本获得简单的读/写和FIFO队列。

You delete it as normal file. 您将其删除为普通文件。 Don't use random access or seek as it is real stream without history. 不要使用随机访问或seek因为它是没有历史记录的真实流。

Why don't you use some message queues? 你为什么不使用一些消息队列? You cannot literally write into memory of a JVM or share it with other processes. 您无法直接写入JVM的内存或与其他进程共享它。

In order to communicate between others you can make use of a Message Queue technology. 为了在其他人之间进行通信,您可以使用Message Queue技术。 You can have message queue running and PHP could easily transfer the data. 您可以运行消息队列,PHP可以轻松地传输数据。 The java application could read the queue, get the data and process accordingly. java应用程序可以读取队列,获取数据并进行相应的处理。

为了扩展Abdel的答案,我推荐RabbitMQ ,它有Java和PHP客户端。

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

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