Caffeinated PHP Zendcon 2009 by Kevin Schroeder

2,203 views

Published on

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.

Published in: Technology
  • Be the first to comment

Caffeinated PHP Zendcon 2009 by Kevin Schroeder

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

×