PHP and Java have complementary strengths that make many software development teams want to deploy both and get the best of both worlds. Using the Zend Server Java Bridge, learn to give your PHP applications that extra kick as we look at some options for printing, connecting with message queues and producing 3D graphics for CAPTCHA.
2. About me Kevin Schroeder Consultant for Zend Programmer Sys Admin Author Etc. Me!
3. PHP is growing in the enterprise Programmers (Millions) Part-Time Developers and Hobbyists This means that enterprise PHP developers are going to increasingly need to work with non-PHP systems and do “non-PHP things” Independent Software Vendors Corporate ITDevelopers Source: Gartner Dec 2007, Findings – Corporate and IT Programmers Will Discover PHP's Benefits in Larger Numbers | 3 | 21-Oct-09
4. PHP Extensions Can handle tasks that computationally intensive Can provide integration into existing third party software Some are bundled with the standard PHP distribution, some must be downloaded separately You can write your own!! Written in C What if you are not a C programmer? …or at least, not a good one | 4 | 21-Oct-09 PHP: Past and Present
5. Why Use The Java Bridge? On the web PHP will do most of what you need to do, easier. Java can fill missing integration points in PHP or augment functionality The Bottom Line(s) It allows you to extend PHP without building a C-based PHP module or The PHP/Java Bridge allows you easily connect in with Java-based applications or use existing Java APIs in PHP or Allows you to port applications from Java to PHP on a part-by-part basis | 5 | 21-Oct-09
6. Where does the Java Bridge fit? JEE JAAS JNDI Beans J3D JMX JMS Your Java Code | 6 | 21-Oct-09
13. How do you use it? <?php header('Content-Type: text/plain'); $system = new Java('java.lang.System'); var_dump( $system->getProperties() ); ?> Which produces: | 13 | 21-Oct-09
14. How do you use it? array(50) { ["java.runtime.name"]=> string(48) "Java(TM) 2 Runtime Environment, Standard Edition“ ["sun.boot.library.path"]=> string(29) "/usr/jdk1.5.0_09/jre/lib/i386" ["java.vm.version"]=> string(12) "1.5.0_09-b01" ["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(26) "Java HotSpot(TM) Client VM“ ["file.encoding.pkg"]=> string(6) "sun.io“ ["user.country"]=> string(2) "US" ["sun.os.patch.level"]=> string(7) "unknown“ ["java.vm.specification.name"]=> string(34) "Java Virtual Machine Specification" Etc, etc. | 14 | 21-Oct-09
15. How do you use it? What did you notice? $system->getProperties() returns an object of type Properties But var_dump($system->getProperties()) displayed a PHP associative array. What does that tell us? The PHP-Java bridge will convert any Hashtable-derived object into a PHP associative array. Hashtable methods are no longer available after conversion. | 15 | 21-Oct-09
16. How do you use it? Conversions Integer = int Long = float String = string StringBuffer/Builder remain Java objects Java arrays are converted to PHP arrays All other objects retain their status as Java objects and can be called as such within the PHP code For example$java = new java('java.util.jar.JarFile','me.jar');$entry = $java->getJarEntry('me.txt');$attributes = $entry->getAttributes(); | 16 | 21-Oct-09
17. Use Case #1 - Printing Using the Java Bridge does not just mean running simple Java classes in PHP There are several technologies available in the Java stack that could benefit you Printing is one…
18. Printing to JPS from PHP Part of the javax package Prints using the Java2D interface or via java.awt.print
19. Creating a JPS abstraction layer Code in com.zend.Printer
20. Adding printer support to the Java Bridge Edit the file /usr/local/zend/etc/watchdog-jb.ini
23. Case #2 – Object Serialization Serialization is the process of transforming data into a format that can be transferred across a network PHP does serialization really well PHP NEEDS to do serialization really well Java doesn’t do, or need to do, extensive serialization Any Java object that implements the java.io.Serializable interface can be stored as a string
24. Object Serialization – Serializing to PHP To store the entire Java object each property must implement java.io.Serializable Serialization should occur before a shutdown hook. It may work beforehand, but don’t count on it Therefore, explicitly serialize your object ZF app dispatchLoopShutdown() is a good place if you are serializing to a session variable
26. Case #3 – JMS Integration JMS = Java Messaging System (not the creator of Babylon 5) It is an API for sending messages (data) (very) loosely coupled Asynchronous or synchronous
27. Step 1 – Set up a JMS Broker We’re using ActiveMQ http://activemq.apache.org/ Download and unzip Start with a regular user bin/activemq-admin start
32. Step 6 – Do it in PHP Probably best implemented as an action helper resources.frontController.actionhelperpaths.Default_Helper = APPLICATION_PATH "/helpers“ To the CLASSPATH, add All the libraries in active-mq/lib (hint: don’t type it in by hand)
33. Receiving a message First need to create a helper that initializes the appropriate Java objects. Default_Helper_Mq Receiving a message involves creating a consumer based on a topic and calling receive() Let’s look at some code!!
34. Receiving a message What’s this issue with how this is done? It is a blocking request Why is it done that way? Because only messages sent during the connection’s active phase are received When running in a JVM on a desktop, not problem but since we’re running on a web site this may need some work to make it work well with your app … but we also seem to have a bug with a sort of dead-lock. So… how do we fix it?
35. Debugging Debugging PHP is easy but how about Java from the Java Bridge? Add this to the watchdog-jb.ini file -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044 Verify that the debugger is running on port 1044 [root@localhost ~]# lsof -i -P | grep 1044 java 8256 apache 4u IPv4 14277 TCP *:1044 (LISTEN) Create a new Remote Debug Run configuration and attach
36. Questions? More information can be found at http://devzone.zend.com/article/4776-PHP-and-Java-Using-Java-Print-Service-with-Zend-Server-Community-Edition (just search for “Java Print Service” on the Devzone) http://files.zend.com/help/Zend-Server/java_bridge_component.htm (or just Google “Zend Java Bridge”)