Java connection to PHP - java

I'm building a Java based HTTP Server with a plugin system. And I'm trying to find a simple and elegant way to load PHP as a Template Engine plugin. preferably something that also requires very steps for users that install the plugin, for example pointing to the php directory and the plugin does the rest.
However, Google is not proving to be very helpful (it doesn't matter what keywords I use the results are rather useless).
I've found several things about a java-php-bridge, which seems like it could work but it's a rather large old (java 1.4 support) library and I've got a feeling that there is a simpler dedicated solution.
I want to be able to push a map with variables to PHP (headers, get vars, custom vars, etc.)
and retrieve the parsed PHP back (including headers that have been set and other possible output)
and, if possible, I want to do this with the least possible computing time (e.g. I could call php.exe every time, but if I'm correct that's a rather expensive way to handle it)

If you want to interact with PHP in web manner (header, request variables and so on), you can use:
CGI mode (slow and not effective, but rather simple).
FastCGI mode (fast and effective, but requires separated FastCGI server to be running).
SAPI (rather fast and effective, but requires separated SAPI server to be running).
You can search for a client Java code to interact with PHP one of this ways. It solution will not be found, you can write you own client part. Fast and effective solution newer will be simple to programmer and end user.
Using C/C++ php engine can be used as library and linked in application (like mod_php in Apache httpd). This is the fastest way. But it's not possible with Java.

Related

fastest way to connect node.js process with java/scala code

I have a computation library implemented with java/scala. And I also have a little of node.js code serving my application. I need to find a way how to connect this 2 worlds with maximum performance, but also simplicity in mind. I was thinking about inter process communication via shared memory, but don't find any mature way to do that in node.js
This should work mostly as a proxy mechanism to call some java (ideally any) code from node.js code. From node.js to java side it will be only request metadata passing, however from java to node.js sometime it could be significant data returned (let's say 100-200 kb as upper border, and around 600-1000 bytes in 90% of the cases) However amount of that's request could be significant.
Think OpenMP could be an option, but also can't find any openmp protocol implementation for Node. However there is also no clear project for java as well.
Looks like for the current moment there is several alternatives:
Native extension + Java Unsafe (currently extracted via reflection, should be opened in JDK 9) and using shared memory in C/C++ based env (need investigation and development. Looses for Node -> c -> Java could be higher than shared memory benefits)
Use socket (quite fast on linux, not sure about Windows, crossplatform)
FastCGI (still use sockets transfering inside, so will be slower than 1 option)
ZeroMQ/Nanomessage as transport layer (again socket inside, but simplified development)
#David's answer. However can't say anything specific about it. Investigation needed.
Well, if sockets are too slow for you, why not keep it in-process?
You could try:
running your unmodified Node.js scripts on Nodyn which in turn runs on DynJS on the JVM; -or-
if you've not particular to the Node.js stack, but like the idea of extreme-wait-free throughput on the JVM, code it all up in vert.x.
Note: An alternative to Nodyn/DynJS would have been the Avatar.js project, which uses Nashorn, which in turn is shipped with recent JVM's and uses the latest and greatest bytecode operators. However in late 2015 the Avatar.js project feels abandoned. :-\

Is it possible for node.js to import a java library

I have a nodejs application that has some expensive computations. I'm thinking of doing this part in java so I can more easily take advantage of threading and math libraries. Is there an easy way to have nodejs talk to external java libraries?
The java library will contain a loop that frequently calls javascript functions. Will I see a big performance hit due to having these two libraries constantly cross talk (rather than packaging the entire task, sending it to the jvm, and then getting a result back)
It may be better to just create a java server to do the computations and communicate with your node.js application over a messaging queue. Here is an example which shows how to do that - http://blog.james-carr.org/2010/09/09/rabbitmq-nodejs-and-java-goodness/
You might want to take a look a Vert.X, which will let you mix and match JavaScript and Java as you see fit and communicate via a local message bus.

Best way to send "messages" from PHP to Java on the same workstation

What is the best way to send "messages" from PHP script to Java program in real time. PHP script and Java programs are both working at the same work station with OS Windows. Maybe some kind of client/server? The main feature is real time; that's why I don't want to use files.
PS: I'm going to send logger messages (php) and display (java) them at OS system tray tooltip.
PPS: I'm real noob in Java; it will be my first Java program. :)
Thank you.
You could use sockets (probably UDP, but depends on your needs). This way, if in the future you will need to put scripts and Java programs on different machines, you'll be able to do that without modifing the code.
In addition, once you established a communication protocol between client and server, this solution is language independent. So it's easy switch from PHP to another scripting language (the same for Java).
This depend on how heavy weight your application is.
If it is your first program and it is just a little project, a possibility is to open a socket on the server, connect to it with a client and send the data as a string, make your php program the client and java program the server.
Their are things that you can borrow to avoid doing everything on the low level. But they will add weight to your program, for example using a JSON/XML parser to serialize(make the messages into bytes readable on both side) the message instead of using your own format.
Or, use a framework like JAX-RS to quickly and easily (for people familar with it, you may need some time to understand it because it is quite different from writing plain java program) to build a little web service like professionals would do.
Possibilities are:
Send your data as a POST to jsp page.
Make your java code read your php logs.
use queuing systems like RabbitMQ, AciveMQ, Redis etc.
For simplicity use a database table as exchange medium.
It is also easier to debug.
(It is asynchrone, one side, PHP or Java, may be down. Performance is fast, as DB-Server will keep as much in memory.)

Make calls to a Java object running in a existing JVM with PHP

I am getting into an existing project which implies lots of remote objects communicating together to compute and transfer data.
My goal is to create a web application allowing enduser to input some data and get the results after treatment through the existing distributed application.
Regarding that, I looked for a way to make calls to existing Java objects with PHP, but in most of cases it was about how to create a JVM and instanciate objects directly in PHP, but not accessing to an existing and running JVM.
So, what could be the better way to do that ? I also heard about creating a servlet, but I have no real knowledge about this for the moment, so I am sending a S.O.S in a bottle to the StackOverflow community, hoping someone (and I am sure there is) would have a good answer to that problem :-)
Thank you for your time !
You need to modify the java application. You have to add some kind of remote interface to that app, which can then be used by PHP. If you are inexperienced in java, you're out of luck.
One of the options described above is the servlet, which basically means exposing your java app through a REST or SOAP interface. That may or may not be the simplest solution, depending on your java app. If it is a webapp, you're in luck. You can try using JAX-WS to do that. The downside is that such communication comes with an overhead. If your java app is a command line program, you could use an embedded servlet container such as Jetty or try using WSpublish (built into java 6).
You can give Hessian a try. It is a binary web service protocol that supports both PHP and java. I have used it extensively on java-only environments, but it may work in php-java scenario as well. http://hessian.caucho.com/
As you can see, there are plenty of options, but all of them require knowledge and experience in JAVA and cannot be described in one sentence or two.
The easiest java WS example I have seen can be found here:
http://java.dzone.com/articles/jax-ws-hello-world?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+javalobby/frontpage+(Javalobby+/+Java+Zone)
maybe you could try the PHP/Java Bridge project : http://php-java-bridge.sourceforge.net/pjb/
There's one door into a running java virtual machine: the java management extensions. The door has to be unlocked from the inside, so the application has to offer some managed beans and the jvm has to be started with some parameters.
But once this is setup up properly, then you have an open port where you can read and set data from/on instances or execute methods.
I can't tell exactly how difficult it is to use this connection to the jvm from "other languages", maybe you just have to be able to emulate javas object serialization with php. But it might be offer a solution for your actual problem.

Can I call Java from Node.js via JNI and how?

can I call Java from Node.js via JNI? Are there any examples?
You should try the node-java npm module which is a well-written wrapper over JNI.
node-jave doesn't appear to (yet) have broad adoption, but playing with it, I've been impressed with how straightforward and robust it has been.
It's as simple as:
var list = java.newInstanceSync("java.util.ArrayList");
list.addSync("item1");
list.addSync("item2");
console.log(list.getSync(1)); // prints "item2"
You can do just about anything with your embedded JVM - create objects, call methods, access fields, etc.
There is a slight impedance mismatch between Node and Java, so if you are going to interact with something complicated, I'd recommend writing most of your interactions in Java and exposing a simpler interface across the Node/Java barrier. It just makes for easier debugging that way.
--- Dave
p.s., RealWorldUseCase(tm): I worked at a place that had a pretty complex (and spaghetti-coded) protocol between multiple browser clients and a Java-based service. I wrote a pretty sweet test-harness which used jsdom to host N simulated browsers and used node-java as a wrapper around the Java service code. It was trivial to shim out the transport interfaces, both in JS for the clients, and in Java for the service, so whenever any of these things sends a message, I capture that and stick it in a queue for probabilistic delivery to the intended target (ie, I virtualized the network). In this way, I could run a full-on simulation of multiple clients interacting with and through a Java service, and run the whole thing inside a single process without any wire communication. And then I could do fun stuff like deliberately reorder message deliveries to make sure the code was resilient to timing bugs. And when a bug was discovered, I had the message orderings logged and could reproduce them to repro the bug. Oh, and the whole thing set up and ran a pretty complex scenario with a few thousand lines of logging and finished in under 1 second per run. 2-weeks well spent. Fun stuff.
RealWorld Use Case #2: selenium-inproc - a module that wraps the SeleniumRC JAR file providing a node interface to browser automation testing w/ Selenium without having to run yet another localhost service.
That looks tricky. Node.JS runs on the Google Chrome JavaScript engine V8. What you will have to do is to create a V8 C++ binding (v8 c++ Crash Course shows an example) that starts a JVM and does all the JNI handling.
I think you might be better off letting a JavaServer and Node.js communicate via the network (someone wrote an example for using RabbitMQ for Java/Node.js message based communication). Here, JSON would be a great data exchange format (if you trust your Java server produces proper JSON you can just eval() it in Node).
I think what you are looking for is a native extension to use as a bridge. Although I don't have an example of what you are saying, I do have an example on how to call a C++ extension using Node.JS
https://github.com/jrgleason/NodeJSArduinoLEDController
I am not aware of all the details of Node.js but i am assuming that your mentioning of JNI is actually the Java Native Interface. One can only use JNI from Java, so imho it does not make sense to access Java from JNI if you are not already in java.
It would appear that this is the wrong approach, and you need to search the Node.js doco for their integration chapter...
I wonder if it is possible at all. but even if it is possible I imagine it is hard to implement and I am certain that nobody has done that yet.
how about using a named pipe to communicate between processes(java and node.js) ?

Categories