I'm writing a watchdog-style program in Java - that is, it will be running constantly.
I want to be able to somehow send input and receive a response using PHP.
What is the best way to do this?
Thanks!
EDIT: Just to clarify, the Java and PHP are running on the same machine.
A really simple solution,not as simple as the "file solution" by Itay), but more light-weight than HTTP or XML-RPC would be using a plain socket-based solution, as simple as illustrated here.
That would fit nicely for your "String back and forth" protocol.
There are a million ways to do this: one comparatively easy way is using a networked RPC system such as XML-RPC. You can quite cleanly call from PHP and receive the responses back.
very crud and not the most efficient
A socket like way:
Create file A with permissions to both the Java process and php process.
Java writes to the file, only when it is empty (or concatenate)
PHP reads the file and deletes it.
Do the same but the other way around for the PHP to send input to Java.
Again, this is crud and as you can see in the search result above, there are ready made solutions.
I'd do this by running the Java program in a servlet container (Jetty or Tomcat would do just fine) and use HTTP request/response between the PHP and Java. The Servlet will effectively provide an HTTP interface to your Java program. On the PHP side you can use cURL, file, or even fopen to perform GET requests, or cURL to perform POSTs and retrieve responses.
The data format you use for transactions is up to you, JSON seems like an easy choice, there's built-in support in PHP and excellent libraries for Java.
Maybe I'm missing something with all those giant libraries floating around, but what about using a socket and communicating over TCP? Can't get much simpler for transmitting simple data like strings and should be fairly performant.
Related
I have a PHP web application that should perform reasoning on an OWL (Ontology Web Language) document. I know of two reasoners, both of which are written in Java, but I have a standard Apache server at my disposal, so I can type in PHP, not in JSP.
Is there a way to execute Java code on server, like this: the PHP script sends data to the Java code (reasoner), then Java code performs the reasoning and returns data to the PHP script? Is there a way for one Java program to be perhaps somehow wrapped in something on the server, or whatever?
Thanx,
Martin
I think a good approach in your case would be to expose the reasoner via a SPARQL endpoint, and write your application in PHP against the SPARQL protocol.
Calling out to Java from PHP sounds like a hack, and seems brittle. If you base your application on the SPARQL protocol, you can get away with just HTTP, JSON, & XML libs, you don't need anything strictly RDF or OWL specific. Could simplify your implementation quite a bit.
Further, if you use SPARQL protocol, it isolates you from implementation specific details for whatever reasoner/database you're using. You can switch to a new reasoner and as long as it's exposed as a SPARQL endpoint, you don't need to change the code in your webapp.
None of that really gets around you having to run the reasoner on a machine, and most likely you'll need a servlet container, though some options can run stand-alone.
You need first to be able to run some Java code on your machine and you have to install a JVM on your server.
Then you could look at the following answer: calling java code from PHP . This should give you an idea how to call a Java program from PHP.
An alternative hack if you don't rely too heavily on the reasoning would be to open the ontology with Protege, classify it and then save it as such. Therefore you won't have to use any Java on the server side and you would just consume the data present in the ontology.
I was going to use an objectOutputStream but heard this is unreliable because different java versions might deserialize objects differently. Something about 'horrible cross-architecture practice..'
So how else can I send objects and arrays between these devices, where the receiving end can piece back together the proper object or array data?
Edit: Just read what you are doing. You might not need a web server. A lot of people recommend one because of the massive support web servers have. You certainly use TCP or UDP to talk between a server and a client. You'll need to have some protocol if you want data interchange, and most people here would be familiar with XML or JSON
If you need inspiration, try looking at a few protocols like FTP, or even Bittorrent
Web Server case:
I wrote a Java web server for a college homework assignment. A web server actually be quite simple if you have a good grasp on TCP/IP. The code scattered everywhere online to do it though gets a little hard to decipher what exactly is going on, but once you do, it's not bad
You definitely should check out the RFC for HTTP, even though those tend to be worded in legalese. Beyond that, on the server, you basically read strings in line-by-line and you should be able to figure out what to do on the server (example GET /somefile.html HTTP/1.0). Just do System.out.println on those lines and go from there. The same goes for client code. You also can use telnet to see what a web server does
To test, I would actually recommend trying just a regular web browser like Firefox, Chrome, IE, Safari, and even curl scripts. This is an easy test to see if your server is running correctly
As far as data interchange goes, XML or JSON would be recommended, mostly that if you learn how to handle it, you get 100 experience points for your resume. However, to get things started, you can start out just by sending and receiving text like "Wazzzaaap". Web browsers can also grab XML and JSON data.
By 'java server', what kind of protocols are you using?
One option is RPC, which is defined in java.rmi
If you are using http, the simplest choice is to implement a small servlet in tomcat/jetty and use restful services
The data format can be xml, json, bin, etc
I want some help on a way to transfer data from a python web application to a java desktop application.
What I am doing is having java listen on a port and receive data. But I have no idea how I would send data from python to an open port on a server.
What my question is how would I send data from a python web app to an open port on a computer. And would there be any problems like data types and any other things?
This is a really large question as there are many ways to send data back and forth between server (your java app) and client (your python app).
Your situation is not quite clear (what exactly is your "python web application"?), but you may want to look into XML-RPC. XML-RPC is extremely simple to use and set up, and takes care of "problems like data types and any other things". You basically just set up some functions on your server that the client can call, and have python call them. Arguments are neatly wrapped up by teh client and unwrapped by the server. Return values are the same. It is a simple and clean interface.
For python making calls to the server, you want to use the xmlrpclib module.
To set up an XMLRPC server in java, you have many options. I'm not a Java guy, but I'm sure it is quite simple on that side as well.
There are many good xml-rpc tutorials. Here is one that covers client and server in python.
Like I said earlier, there are MANY options available to you. XML-RPC is a good and simple way to get your feet wet, without really limiting you very much (eg: it has built in fault handling).
Good luck!
If you use a platform independent data format -- xml, json, yaml, ascii txt, ... -- to represent numbers, you have really nothing to worry about.
If you can not afford the inefficiencies of above, then a binary protocol is required.
Java uses network byte ordering (or Big Endian). Python uses the native host byte ordering, OR, you can specify the byte ordering. Here you want to specify Big Endian (sec 7.3.2.1) in writing your numeric data.
Why not use sockets in python too and send it to the java server. Java does not know that the end client is python, what it reads is just data(bytes). I have done this, and it works seamlessly.
See the python's struct module for more details on converting datatypes
I have a component based library which is completely written in PHP and works like a charm.
A sample snippet of the code is :
<?php
require_once 'lib/my_class_file.php';
$variable1 = "some-value";
$variable2 = "some-value";
$new_variable = ClassName::newInstance(.....);
$new_variable->method_call($args);
?>
Now, I want to extend the use of this library to other platforms like Java, ASP.NET and Python. I don't know how can I consume this library in other languages like Java, ASP.NET and Python.
My concern is whether this is possible or not. If possible any pointer / online tutorials / sample code would be highly appreciated.
Thanks in advance.
A web service will be the common answer for your problem. In the past SOAP was popular - you can still use it, but it's probably better to use a simple REST server, or even better, use Thrift as a generic scalable solution. To use it, you first need to describe your data structures for your parameters and return value using a definition file, and then run a script which creates servers and clients for the various programming languages you will use.
See also https://github.com/volca/thrift for a non-blocking port of the php server (I did not test it myself)
I am implementing a website using PHP for the front end and a Java service as the back end. The two parts are as follows:
PHP front end listens to http requests and interacts with the database.
The Java back end run continuously and responds to calls from the front end.
More specifically, the back end is a daemon that connects and maintain the link to several IM services (AOL, MSN, Yahoo, Jabber...).
Both of the layers will be deployed on the same system (a CentOS box, I suppose) and introducing a middle layer (for instance: using XML-RPC) will reduce the performance (the resource is also rather limited).
Question: Is there a way to link the two layers directly? (no more web services in between)
Since this is communication between two separate running processes, a "direct" call (as in JNI) is not possible. The easiest ways to do such interprocess communcation are probably named pipes and network sockets. In both cases, you'll have to define a communication protocol and implement it on both sides. Using a standard protocol such as XML-RPC makes this easier, but is not strictly necessary.
There are generally four patterns for application integration:
via Filesystem, ie. one producers writes data to a directory monitored by the consumer
via Database, ie. two applications share a schema or table and use it to swap data
via RMI/RPC/web service/any blocking, sync call from one app to another. For PHP to Java you can pick from the various integration libraries listed above, or use some web services standards like SOAP.
via messaging/any non-blocking, async operation where one app sends a message to another app.
Each of these patterns has pros and cons, but a good rule of thumb is to pick the one with the loosest coupling that you can get away with. For example, if you selected #4 your Java app could crash without also taking down your PHP app.
I'd suggest before looking at specific libraries or technologies listed in the answers here that you pick the right pattern for you, then investigate your specific options.
I have tried PHP-Java bridge(php-java-bridge.sourceforge.net/pjb/) and it works quite well. Basically, we need to run a jar file (JavaBridge.jar) which listens on port(there are several options available like Local socket, 8080 port and so on). Your java class files must be availabe to the JavaBridge in the classpath. You need to include a file Java.inc in your php and you can access the Java classes.
Sure, there are lots of ways, but you said about the limited resource...
IMHO define your own lightweight RPC-like protocol and use sockets on TCP/IP to communicate. Actually in this case there's no need to use full advantages of RPC etc... You need only to define API for this particular case and implement it on both sides. In this case you can serialize your packets to quite small. You can even assign a kind of GUIDs to your remote methods and use them to save the traffic and speed-up your intercommunication.
The advantage of sockets usage is that your solution will be pretty scalable.
You could try the PHP/Java integration.
Also, if the communication is one-way (something like "sendmail for IM"), you could write out the PHP requests to a file and monitor that in your Java app.
I was also faced with this problem recently. The Resin solution above is actually a complete re-write of PHP in Java along the lines of JRuby, Jython and Rhino. It is called Quercus. But I'm guessing for you as it was for me, tossing out your Apache/PHP setup isn't really an option.
And there are more problems with Quercus besides: the free version is GPL, which is tricky if you're developing commercial software (though not as tricky as Resin would like you to believe (but IANAL)) and on top of that the free version doesn't support compiling to byte code, so its basically an interpreter written in Java.
What I decided on in the end was to just exchange simple messages over HTTP. I used PHP's json_encode()/json_decode() and Java's json-lib to encode the messages in JSON (simple, text-based, good match for data model).
Another interesting and light-weight option would be to have Java generate PHP code and then use PHP include() directive to fetch that over HTTP and execute it. I haven't tried this though.
If its the actual HTTP calls you're concerned about (for performance), neither of these solutions will help there. All I can say is that I haven't had problems with the PHP and Java on the same LAN. My feeling is that it won't be a problem for the vast majority of applications as long as you keep your RPC calls fairly course-grained (which you really should do anyway).
Sorry, this is a bit of a quick answer but: i heard the Resin app server has support for integrating java and PHP.
They claim they can smash php and java together: http://www.caucho.com/resin-3.0/quercus/
I've used resin for serving J2ee applications, but not for its PHP support.
I'd be interested to hear of such adventures.
Why not use web service?
Make a Java layer and put a ws access(Axis, SpringWS, etc...) and the Php access the Java layer using one ws client.
I think it's simple and useful.
I've come across this page which introduces a means to link the two layers. However, it still requires a middle layer (TCP/IP). Moreover, other services may exploit the Java service as well because it accepts all incoming connections.
http://www.devx.com/Java/Article/20509
[Researching...]