Home > Uncategorized > Extending the Grizzly HTTP Runtime part IV: Adding native PHP with support for the Bayeux Protocol

Extending the Grizzly HTTP Runtime part IV: Adding native PHP with support for the Bayeux Protocol

Before I deep dive into the AsyncFilter API I’ve proposed in part 3, I’ve decided to show how easy is to add…PHP to your Grizzly based application with support for the Bayeux protocol.

IMG_0321.JPG

In part one, two and three, I have showed how easy is to embed the Grizzly Http Runtime and add JMX and Monitoring support. This time I would like to add PHP to the picture. Yes, PHP! A couple of months ago, my friend Kin-Man blogged about running native PHP using the jsr223 Servlet. The PHP Servlet used in Kin-Man’s blog uses jsr223 scripting API and JNI to communicate with a native PHP runtime. That’s a really good Servlet to serve directly from Grizzly. How, by just doing the following:

   
        GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
        try{
            ServletAdapter sa = new ServletAdapter();
            sa.setRootFolder("/Path/To/PHP/Folder");
            sa.setServlet(new com.sun.script.php.PHPServlet());
            // PHP requests are serviced under /php/
            sa.setContextPath("/php/");
            ws.addGrizzlyAdapter(sa);
            ws.start();
        } catch (IOException ex){
            // Something when wrong.
        }

That’s it. One thing you must make sure is to set the -Djava.library.path to point to your native PHP installation libs directory. Example:

 java -Djava.library.path=/path_to_php_installation/php/php-5.0.1/libs/ \ 
          -jar  grizzly-servlet-webserver-1.8.3.jar -p 8080 -a /www/php

will works, assuming you have added the required jars Kin-Man talks about inside the original grizzly-servlet bundle. Now let’s add the Comet based Bayeux protocol to the picture. This is also quite simple:

   
        GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
        try{
            ServletAdapter sa = new ServletAdapter();
            sa.setRootFolder("/Path/To/PHP/Folder");
            sa.setServlet(new com.sun.script.php.PHPServlet());
            // PHP requests are serviced under /php/
            sa.setContextPath("/php/");
            ws.addGrizzlyAdapter(sa);

            // Add Comet Support
            ws.addAsyncFilter(new CometAsyncFilter());
 
            //Now that we have Comet support, 
            CometdAdapter cometdAdapter = new CometdAdapter();
            ws.addGrizzlyAdapter(cometdAdapter);

            ws.start();
        } catch (IOException ex){
            // Something when wrong.
        }

That’s it! You have now a WebServer that supports PHP and the Bayeux protocol, where some requests are dispatched to PHP, and some to the Bayeux implementation. You can also bridge and push data from your PHP application to the Bayeux runtime using the PHP SAPI and by following Shing Wai’s blog on Java API to Bayeux we offer in Grizzly. Coucourouquuiquui! Since it’s vacation time, post your questions to users@grizzly.dev.java.net to be sure you get a response🙂

_uacct = “UA-3111670-1”;
urchinTracker();

technorati:

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: