Exchange memory between Java and PHP?

Can someone provide me with links or snippets where the PHP process writes to memory and the Java process reads from shared memory?

Thanks for the great answers.

Edited question: Suppose I create shared memory in php like this

<?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 there is some method by which I can pass the value of $shm_id and then read from it in java.

+4
source share
3 answers

If you do not need to synchronize the interaction between Java and PHP, I would use memcached , membase, or some other type of memory key storage.

Another way, for a huge amount of data flow, use Unix named pipe (FIFO). This is a common method in IPC (Inter Process Communication). First create the channel as a regular file using the mkfifo command. Add some reasonable permissions. In PHP, open the channel with r+ as a regular file and write, finally close. On the Java side, you keep it open as a regular file and read FileInputStream continuously with a read / readline lock or non-blocking NIO.

Compared to SHM, you don’t have to play with JNI, shared memory synchronization, ring buffer implementation, locking and memory leaks. You get simple read / write and FIFO queues with minimal development costs.

You delete it as a regular file. Do not use random access or seek , as this is a real stream without history.

+8
source

Why aren't you using message queues? You cannot literally write to the JVM or share it with other processes.

To communicate between others, you can use Message Queue technology. You can have a message queue, and PHP can easily transfer data. The java application can read the queue, receive data and process accordingly.

+5
source

To expand on Abdel's answer, I would recommend RabbitMQ , which has Java and PHP clients in it.

+1
source

Source: https://habr.com/ru/post/1341937/


All Articles