The Source for Java Technology Collaboration
User: Password:
Register | Login help    

Search

Online Books:
java.net on MarkMail:


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

Posted by jfarcand on July 17, 2008 at 12:19 PM PDT

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 :-)

technorati:

Related Topics >> Glassfish      
Comments
Comments are listed in date ascending order (oldest first)

Hello I tried most of the comet example, good and am already implemented cometd with bayeux and dojo. What we need is a service that distributes data to different applications, not web, ie desktop applications in Java swing, Flash and J2ME applications. Do not or can not make the connection from my server with these different applications, I do not know if I need a special API, or because there is any implementation for this, for example, Java applications on my desk, reading into the data through a Timer that reads XML files of information. This should eliminate completely and make me read the data immediately envy from my server push. Well, while waiting for your response and great help, was dismissed from Chile. Maria Graciela Manquehual

SalutThe API you are looking at are described [1]. Sorry I was on vacation. next time post your question to users@grizzly.dev.java.net.Thanks! [1] http://blogs.sun.com/swchan/entry/java_api_for_cometd