Zend Server User Guide > Reference > Java Bridge Use Cases

Java Bridge Use Cases

This section describes some of the common uses for the Java Bridge. The usage scenarios and examples discussed here provide a framework for the Java Bridge’s uses, rather than a complete picture. Real world experience indicates that companies are finding more and more applications for the Java Bridge, beyond what was initially anticipated.

Usage Scenarios

There are two usage scenarios that describe the most common applications for the PHP/Java Bridge:

Activities

This section describes two sample activities that indicate some of what you can do with the PHP/Java Bridge. In the sample activities, it is important to differentiate between Java and J2EE. The difference will impact on architecture and in turn, on the script code.

The important differences are:

Example 1: A Case Study in Java Bridge Performance (Java)

The Forever Times newspaper maintains a PHP-based website - let’s call it ForeverOnline.com. The newspaper has been searching for a real-time Stock Ticker application to add to their already successful and heavily visited website. The Forever Times Newspaper feels that real-time financial information is the one thing their website is lacking.

Forever Times believes they have found exactly the Stock Ticker application they need. The application provides up-to-date quotations from all the major markets, currency rates, and even links to some of the local exchanges. However, the application is written in Java and uses existing Java libraries.

Forever Times realizes that a PHP-based Web implementation that handles Java requests - a Java Bridge - is their best bet. At the same time, they are concerned that the performance of their Website remains optimal. To Forever Times’ horror, in testing the new application, they find that loading the site with user-requests for the Stock Ticker slows down the performance of the whole website.  

The following code example illustrates how the Java Bridge applies to this business scenario and others like it:

Usage Example

Example:

<?

// create Java object

$stock = new Java("com.ticker.JavaStock");

// call the object

$news = $stock->get_news($_GET['ticker']);

// display results

foreach($news as $news_item) {

print "$news_item<br>\n";

}

?>

 

The example code can be understood as follows:

  • The code example is written in PHP and forms part of a PHP Web application.
  • The PHP code creates the Java object-"com.ticker.JavaStock"-which is the PHP proxy.
  • Requests come into the PHP based Website - ForeverOnline.com - which then references the Stock Ticker application.
  • Stock Ticker references a custom object- get_news-in the JVM library.  This is all in native Java.
  • The PHP code then outputs the results on the Website.

As opposed to a typical Java Bridge Implementation, the Zend Server Java Bridge implementation addresses performance issues through the Java Bridge architecture.

Implementing the Java Bridge is a way to address scalability issues by using the Java Bridge to handle all communication in a single JVM instance, instead of in several instances.

Note:

While the single JVM constitutes a single point of failure, the fact is, Zend’s PHP-Java connection is the most robust on the market. Failures in systems of this type generally tend to occur when the Java Server is overloaded, rather than as a result of glitches in the applications. Zend Server ’s system architecture insures performance by diminishing overhead. However, in the event of failure, the Java Bridge supports a restart feature that makes monitoring the status of the Java Server and restarting quick and simple. One last point: if the failure was caused by a glitch in the application, the same thing would most likely occur in each of the JVMs in the non-Zend system!

Example 2: A Case Study in Management Integration (J2EE)

A company called FlowerPwr.com sells flowers over the Internet. They are a successful East Coast-based firm that has an aggressive management profile. They are currently in the process of acquiring a West Coast competitor - let’s call it Yourflowers.com - that provides a similar service.

FlowerPwr.com has its own website: Its various enterprise applications are written in PHP. Yourflowers.com also has its own Website: However, all its applications are Java-based and were developed for J2EE. They have their own J2EE application server. FlowerPwr.com needs to begin operating as an integrated commercial entity as soon as possible, in a way that conceals the fact that the companies have merged.

Using the Java Bridge, FlowerPwr.com can create a common portal in PHP. The company can leave Java up and running and take full advantage of their acquisition’s existing Java services. FlowerPwr.com can do this over an existing portal using PHP.

The following code example illustrates how the Java Bridge can apply to this business scenario and others like it:

Usage Example

Example:

<?

// EJB configuration for JBoss. Other servers may need other settings.

// Note that CLASSPATH should contain these classes

$envt = array(

"java.naming.factory.initial" => "org.jnp.interfaces.NamingContextFactory",

"java.naming.factory.url.pkgs" => "org.jboss.naming:org.jnp.interfaces",

"java.naming.provider.url" => " jnp://yourflowers.com:1099");

$ctx = new Java("javax.naming.InitialContext", $envt);

// Try to find the object

$obj = $ctx->lookup("YourflowersBean");

// here we find an object - no error handling in this example

$rmi = new Java("javax.rmi.PortableRemoteObject");

$home = $rmi->narrow($obj, new Java("com.yourflowers.StoreHome"));

$store = $home->create();

// add an order to the bean

$store->place_order($_GET['client_id'], $_GET['item_id']);

print "Order placed.<br>Current shopping cart: <br>";

// get shopping cart data from the bean

$cart = $store->get_cart($_GET['client_id']);

foreach($cart as $item) {

print "$item['name']: $item['count'] at $item['price']<br>\n";

}

// release the object

$store->remove();

?>

 

The example code can be understood as follows:

  1. The code example is written in PHP and forms part of a PHP Web application.

  2. The PHP application first initializes an operation with the EJB, located at a specific URL that has the name:"//yourflowers.com:1099."

  3. The code then specifies the bean-YourflowersBean-that the application will look for.

  4. Next, the bean object is returned from the EJB server.

  5. The application then calls methods-in this case, the Java application includes two functions:

    • place_order receiving two numbers - client ID and the item ID to add to shopping cart
    • get_cart receiving one number - client ID and returning the list of the items placed in the shopping cart so far.

After script execution, the referenced class may be disposed.

 

Read Also

 

Java Bridge

   
 

Working with the Java Bridge