I need to pass messages to CLI PHP processes via stdin from Java. I'd like to keep about 20 PHP processes running in a pool, such that when I pass a message to the pool, it sends each message to a separate thread, keeping a queue of messages to be delivered. I'd like these PHP processes to stay alive as long as possible, bringing up a new one if one dies. I looked at doing this with a static thread pool, but it seems more designed for tasks that execute and simply die. How could I do this, with a simple interface to pass a message to the pool? Will I have to implement my own custom "thread pool"?
相关问题
- Views base64 encoded blob in HTML with PHP
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- Laravel Option Select - Default Issue
- How to maintain order of key-value in DataFrame sa
I am providing some code with this as I think it will make things clearer. Basically you need to keep an pool of process objects around. Be considerate that each of these processes has a input, output and error stream you need to manage in some way. In my example I just redirect the error and output to the main processes console. You can setup callbacks and handlers to obtain the output of the PHP program if needed. If you are just processing tasks and don't care what PHP says then leave it as is or redirect to a file.
I am using the Apache Commons Pool library for the ObjectPool. No need to reinvent one.
You'll have a pool of 20 processes that run your PHP program. This alone will not get you what you need. You might want to process tasks against all 20 of these processes "at the same time." So you'll also need a ThreadPool that will pull a Process from your ObjectPool.
You'll also need to understand that if you kill, or CTRL-C your Java process the
init
process will take over your php processes and they will just sit there. You'll probably want to keep a log of all the pid's of the PHP processes you spawn, and then clean them up if you re-run your Java program.Output of Program Run:
Code of C++ program to just output what was input:
Update
Sorry for the delay. Here is a JDK 6 version for anyone interested. You'll have to run a separate thread to read all the input from the InputStream of the process. I've set this code up to spawn a new thread along side each new process. That thread always read from the process as long as it is alive. Instead of outputting directly to a file I set it up such that it uses the Logging framework. That way you can setup a logging configuration to go to a file, roll over, go to console etc. without it being hard coded to go to a file.
You'll notice I only start a single Gobbler for each process even though a Process has stdout and stderr. I redirect stderr to stdout just to make things easier. Apparently jdk6 only supports this type of redirect.
Output
Your best bet here is use the pcntl functions to fork a process, but communication between processes is difficult. I would recommend creating a queue that your processes can read from, rather than trying to pass messages to the command line.
Beanstalk has several PHP clients that you could use to handle the messaging between processes.