PHP Communicating w/ Java program - java

We're come across a problem here at my company and I'm trying to find the best solution.
Software was recently purchased that utilizes a Java program to get the tax for a certain shipment. The site that needs this was written in PHP4. How can I communicate between the two?
It was suggested to use files to communicate but that was horribly slow since the Java program needed to be recompiled every time. So, what is the best solutions to this:
Create a mutli-threaded Java server and use PHP to send/receive the info.
Some other type of file-writing method
Something cool that I dont even know about.
Thanks in advance!
Edit:
I understand the importance of web services but why would this be more efficient that using a mutli-threaded socket-based java server? The only thing connecting to this web services will be my PHP program, no one else. It seems like it might be overkill for my simple task. Am I mistaken? If so, why? Thanks.

Wrap the Java program in a Web Service, and invoke it from PHP. You can even use caching in the Web Service, to optimize performance.

Why not dump the info into a database and have some sort of schedualed job read from it once and a while?
You can always use Quercus which allows you to run PHP in a Tomcat Servlet container.

Web Services is the elegant solution. But in many cases I found much practical to go for a quick-and-dirty solution: start a Java server that communicates using a lightweight communication protocol (none of the heavyweight stuff like XML from Web Services) - example: Apache Thrift. The write a very light client, that takes parameters from command line and writes the output to the console. The client can be in Java or even in other languages, like C++ (Apache Thrift supports that). Then you call the client with system() or with exec() from PHP.
This is not a solution I would ever recommend for production, but it's great for prototyping. Quick and dirty and flexible and extremely modest learning curve (if you already use light-weight communication between your Java processes).

Since you are using PHP4, you may want to just set up a tomcat server that is on a closed network, or just local on the machine of interest, and have it communicate with a servlet, that way you don't have to write a multi-threaded server and deal with creating a communication interface.
If you can upgrade, this page has two other options that may of interest:
http://us3.php.net/manual/en/intro.java.php

Give a look at Quercus
Quercus is Caucho Technology's fast, open-source, 100% Java implementation of the PHP language
I never used it though,

Web Services is the answer. Here's a nice intro link. Your problem is the very reason web services came to the forefront - communication between systems that couldn't ordinarily communicate.
What a web service is essentially going to do is send XML between the PHP and the Java systems. You're going to have to establish an interface for the two, which might be more difficult at the upstart, but you'll reap the benefits later on. In either case, it will be much faster than reading and writing files on the server. Disk I/O are the major bottlenecks on any server.

I may miss something, but if your java program output the needed values, can't you just start the java program from php using exec (http://dk.php.net/manual/en/function.exec.php)

Use the PHP/Java Bridge from sourceforge.net. It is mature, fast and easy to install.

Related

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.

http server in php

I want to create http socket connection for server in php for multiple client . how can I do that ? I need some resource .
First I was trying to create server in java .I create a server in java . And trying to reach from android application .But server can't find any client.But when I create client in java .It was working. How can I solve that problem ???
Take a look at this article:
Writing Socket Servers in PHP by Zend
Also give a try with Google:
http://www.google.com/search?aq=0&oq=php+socket+server+mul&sourceid=chrome&ie=UTF-8&q=php+socket+server+multiple+clients
Personally I think this would be a pretty bad idea, as already mentioned it lacks Threading and it's Socket support (imo) isn't really that adaptable.
The only plus side is that you can use fork to fork off another PHP process to handle a client, but you're getting very complex.
Another language would be much more suited for this type of development.
Note that even if you did do this in PHP, you'd probably have to rely on external services anyway, and possibly even end up writing at least some code in another language anyway.
You're trying to use PHP to do what? Mind you, I like PHP and work with it almost every day, but please do remember PHP in and on itself is based on request and response, and not very suitable for long running processes. In a manner of exercise, it might be interesting, but if you're trying to write a webserver from scratch using PHP, you might want to reconsider your choice of language.
That said, you can create a socket acting as a server, and listen to incoming packets. I do still think you're reinventing the wheel though.
though i love php and java, i wrote my socket servers in c++ running under lamp in an amazon ec2 cloud server. it is very, very simple to code and debug and safe and you can practically just copy/paste examples.
in the long run, i will probably develop a java solution because of portability and scalability, but the initial effort to get a c++ solution working is just so much less than implementing a java solution...
the first thing you must ascertain (find out) is whether your server allows you to open custom ports. amazon ec2 does and at this point in time (feb13), can be used for free for 12 months.
so, this is for you if you are in a hurry:
this here set of examples has all that you need to be up and running in no time.
Judging from the question title (the rest only makes it more confusing) you could use an existing package like http://pear.php.net/package/HTTP_Server to implement a webserver in PHP. It already contains all the socket code to accept client connections and stuff.
So what i have to do to find the server from different client
"Finding" is too broad a topic. Depends on your actual setting. On a LAN there are some protocols for discoverability. Otherwise you should just rely on a fixed machine name and port number for your instantiated server. You can connect to it as e.g. http://localhost:8007/ or whatever you've predefined.

Spring / Java, good method for remotely interacting with command line Java app on another server?

I am working on a Spring web application where I have a need to interact with a remotely based command-line java application to run a simple search query on this application and get back the results. I initially had integrated this into my Spring app but my app is, itself, needing a lot of memory (its an app that involves huge amounts of data) and I don't think they can coexist on one server anymore.
I am running everything on Amazon ec2 so the latency between the servers should be really low. I figure I could use a direct SSH connection but am not so sure if this is the best approach. I'd like to keep the command-line app I am interacting with as simple as possible (would rather not make it into a web-service if I don't have to). I'm still fairly new to Java so sorry if this sounds like a basic question.
You have several options other than a web service. Some of them are:
Protocol Buffers
JMS
Simple socket based client/server Java
Thrift
Assuming you have or can have spring on both ends -
Exposing service objects and consuming them from a different process becomes extremely trivial using Spring's remoting support. (RmiServiceExporter may be most appropriate and least trivial to setup in this case)
It really does away with all the boiler plate code and let's you focus on your business/service logic.
You can write a hello world service and consume it from another Java program in less than twenty minutes. Once you have this "infrastructure" setup, you are free to focus on your actual business logic.
(You absolutely don't have to know rmi to get this working though rmi working knowledge may help if you run into problems. But then, what is SO community for? ;))

hosting a java based server

i want to write a game that will utilize java applets as client programs and will run a server application to operate the game (control the game handle the chat etc) is there a way to host such an application on a free server, or does it require a specialized server?
also is there a way to use php for tcp connection so it will receive the data and send it using tcp to the users (using a db to store user information from request to request) (for instance will forward chat massages)
If you are planning to use Java, make it completely on Java based, it will provide you the security and the performance would be much better.
If you are looking for a Free Java Server, I can provide you the Java server to host your Java based application for free.
A little remark about PHP: If you looking at PHP as a possible replace for server-side Java, I think that's not a good idea, cause PHP may be much slower (up to 1000 times according to the benchmarks I've seen, but that might not be absolutely correct).
I've seen a free java hosting called 'MyJavaServer' and that's all that I've found at that moment (couple of years ago). So you'll have to figure out how much java hostings are available now. And of course, there still makes sense to buy/rent a dedicated java server.
Addition: You do not really have to use DB to store intermediate information, you could do that even with PHP through things like 'memcached'.

Categories