Caffeinated PHP Zendcon 2009 by Kevin Schroeder
-
Upload
sam-hennessy -
Category
Technology
-
view
3.080 -
download
1
description
Transcript of Caffeinated PHP Zendcon 2009 by Kevin Schroeder
Caffeinated PHP
Kevin Schroeder
Technical Consultant
Zend Technologies
2
About me
• Kevin Schroeder Consultant for Zend
Programmer
Sys Admin
Author
Etc.
Me!
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”
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
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
6
Where does the Java Bridge fit?
| Apr 8, 2023
| 6
JEEJAASJNDI
BeansJ3DJMXJMS
Your Java Code
7
A (rough) Architecture
8
Installing the Java Bridge – Windows
| Apr 8, 2023
| 8
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
10
Installing the Java Bridge - apt
aptitude install \
java-bridge-zend-pe
| Apr 8, 2023
| 10
11
Installing the Java Bridge - Setup
• Make sure to execute
/usr/local/zend/bin/zendctl.sh setup-jb
12
Installing the Java Bridge - Configuration
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
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
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
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
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
21
Adding printer support to the Java Bridge
• Restart the Java Bridge server
22
Let’s look at some code
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
25
Let’s look at some code
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
28
Step 2 – Browse to http://dev:8161/admin
29
Step 3 – Create a queue (or don’t)
30
Step 4 – Send a message (and get it)
31
Step 5 – View the message
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)
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”)