4

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

Thanks for the wonderful answers.

Edited question : Suppose i create a 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 is there some method by which i can pass the value of $shm_id and then read from it in java.

2
  • What type of data is it? Binary or text? What life time? Is Java process a standalone application or web app? Commented Mar 2, 2011 at 9:28
  • its text data and data is being fetched continuously from a server. Java process is a standalone application. Commented Mar 2, 2011 at 10:22

3 Answers 3

8

If you don't need synchronized interaction between Java and PHP - I would use memcached, membase or some other type of memory key store.

Another way, for huge amount of data stream, is using Unix named pipe (FIFO). It is common way in IPC (Inter Process Communication). First create the pipe as normal file using mkfifo command. Add some reasonable access rights. In PHP open the pipe with r+ mode as normal file and write, finally close. On Java side you keep it open as normal file and read continuously by FileInputStream with blocking read/readline or nonblocking NIO.

In comparison to SHM, you don't have to play with JNI, shared memory synchronization, ring buffer implementations, locking and memory leaks. You get simple read/write and FIFO queue at lowest development cost.

You delete it as normal file. Don't use random access or seek as it is real stream without history.

Sign up to request clarification or add additional context in comments.

Comments

5

Why don't you use some message queues? You cannot literally write into memory of a JVM or share it with other processes.

In order to communicate between others you can make use of a Message Queue technology. You can have message queue running and PHP could easily transfer the data. The java application could read the queue, get the data and process accordingly.

5 Comments

actually i have a shared memory where my php code writes some live data. This data is actually huge. Now i want my java code to read this memory.
@ayush, as far as I understand, PHP APIs use POSIX apis to handle the shared memory. What you can do is have JNI routines and try accessing the shared memory via this. I think that is the only way to communicate if you insist on using shared memory
@ayush, how huge is the data? Message queues are mostly DB-backed and can handle pretty huge data. Doing your own JNI wrappers is only a good idea if you are paid by the hour and no one is counting.
... and not counting maintenance nightmare.
@Yar : data size is like receiving 1000 characters per second.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.