• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Caffeinated PHP Zendcon 2009 by Kevin Schroeder
 

Caffeinated PHP Zendcon 2009 by Kevin Schroeder

on

  • 2,978 views

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 ...

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.

Statistics

Views

Total Views
2,978
Views on SlideShare
2,923
Embed Views
55

Actions

Likes
4
Downloads
68
Comments
0

1 Embed 55

http://www.slideshare.net 55

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Demo is a manual login procedure

Caffeinated PHP Zendcon 2009 by Kevin Schroeder Caffeinated PHP Zendcon 2009 by Kevin Schroeder Presentation Transcript

  • Caffeinated PHP
    Kevin Schroeder
    Technical Consultant
    Zend Technologies
  • About me
    Kevin Schroeder
    Consultant for Zend
    Programmer
    Sys Admin
    Author
    Etc.
    Me!
  • 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
  • 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
  • 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
  • Where does the Java Bridge fit?
    JEE
    JAAS
    JNDI
    Beans
    J3D
    JMX
    JMS
    Your Java Code
    | 6
    | 21-Oct-09
  • A (rough) Architecture
  • Installing the Java Bridge – Windows
    | 8
    | 21-Oct-09
  • 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
    | 9
    | 21-Oct-09
  • Installing the Java Bridge - apt
    aptitude install
    java-bridge-zend-pe
    | 10
    | 21-Oct-09
  • Installing the Java Bridge - Setup
    Make sure to execute
    /usr/local/zend/bin/zendctl.sh setup-jb
  • Installing the Java Bridge - Configuration
  • 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
  • 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
  • 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
  • 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
  • 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…
  • Printing to JPS from PHP
    Part of the javax package
    Prints using the Java2D interface or via java.awt.print
  • Creating a JPS abstraction layer
    Code in com.zend.Printer
  • Adding printer support to the Java Bridge
    Edit the file /usr/local/zend/etc/watchdog-jb.ini
  • Adding printer support to the Java Bridge
    Restart the Java Bridge server
  • Let’s look at some code
  • 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
  • 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
  • Let’s look at some code
  • 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
  • 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
  • Step 2 – Browse to http://dev:8161/admin
  • Step 3 – Create a queue (or don’t)
  • Step 4 – Send a message (and get it)
  • Step 5 – View the message
  • 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)
  • 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!!
  • 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?
  • 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
  • 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”)