Domanda

I'm using proc_open in php to call java application, send a large text to it for processing and capture a returned result. Is it possible to pass several text strings (input streams) instead of just one?

This is what I've got at the moment:

fwrite($pipes[0], $input);
fclose($pipes[0]);

$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);

If I do something like this, java still recognizes it as one input stream:

fwrite($pipes[0], $input);
fwrite($pipes[0], $input1);
fwrite($pipes[0], $input2);
fclose($pipes[0]);

$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);

So is something like this possible at all? If not, any alternatives? I can't use command line params because it's a large text with multiple lines.

È stato utile?

Soluzione

It depends what you are trying to do, and what the java application expects.

If you want the Java application to see the concatenation of $input, $input2 and $input3, then sure ... your code will do that.

If you want the Java to be able to automatically see those inputs as distinct streams, then no. As far as the Java IO system is concerned, the bytes are just bytes. There are no natural boundaries ... apart from the ultimate end of the (combined) stream.

If you want the Java to see one stream that it can then split into three streams, then it is possible, but it will require some programming effort.

  • On the PHP side, you have to add some kind of "framing" information to the stream that tells the Java side where one "stream" ends and the next one starts.

  • On the Java side, you have to look for / interpret that framing information.

The framing could be done by sending a byte count for each stream followed by the bytes, or it could be done with marker characters or sequences that designate the end of a stream.

Altri suggerimenti

Nope, a process has only a single standard input stream, as well as a single standard output stream and a single standard error (output) stream (this is true for every process not just java or php).

  • You can set up some socket communication, e.g. a client-server architecture, that would allow for multiple streams, but would only help if both client (php) and server (java) can do multi threading.
  • You can send through the pipe some delimiter sequence, so java can distinguish the three input strings
  • You can simply use more than one proc_open

EDIT:

  • You can use files instead of the stdin and stdout (php and java could share those)
  • You can use unix pipes (similar to the socket solution), but this is pretty hard to implement.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top