caffeinated php zendcon 2009 by kevin schroeder
DESCRIPTION
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.TRANSCRIPT
![Page 1: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/1.jpg)
Caffeinated PHP
Kevin Schroeder
Technical Consultant
Zend Technologies
![Page 2: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/2.jpg)
2
About me
• Kevin Schroeder Consultant for Zend
Programmer
Sys Admin
Author
Etc.
Me!
![Page 3: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/3.jpg)
3
PHP is growing in the enterprise
| Apr 8, 2023
| 3
2007 20130
1
2
3
4
5
6
Source: Gartner Dec 2007, Findings – Corporate and IT Programmers Will Discover PHP's Benefits in Larger Numbers
Corporate ITDevelopers
Independent Software Vendors
Part-Time Developers and Hobbyists
Programmers (Millions)
This means that enterprise PHP developers are going to increasingly need to work with non-PHP
systems and do “non-PHP things”
![Page 4: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/4.jpg)
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
| Apr 8, 2023
PHP: Past and Present | 4
![Page 5: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/5.jpg)
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
orThe PHP/Java Bridge allows you easily connect in with Java-based
applications or use existing Java APIs in PHP
orAllows you to port applications from Java to PHP on a part-by-part
basis
| Apr 8, 2023
| 5
![Page 6: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/6.jpg)
6
Where does the Java Bridge fit?
| Apr 8, 2023
| 6
JEEJAASJNDI
BeansJ3DJMXJMS
Your Java Code
![Page 7: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/7.jpg)
7
A (rough) Architecture
![Page 8: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/8.jpg)
8
Installing the Java Bridge – Windows
| Apr 8, 2023
| 8
![Page 9: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/9.jpg)
9
Installing the Java Bridge - yum
For Zend Server 4
yum install \
java-bridge-zend-pe
For Zend Server 5
yum install \
php-5.2-java-bridge-zend-server
| Apr 8, 2023
| 9
![Page 10: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/10.jpg)
10
Installing the Java Bridge - apt
aptitude install \
java-bridge-zend-pe
| Apr 8, 2023
| 10
![Page 11: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/11.jpg)
11
Installing the Java Bridge - Setup
• Make sure to execute
/usr/local/zend/bin/zendctl.sh setup-jb
![Page 12: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/12.jpg)
12
Installing the Java Bridge - Configuration
![Page 13: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/13.jpg)
13
How do you use it?
<?php
header('Content-Type: text/plain');
$system = new Java('java.lang.System');
var_dump( $system->getProperties() );
?>
Which produces:
| Apr 8, 2023
| 13
![Page 14: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/14.jpg)
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.
| Apr 8, 2023
| 14
![Page 15: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/15.jpg)
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.
| Apr 8, 2023
| 15
![Page 16: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/16.jpg)
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();
| Apr 8, 2023
| 16
![Page 17: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/17.jpg)
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…
![Page 18: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/18.jpg)
18
Printing to JPS from PHP
• Part of the javax package
• Prints using the Java2D interface or via java.awt.print
![Page 19: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/19.jpg)
19
Creating a JPS abstraction layer
• Code in com.zend.Printer
![Page 20: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/20.jpg)
20
Adding printer support to the Java Bridge
• Edit the file /usr/local/zend/etc/watchdog-jb.ini
![Page 21: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/21.jpg)
21
Adding printer support to the Java Bridge
• Restart the Java Bridge server
![Page 22: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/22.jpg)
22
Let’s look at some code
![Page 23: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/23.jpg)
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
![Page 24: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/24.jpg)
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
![Page 25: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/25.jpg)
25
Let’s look at some code
![Page 26: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/26.jpg)
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
![Page 27: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/27.jpg)
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
![Page 28: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/28.jpg)
28
Step 2 – Browse to http://dev:8161/admin
![Page 29: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/29.jpg)
29
Step 3 – Create a queue (or don’t)
![Page 30: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/30.jpg)
30
Step 4 – Send a message (and get it)
![Page 31: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/31.jpg)
31
Step 5 – View the message
![Page 32: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/32.jpg)
32
Step 6 – Do it in PHP
• Probably best implemented as an action helperresources.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)
![Page 33: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/33.jpg)
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!!
![Page 34: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/34.jpg)
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?
![Page 35: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/35.jpg)
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
![Page 36: Caffeinated PHP Zendcon 2009 by Kevin Schroeder](https://reader033.vdocument.in/reader033/viewer/2022061121/546df4c8af79597b298b5591/html5/thumbnails/36.jpg)
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”)