1

Is there any way to avoid the delay caused by the JVM starting up each time you run an exec command in PHP?

I have two JARs for encryption and decryption that I need to run using PHP. Both run on the same script although one decrypts a URL parameter and then the other encrypts some other information. When I run them through the command line, they both finish in less than 0.4 seconds each. However, when I run them using the PHP exec function, a new instance of the JVM is started which adds 5 seconds onto each JAR execution time.

I have investigated using Nailgun but can't get that to work. I can't find any documentation for getting it to run a JAR and when I use classes, it can never find them either.

I have also considered using PHP/Java Bridge. I would prefer however to continue using exec. I am already running IIS 7.5 and I am not sure how one would configure the bridge to work with this.

My question is this:
Is there any way to keep the JVM running in the background in such a way that the PHP exec function does not need to start a new instance each time? I think there must be a way as there is no delay through the command line.

If there is no way of doing this, then I am open to other suggestions. 11 seconds to run a PHP script means that visitors to the website will most likely leave.

Additional information that may or may not be of use:
It will be running on Windows Server 2008 R2 32-bit OS.
Local access required only.
IIS server 7.5 being used.
Website is coded in PHP. PHP version is 5.3.5.
Server is running the latest JRE - Java7 u6

4
  • That's exactly what nailgun is for, I think. Maybe you should get some advice on getting it to work. Commented Aug 30, 2012 at 9:52
  • @Qnan I will look into Nailgun again if there are no alternatives I can get to work. Do I just edit my question to include asking for advice on Nailgun if anyone has experience using it? Not sure, new enough here. Commented Aug 30, 2012 at 10:05
  • it's usually better to ask a separate question and tag it appropriately, so the people who have more experience with the particular software you intend to use would find it Commented Aug 30, 2012 at 10:11
  • @Qnan ok, I will see if I get a solution to my problem using this question and if not, I will create a new Nailgun related question. Thanks :) Commented Aug 30, 2012 at 10:13

2 Answers 2

2

Is there any way to keep the JVM running in the background in such a way that the PHP exec function does not need to start a new instance each time?

You can start a ServerSocket on a known port.

If its the first time the application has run, this will be successful and this process can keep running.

If this is not successful, the application can open a Socket on that port and send the command as required and get the response.

Sign up to request clarification or add additional context in comments.

8 Comments

I've never worked with sockets on PHP before. I'm assuming I just need to add some code to the top of the script similar to - if socket exists, run exec code. Else create socket and run exec code?
You could do that as well. My suggestion was to have the Java process create the socket and you wouldn't have to change your PHP.
Ok, I will try it using Java then. Thanks for your help. I will try this later on today and post results.
I tried adding this to the top of each main method in the main Java class of each JAR but it didn't work. The JVM delay is still present. Code: final int PORT = 48556; { try { new ServerSocket(PORT, 10, InetAddress.getLocalHost()); } catch (UnknownHostException e) { } catch (IOException e) { } } Perhaps I misunderstood your answer though?
Got it working now. Thanks. I would up vote your answer but I am not allowed ?
|
0

Even though the accepted answer mostly resolved my problem, the JAR execution time was still taking slightly too long. I realize that the question deals specifically with the JVM startup time but if anyone else has a similar issue, this may be of help to them also.

I had been generating executable JARs using Eclipse. I was packing the 3rd party JARs inside my JARs. On examination of the generated JARs, I noticed Eclipse had added a lot of it's own classes one of which was in the manifest as the main class (it dealt with loading JARs within JARs I believe). I also noticed that all the files in the JAR were compressed (as you would expect in an archive).

Seeing as my JARs stay on the server machine and are never transferred over the network, I seen the decompression as an additional overhead that could be easily avoided.

I decided to make three simple changes to my JARs based on the info above:

1) Generate the JARs using java command in the command line and write my own manifest. This eliminated the additional class files that were being added by Eclipse.

2) While generating the JARs through the command line, I added the 0 option to disable compression.

3) I did not include the 3rd party JARs in my JAR archives. Instead, I included them in the same folder and added them to the classpath option in each generated JARs manifest file.

This reduced the time it took to run the JARs by ~269%.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.