3

Is it possible to somehow use some of the PHP classes that come with Zend in a Java project? I'd like to think that there's always a way, but how?

Charles
  • 50,943
  • 13
  • 104
  • 142
jblue
  • 4,390
  • 4
  • 46
  • 79
  • @Alfred, Haven't tried yet actually. Did you try it yourself? – jblue Sep 17 '10 at 23:58
  • For fun I created a simple little message queue in java which you could use. I did similiar stuff like this in the past on top redis. It works pretty good! I also did write a lot of simple webservices which could also work and is really easy to write! I guess you should start with writing a little simple webservice because that is really easy! – Alfred Sep 18 '10 at 00:16

2 Answers2

5

You could try using Quercus, a Java based PHP runtime. Other than that, you could write a web service in PHP, which is called by your Java code, or a command-line PHP script which is executed by Java.

Jani Hartikainen
  • 42,745
  • 10
  • 68
  • 86
  • yup I also Quercus is what I was also was thinking of as option. – Alfred Sep 13 '10 at 21:42
  • +1 Thanks. but what is Quercus exactly? It sounds like a Java/PHP hybrid. Can it be added to Apache/PHP when the base is PHP or only when the base is Java. It's a bit of a strange concept for me. – jblue Sep 13 '10 at 22:00
  • +1 -- never heard of it before, will file away for reference. – Jason S Sep 13 '10 at 22:52
  • @jblue Quercus http://www.caucho.com/resin-3.0/quercus/ is an implementation of PHP, but actually written in Java – Mark Baker Sep 14 '10 at 10:50
3

Question you should ask yourself first

First some questions you should ask your self, which could help use give you the best answer(because programming is a lot of times about trade-offs):

  • Can you run PHP code natively: If it is not possible to run PHP code natively via (http/CLI) then your only option would be to try if Quercus does the job.

  • How much concurrency does PHP have: Let's assume you don't call PHP much. Then I would consider writing a simple webservice(see below)because this is the easiest to implement. If not a PHP deamon which is running in the background waiting for work(PHP) to process which it receives from java message queue(deamon), and sends the message to queue.

  • How quickly should PHP respond to java: If quick then a daemon would be the way to go.

But I would first advice try to implement the easiest/quickest solution and benchmark it. The quickest solution could be written in a couple of minutes. The more difficult could take some time.

Solutions:

  • Quercus: If you can't run PHP natively(CLI/HTTP) then this is your only option.
  • Simple webservice: let's say you call http://localhost:8181/zend/doZend.php?a=a&b=b from within your Java program. This would call:

    1. doZend function with function parameters a&b from your corresponding PHP service
    2. return results to Java program which called PHP service.
  • A named pipe could also be quick/easy solution if you are in *nix. I assume this would out perform I think the queue part would perform better under high load, but this one is easier to implement.
  • MessageQueue in java/C: write a PHP deamon which reads from a queue(redis blocking list pop(*nix)/write your own java version) and puts the answer on another blocking queue from which your Java program will read the answer when available. This option is a little bit hard to implement, but if scaling is a must I think this is the way to go. The redis part could(If not possible to compile it) also be replaced by a simple java deamon which only does have a blocking queue if desired. But the redis variant is really fast/stable and I would really go with that.
Community
  • 1
  • 1
Alfred
  • 60,935
  • 33
  • 147
  • 186
  • Thanks. Are you saying that a daemon is likely to be faster than Quercus? I'm not sure if you used it, but in general what's your guess. – jblue Sep 13 '10 at 23:49
  • it will be faster than Quercus, because quercus has http overhead. I use Redis and it is really fast! – Alfred Sep 15 '10 at 15:09