views:

54

answers:

1

I'm trying to get started on combining my (slightly limited) PHP experience with my (better) Java experience, in a project where I need to allow uploads of Java source files to the server, which the server then executes Javac on to compile it.

Then, at a set time (e.g. specified on upload) I need to run that once on the server, which will generate some database info for the PHP site to display.

To describe my current programming abilities- I have made many desktop Java programs, and am confident in 'pure' Java, but so far have only undertaken a couple of PHP projects (including using the CodeIgniter framework).

My motivation for using PHP as the frontend is because I know it is very fast, lightweight and I will be able to display the results I need very easily with it (simple DB readout). Ideally, the technology used should be able to be developed on a localhost (e.g. WAMP, Tomcat etc..)

Is there any advice which you could give on what technology I should consider to use to bridge this gap, and what resources could help in using that technology? I have looked at a few, but have struggled to find documentation helping in achieving what I need.

A: 

There's a large number of ways to approach this and you didn't provide enough details to narrow down to just one way, so I'll propose a few... Compiling the java source will probably take too long for a normal web request, so my recommendation would be to have the PHP script which accepts the java source files run a background command which compiles the source files and logs it's progress to a database or log file. This way, you can redirect the user to a page that says, "Please wait, your source is being compiled" and use Ajax to check in every few seconds to see the updated progress. If this is the route you want to go check out the PHP exec command.

It might be more beneficial for PHP to simply create an ID in a database for each uploaded file and call move_uploaded_file to move the source files to an "incoming" directory. From there, a cron job that runs every so often can scan the incoming directory or the database for source files, compile them, and update the database when done.

In either of these cases the program which does the compiling could be written in any language, even Java, if you prefer.

Josh
With regards to a CRON job, what sort of overhead is there? Most uploads will likely be around a deadline, as this is a development towards a submission system, so it would seem inefficient run the CRON job often if it is only really needed to run a couple of times around a predefined deadline. Is there any flexibility in CRON which could fit this sort of system?
obfuscation
If the uploads should be processed quickly, I would recommend using PHP's `exec` command to start a background job instead.
Josh
The main concern I would have to guarentee is that the uploads are given a sense of 'priority' over the processing. In this case, would you perhaps argue against using exec, since I assume it would create extra 'competing' threads? Ideally, it may be best to create some form of 'queue' of uploads that are waiting to be processed, and process these at a given rate. Is there any methodology you'd suggest in this case? (In Java, I'd probably just create a seperate thread in which objects are processed in increments)
obfuscation
You could certainly do that. Just have a Java daemon which is constantly running, checking the DB for new uploads and spawning threads to handle them. More complicated but you have much more control.
Josh
I've been trying using the exec command for starting the Java side of things with little success (and the oh so useful error code '1' as the only result). If I were to have the Java backend constantly running, how could interaction with it be handled (for example, obtaining information about any error states it may have encountered)? Similarly, how could the launch/closure of the Java be handled (e.g. so it wouldn't be polling the DB all year, if only needed for 6 months of it)?
obfuscation