Running Java application & PHP - java

I have a java program that has a healthy Java API, but I want build a primitive interface between my java application and a php script as those are the requirements of my project.
My first attempt was to write a PHP script that ran an passthru function to run the jar. i.e.
passthru("java -jar myjarfile param1 param2 param3")
This worked but proved to be quite slow because the jar file had to be launched and executed etc.
My next attempt was to create a servlet on Tomcat7 and interface it with PHP by usin the curl() command. i.e.
curl(http://myserver/mywebapp/myservlet?p1=param1&p2=param2&p3=param3);
This had excellent performance , but the servlet was very unstable and crashed after about 5 minutes (i was loading the server with about 1 request every 10 seconds)
I come to Stack Overflow asking: am i doing this right? Is there a better way? How can I have my java program running in a jvm and interact with it using PHP?
Thanks

There is a world of difference between the Java method of handling things and the PHP method of handling things.
PHP basically runs every script from beginning to end for each request, which amounts to a very imperative programming technique. Java, on the other hand, typically handles stuff by modules that remain in memory for many more than one request. To integrate the two, you need to consider more than the "function calls", you need to consider how those two environments can be meshed cleanly.
Launching the java per PHP request is asking Java to behave like PHP. In other words, you are going to discard most of the best reasons to use Java by making it work like PHP. Instead, consider setting up a Tomcat (or something similar) instance and passing a request from one to the other. In other words, have the PHP make a web request to a Java environment, which handles things without complete buildup and teardown of the Java interpreter (which is how PHP handles things).

I'm assuming that because you attempted to use a JAR you can have the PHP and Java on the same machine. You may find this document on Java integration in PHP quite exciting. Note that I have never used it, I only know it exists. Be sure to read the introduction.

Related

Best way to execute perl script from java application

Currently from our front end java web application, we use cgi script to trigger perl script to take any action on backend datastore. But its very slow when user takes multiple actions. Is there any way to take actions in parallel ?
I can send multiple cgi at same time to achieve this but browser allows only 6 active connection to server at a time.
As #duffymo points out, it is not a good idea to try to push too many cgi calls at the same time. But maybe you donĀ“t want your Java clients to access the database directly. Trying to answer your question, I would say that you have several courses of action.
One is to try to make the perl CGI code faster. Have you considered using Fast CGI or something similar? It usually does not require to change the perl CGI too much and you will get much faster response times. CGI::Fast for instance, only takes a little change to your CGI program:
while (new CGI::Fast) {
<Your previous CGI code goes here>
}
What it does is leave one or more perl processes waiting for http clients so requests are handled much faster. It takes some configuring on the server side.
Another way of doing the same is mod_perl. There are other alternatives. All of them will require some changes to your perl code and some configuration (maybe module installation) at the server side.
The other approach could be to create a new web service (in perl or in any other language you feel confident) and group together client requests. Doing that you could get many calls to your legacy perl script with only one request.
The master web service would take one macro request which comprises many requests and call the perl script as needed.

Can I use apex-code in java outside salesforce, lets say in a servlet?

I really know very less about apex-code and salesforce. But after seeing a few demo I find their class loading per-request really nice. It will really reduce build deploy run cycle time.
So, can I use apex-code in java outside salesforce, lets say in a servlet?
Is these anything in java world close to this?
Next curiosity would be if SOQL and SOSL can run within java?
IMHO this is not feasible, since the Apex compiler / interpreter is only hosted on the SFDC platform. Salesforce does not provide any offline compilers for Apex, as Apex is always supposed to be run inside/on SFDC server.
Further your SOQL queries are tightly bound to your objects - I'm not sure why would you want to do this in Java.
The only way I know of Java running SOQL queries is via web services exposed by SFDC. ( using the query/querymore methods )

Is a Java to PHP Bridge neccessary for this situation?

We are dealing with an API that sends us a significant amount of data. I'm still in the architecture phase of the application, but my strategy is going to be to use PHP for the frontend portion and then for dealing with the API Id use a set of java classes and access them through calling a java file from the exec function in php. There's too much data for PHP to deal with this, which is why I am deciding to use Java, but I was curious if it would be ideal instead to use a java-to-php bridge? It says that using this bridge is significantly better for performance, but it would take some time to figure out how to install it and get it working.
Truthfully, I just want to call java classes with exec but if initiating a new JVM seems to be considerably intensive than I need to rethink my strategy.
Any thoughts?
Java Bridge could be one option: using Quercus or IBM's WebSphere sMash might be another. I've used all three options, but my personal preference is using an API. Calling java via exec isn't really an API

Interface with running applications in PHP?

I would like to know if there is any way to communicate with a running console program (preferably running on Linux / Debian) via PHP. I am currently trying to create a webinterface for a little (existing) console Java program and I have no idea if there is any way I could do this. Could I "inject" a piece of code, lets say, a remote control module, and then use this to "remote-control" the script via PHP?
(It would be great if the existing .jar file wouldn't be changed / just injection, no reprogramming)
I am grateful for every piece of advice!
If the running program has no communication interface, then you can't communicate with it. If it does however, then the answer very much depends on how the program receives external input.
If the program contains a network listening thread (daemon), then you can communicate with it on the loopback interface using CURL or raw sockets from PHP.
Other ways of communicating with the program would be to share access to a file (PHP writes the file, Java reads it) or via a database.
The database would be the best option - it is thread safe and both PHP and Java have excellent MySQL support (Java via JDBC).
If, however you do not need to actually interface with the running program only merely need to start/stop/restart it, you can do this with the system() function in PHP.
If the Java program just runs and outputs to console, then you can do it easily enough, something like this:
$output = system( "java com.yourcompany.package.RunnableClass" );
print $output;
Assuming that the user who is running PHP has access to the Java binary of course, and that you have permission to access the JAR file.
Accessing a running program is a bit more difficult. Most programs will not have this built in by default (nor should they - giving access to random external processes in many cases is not desirable). If it does, though, you are in good shape. If it doesn't, and you can change the Java code, then you're good. If not, then you may be out of luck.
If that is the case, another good approach might be to see what resources the Java code is accessing, and how it is accessing them. Then you can write something similar in PHP. Obviously this is not ideal as you'll be re-inventing the wheel, but if you need to get to the data or whatever it is, and can't use any of the approaches above, it will work.

Run Java class file from PHP script on a website

I have a website and want to be able to allow the user to run a Java file on the server from the website.
I want the user to click a button which will run the Java file on the server AND anything printed to standard-out by the Java program will be printed out on the website for the user to see.
How can this be done (call Java program from PHP and feed the standard out from the Java file back to the PHP website in real time)?
Update:
Thanks for the answers on how to run the Java program from PHP. However I also want to be able, as the Java program is printing to stdout where it will be printing out a lot of text as it is executing, to be able to print this out on the webpage so that the user can see what stage the Java program is in its execution.
How can this be done and does it require any additional AJAX or JavaScript or anything like that?
The PHP exec() function is the way to go, but you should be very careful in what you allow to executed.. in other words don't rely on user input as it could potentially compromise your entire server.
Calling the Java application launcher using exec, you can execute any Java application from PHP, e.g.
<?php exec("java -jar file.jar arguments", $output); ?>
Since you mention real time I would suggest setting up a PHP to Java Bridge. Initializing the JVM at each request takes up a lot of resources.
PHP/Java Bridge
The PHP/Java Bridge is an
implementation of a streaming,
XML-based network protocol, which can
be used to connect a native script
engine, for example PHP, Scheme or
Python, with a Java or ECMA 335
virtual machine. It is up to 50 times
faster than local RPC via SOAP,
requires less resources on the
web-server side. It is faster and
more reliable than direct
communication via the Java Native
Interface, and it requires no
additional components to invoke Java
procedures from PHP or PHP procedures
from Java.
I would rather wrap the Java class in a Java applet, which can then be invoked from a javascript call on the client side : see http://www.rgagnon.com/javadetails/java-0170.html
Otherwise, if the call throws a lot of text to the standard output or the class has to be run on the server because of system dependencies, calling from php exec is the way to go, but you will probably need something like cometd to display the text on the client in real time. There are implementations for various javascript toolkits such as Dojo or jQuery.
For the server side, there seems to be a cometd implementation in php here.
I hope this helps...
Philippe
Check out exec and the other program execution functions. But do this very carefully, or it's a recipe for exploits.
Is the passthru function of any use?
http://www.php.net/manual/en/function.passthru.php

Categories

Resources