Say I have a php application that reads JSON stock data and stores it in MySQL.
Somewhere nearby on the server, I have a compiled Java (or Python, C, etc) executable that can take a set of numbers and return a set of analytics. Is there a way to call and receive data from that program from PHP without using an intermediary text file (or something similar?)
I see theres http://php-java-bridge.sourceforge.net/pjb/, but would it be necessary to run a Java server like Tomcat as well?
Technically you could use the exec function of PHP to call "java -jar YourExecutableJar.jar" and process the return value.
http://php.net/manual/en/function.exec.php
One "general" way is to connect them through sockets -> i.e have a listener at a port in Java that takes the set of numbers and returns the set of analytics back to the socket. The big advantage is that this is universal and can be done between any languages and between remote servers/clients as well.
Technically, any program runninng in background and accepting connections is server. Tomcat is only an example, used in numerous tutorial because it is relatively-light-and-easy-to-learn.
You can use Jetty: Shortest code to start embedded Jetty server
You can use JSON format as communication protocol (GSON library on Java side, on PHP side you already use JSON).
Related
Is there any Java API or Class that can be used in a Java code allowing to detect in real time Snort alerts without using Snort log files ? In other words, does Snort can send alerts which can be recieved by a server process rather than recording them in a log file ?
You can configure the syslog forwarding options.
On Unix
If you are on Unix you can use unix-sockets to send the snort alert to. Then you can either use the Java Native Interface (JNI) to create your own api for unix sockets in java. Or use a preexisting one like junixsocket.
Of course you also need to read the data snort sent to you. This would efficiently also be done with the JNI.
But in the end the whole process of sending data from c-structs in a c program to a java client is never really trivial.
I have some mex files that urgently need to be called via MATLAB, there is currently no way around. However, I really despise MATLAB's GUI (in)possibilities and would like to create some e.g. JavaFX Apps.
My question: how can a Java app's communicate with a running MATLAB instance?
I know that you can include Java objects into MATLAB, however I would prefere to have a standalone Java app.
Java can execute commands via command line for example:
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
So it is possible to execute a MATLAB script via command line in Java.
In MATLAB it is possible to write files with any data needed. I don't remember the exact way you may do this. http://www.mathworks.com/help/matlab/ref/fprintf.html gives an example:
x = 0:.1:1;
A = [x; exp(x)];
fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);
It is some kind of a workaround but it should work and it is not really hard to implement.
Update.
If Matlab is already running and you want to communicate with it in another application (Java), it may be done using a network connection through the localhost. Matlab may listen to some predefined port (for code example see http://www.mathworks.com/matlabcentral/fileexchange/11802-matlab-tcp-ip-code-example ) and do some action when a "start" trigger is sent via Java (or even some data along with the trigger). In Java you may use the Socket class (some code example may be found here http://www.javaworld.com/article/2077322/core-java/core-java-sockets-programming-in-java-a-tutorial.html ).
Also it may be done writing data into files. For example, Java adds some command to some file with predefined name (command.txt). Matlab scans this file in a loop and when something is found there it starts calculation (and Java application waits for results in some results.txt file).
I would suggest to start a server in Matlab that listens on a specific port to send/receive data to/from a Java client. By using the eval Matlab command you could even invoke scripts/command/etc. remotely controlled by a Java client.
You might want to have a look at this code example.
I have a java program that runs on a couple different computers and I want to be able to be able to access them remotely. I don't need to do anything crazy, just some very basic input output (get and set type stuff). I could write a client type application and set each program to a server with RMI and do it that way but I will not always be on the same computer and do not want to have to carry the .java file around with me.
I'd prefer to write a simple php webpage that I can access from anywhere and have some very basic buttons that could send the commands to the different Applications and receive back some output.
Problems is that it would probably impossible to get my host to load anything other then php onto the server where I'd host my webpage.
Does anyone have any ideas? I've tried googling it and found a couple things out there that sort of sound like what I need but the more I look into them they seem to not be what I wanted after all.
If you really want to do this right I'd suggest looking at a Java web framework (I use Play! myself) to create a RESTful web service and then sending requests from your PHP code that will fetch JSON data.
A framework like Play will make it very easy to get a REST web service running. Just create some wrappers that invoke your existing java code and call renderJSON to output the result.
From the PHP code you'll issue the request using curl and then read the response using json_decode which will turn it into a handy php variable.
This also has the advantage that either end can be changed without the other noticing, as long as the json data format is kept the same.
Based on the way you described your setup you'll probably run into routing issues trying to reach your Java apps from your PHP host. I'd recommend you try something else: have your Java apps frequently check with your PHP app if there are any updates. You can simply use a Java URL connection and a timer:
http://download.oracle.com/javase/6/docs/api/java/util/Timer.html
http://download.oracle.com/javase/6/docs/api/java/net/URL.html
I have an apache2 webserver with php installed and working. I am wondering if there is any way I can get a PHP script to access a Java program that is constantly running, mostly to just get information that the process keeps track of. The Java program is not a webapp or running within the apache server. It is a separate program.
Ive been looking into PHP/Java Bridge systems, and all they seem to do is let you run Java programs from PHP and vice-a-versa, which is not what I need. I can think of a way to do this via a web service and SOAP running on localhost, but if there is a way to avoid this and I can gain direct access to the Java program, I could get my webapp to run much faster.
The Java app that I am trying to "talk to" is a program that I wrote so I have access to the source code to make the necessary changes if something within the Java program needs to be set up.
Thanks in advance, I've been looking all over for something like this.
You'll need to communicate with the Java process in one way or another. As a web service is too heavy, you could simply communicate over a socket with a custom text or binary protocol.
Or you could also perhaps make the Java program write to a database every n seconds, and read from the database in your PHP script.
EDIT :
Look at http://www.php.net/manual/en/sockets.examples.php for a socket client example in PHP.
Look at http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html for a server socket example in Java.
PHP/Java Bridges let you call a running Java program from PHP.
Read: http://en.wikipedia.org/wiki/PHP/Java_Bridge
Read: http://php-java-bridge.sourceforge.net/pjb/desktop-apps.php
The bridges actually use local socket communication, from the faq:
<?php
include_once("Java.inc");
$i1 = new Java("your.class", "1");
$i2 = new Java("your.class", "2");
$i3 = $i1->add($i2);
echo $i3->toString() . "\n";
?>
See Java.inc.
And edit your java code to contain the following line:
static final php.java.bridge.JavaBridgeRunner runner = php.java.bridge.JavaBridgeRunner.getInstance("9267");
Hey, so basically you need to run java application from php and get response from java application?
If its that so - I can't see any problem fro php/java-bridge. For example I've successfully run java encryption method for custom encryption in php.
You might look into Quercus/Resin. PHP runs inside Java on a Java web server. You can call Java code as if it was PHP functions/commands. All PHP commands are not supported, both most are. For example, you can run WordPress under Quercus.
http://www.caucho.com/resin-3.0/quercus/
I don't know how to connect to RMAN in Java. Simple request don't work when I use command non-SQL, like RMAN etc... I'm gonna to make program like "ORACLE Secure BackUP", but how they connected to RMAN??
I don't believe that there is a published API for interacting with RMAN other than the command line. So if you wanted to create a program like Oracle Secure Backup that used RMAN, you'd probably have to invoke the RMAN executable from Java. Which means that you'd probably also have to parse the output of the various RMAN commands in order to provide feedback to the user.
If you want to go down this path, you'd want to use the Runtime class in Java. Here is an example of calling an external program from Java.