If you are reading that title and thinking "what is that guy smoking?" then you are probably in good company. Developers often make development decisions based off of philosophy. Some developers go the route of "we will decide on the best tool (language) for this individual job" and end up having hundreds of individual tools that they need to end up supporting. Others desire to take a more puritanical approach in saying that "we will only use this one tool (language) for this individual job." The problem is that the decision is often not as cut and dry as many development philosophies allow.
In my experience, having a homogeneous environment is most desireable. Limiting the number of languages and tools that you need to support is often a good thing. The more tools you need to support, the less likely you are to be an expert in any of them. Jobs whose posting states that the developer needs to know Java, PHP, ASP, Perl and C++ will likely end up getting someone who is not only decent in all of them, but lacking a depth of knowledge in all of them. It's kind of like a job posting I saw for a C# developer that required 2 years of C# experience after C# had been out for 6 months. I am mostly convinced that "using the best tool for the job" is the wrong approach. What the better approach is "what is the best tool for solving the organization's problems". Is PHP the best choice for a daemon? Probably not. But does your organization run PHP for your web properties and have minimal needs for some daemonized services? Running PHP as a daemon may be a viable choice. It's not "the best" choice for the job, but it might be the best choice for your organization.
But there are times when you have two disparate ecosystems that need to work together. PHP is often the top contender for building out a web-based interface, but many organizations have internal systems that are running Java. So do you have to squash your dreams of building your website in PHP because you need to have Java connectivity? By no means!!
As part of Zend Server ( and CE) there is a feature called the Java Bridge. Zend Server CE is our free version of Zend Server. While Zend Server does have several useful "for-pay" features there are also several additional features that are shared between Zend Server and CE. The Java Bridge is one of those features. This means that you get the Java Bridge for free.
Using the Java Bridge is really quite simple. First you need to log in to your instance of Zend Server and make sure that you have the Java Bridge installed. You can see that we have it installed on the image on the right. This means that both the Java Bridge extension and the Java Bridge daemon are up and running. The daemon is basically a JVM that is running in the background and servicing requests made by the Java Bridge extension running in PHP.
If the Java Bridge has not been installed you can use the native installer. In this case we are using yum.
[root@localhost ~]# yum search java-bridge
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* addons: mirror.raystedman.net
* base: mirror.san.fastserv.com
* extras: mirror.skiplink.com
* updates: mirrors.igsobe.com
============================= Matched: java-bridge =============================
php-5.2-java-bridge-zend-server.i386 : Zend Java bridge
php-5.3-java-bridge-zend-server.i386 : Zend Java bridge
We have the name, now we need to install it.
[root@localhost ~]# yum install php-5.3-java-bridge-zend-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* addons: mirror.raystedman.net
* base: mirror.san.fastserv.com
* extras: mirror.skiplink.com
* updates: mirrors.igsobe.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package php-5.3-java-bridge-zend-server.i386 0:3.1.26-2 set to be updated
--> Processing Dependency: java-daemon-zend-server for package: php-5.3-java-bridge-zend-server
--> Running transaction check
---> Package java-daemon-zend-server.noarch 0:3.1.25-15 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
php-5.3-java-bridge-zend-server i386 3.1.26-2 ZendServer 21 k
Installing for dependencies:
java-daemon-zend-server noarch 3.1.25-15 ZendServer-noarch 30 k
Transaction Summary
================================================================================
Install 2 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 51 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): php-5.3-java-bridge-zend-server-3.1.26-2.i386.rpm | 21 kB 00:00
(2/2): java-daemon-zend-server-3.1.25-15.noarch.rpm | 30 kB 00:00
--------------------------------------------------------------------------------
Total 96 kB/s | 51 kB 00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : java-daemon-zend-server 1/2
To enable Java bridge, please run /usr/local/zend/bin/setup_jb.sh
Installing : php-5.3-java-bridge-zend-server 2/2
Installed:
php-5.3-java-bridge-zend-server.i386 0:3.1.26-2
Dependency Installed:
java-daemon-zend-server.noarch 0:3.1.25-15
Complete!
From there we need to set up the Java Bridge daemon. Because there is an external dependency, namely Java, we need to do some basic setup on the Java Bridge. Java may not be installed so you will need to make sure that you have that available. If you have Java already installed you can run the zendctl.sh command, which will set up the Java Bridge daemon. I have bolded my type.
[root@localhost bin]# /usr/local/zend/bin/zendctl.sh setup-jb
Welcome to Java bridge setup!
Please specify path the Java executable :
NOTE: please use Java by Sun or IBM version 1.5 and above.
/usr/bin/java
Starting Java bridge [OK]
[ 01.03.2010 16:23:58 SYSTEM] watchdog for java_daemon is not running.
[ 01.03.2010 16:23:58 SYSTEM] java_daemon is not running.
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
At that point we're pretty much ready to go. The Java Bridge works by initiating a TCP connection between the Apache process and the JVM. The Java Bridge extension then sends command such as 'create object "System"' or 'run method "getProperties" on object "42fse2@System"'. The Java Bridge then executes the request and returns the return value of that individual method call. That return value can either be a regular Java object, a primitive or one of a few types that can be converted into native PHP types. If you want to test your installation to make sure that it is running you can do so with the following code:
echo '
'; $java = new java('java.lang.System'); var_dump($java->getProperties());
If all is well and good you should get output similar to this:
array(54) { ["java.runtime.name"]=> string(28) "OpenJDK Runtime Environment" ["sun.boot.library.path"]=> string(52) "/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/lib/i386" ["java.vm.version"]=> string(9) "1.6.0-b09" ["java.vm.vendor"]=> string(21) "Sun Microsystems Inc." ["java.vendor.url"]=> string(20) "http://java.sun.com/" ["path.separator"]=> string(1) ":" ["java.vm.name"]=> string(17) "OpenJDK Client VM" ["file.encoding.pkg"]=> string(6) "sun.io" ...
That's all for today. We will look at some more practical examples in our next look at the Java Bridge.
Comments
Lucas R. Hibbard
Great article!! I have just set up a Zend JavaBridge recently as well. I’ve noticed that there seem to be issues getting to certain .class files that I have already built. Have you noticed anything like that, or have heard of what might be causing this issue?
Thanks.
Kevin Schroeder
Nope. Haven’t run into anything like that. In my experience, as soon as the class files are in the CLASSPATH then I have been able to access them. Just like in a regular JVM. That’s not to say that there isn’t an issue but I’ve not run into it.
Daniel Del Rio
Great article. I use Zend Server CE and always wondered what that Java thingy was for. Looking forward to the examples you spoke about.
Alex
Hello,
Is there someone who benchmark or review Zend Server Java Bridge against the other free php/java bridge (http://php-java-bridge.sourceforge.net/pjb/) ?
What are your opinions ?
Kevin
I have not, though if I were to venture a guess based on differences in architecture I would say that ours would end up being quicker. That said, this would be one of those things that I wouldn’t use performance as a driving force behind my choice unless there was a massive performance difference.