Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make PHP execute and communicate with a Java application on a web server

Tags:

java

php

I have a java application that will take the image as an input and output another image. I have a website with a popular host (PHP+MYSQL Hosting). I want to create a page on the website with PHP with a form where a user can upload an image which will then pass the image onto the Java application.

What I am planning on doing is when then user uploads the image, it gets stored in a folder on the web server. I will then call the java app on the server passing the url of the image as an argument and then the java app will output another image, let’s say, to a result folder. The PHP page after the execution will then display the result image on the browser.

Now my questions are: Is it possible to execute java apps on popular webhosts (for example mine is WebHostingBuzz.com)?

The java app is fairly heavy as it does a lot of image processing. Should I offload the java app to another web server? If yes, are there any services that will run my java app?

(Optional) It’s a demo of my java app and I don’t want to store the images people upload. Is there a way where I can directly pass the uploaded image to the java app and output the image generated directly instead of storing it on the web server? I would prefer this because, if the image is big, I can make PHP stop the execution after a timeout.

How do I communicate with the java app from PHP for info on its execution, for example When PHP calls the java app, the page has to wait till the app finishes processing? I want the java app to send a response to the PHP page saying that the processing is completed and the page is redirected or refreshed accordingly.

I hope you get the idea, please suggest the technologies that I can use to implement this and also if you have a better idea, post it! Thanks!

like image 819
tHeSiD Avatar asked Dec 10 '10 15:12

tHeSiD


2 Answers

Now my questions are: Is it possible to execute java apps on popular webhosts (for example mine is WebHostingBuzz.com)?

It's technically possible. But the hosting has to install JRE at the host and give the PHP user sufficient OS-level and filesystem-level permissions. So you're really dependent on the hosting whether they provide this opportunity. Best is to just contact their support team and ask it.

If it is supported, you could just use shell_exec().

$result = shell_exec("java -jar /path/to/imageprocessor.jar " + $imagepath);
if ($result) {
    // Shell execution succeed.
} else {
    // Shell execution failed.
}

For asynchronous communication / background processing, the client has to fire an ajaxical request.

If it is not supported, consider porting Java to PHP. The GD image library has pretty a lot of functions which may be of use.

like image 191
BalusC Avatar answered Oct 14 '22 07:10

BalusC


Google App Engine allows to host Java (and Python) web applications. The SDK and the basic account is free of charges. With the SDK, you could develop and test the application locally and then simply deploy to App Engine (NetBeans and Eclipse plugins are available).

Then the PHP app could send the data in a HTTP POST to the Google App Engine application and get the result in the response data.

Or the data is stored first in a database blob and a processing job is put in a task queue (a 'message queue'). This has the advantage that the PHP client request will return immediately after the data has been POSTed. Then, the PHP application could poll for the result data while Google App Engine processes the image. The PHP side would be more responsive this way.

like image 29
mjn Avatar answered Oct 14 '22 08:10

mjn