The Passionate Craftsman

Ruby, PHP, MySql, Software engineering and more.

Friday, 5 June 2009

Use Hazelcast library in PHP

If you want the power of Java in PHP, easily, the answer is Quercus / PHP. Quercus is a 100% implementation of PHP in Java. I have downloaed the GPL version of Resin, unzipped in C:\. The file to start the server is httpd.exe.

In "C:\resin-3.1.9\webapps\ROOT\WEB-INF" you will find "resin-web.xml", the configuration file of Resin. Copy the following text:


Than create this file "hello-world.php" in "C:\resin-3.1.9\webapps\ROOT" and copy this text:

echo "Hello World";

Now you should be able to see the script executed: http://localhost:8080/hello-world.php

Go to the Hazelcast web site and download the zip. Inside the zip copy "hazelcast.jar" in "C:\resin-3.1.9\webapps\ROOT\WEB-INF\lib", create the lib directory if does not exist.

Create another php file, say "htest.php". Copy the code:

import com.hazelcast.core.Hazelcast;
import java.util.Map;
import java.util.Collection;
require_once './Customer.php';

$mapCustomers = Hazelcast::getMap("customers");
$mapCustomers->put("1", new Customer("Joe", "Smith"));
$mapCustomers->put("2", new Customer("Ali", "Selam"));
$mapCustomers->put("3", new Customer("Avi", "Noyan"));

$colCustomers = $mapCustomers->values();
foreach ($mapCustomers as $cus) {
echo $cus->getName().'

In "htest.php" you call the Hazelcast library, get an instance of a Map, a distributed Map! You put 3 new object into the map and than you print its property. To run this example you have to create the "Customer" class in PHP. In the same directory create "Customer.php" and copy this code:

class Customer {
protected $strName;

public function __construct($strName) {
$this->strName = $strName;

public function getName() {
return $this->strName;

Tahn run the example: http://localhost:8080/htest.php

You should get this output:



Hazelcast docs
Infoq: PHP on Java: Best of Both Worlds?
Use Java to Improve Drupal's Scalability


Post a Comment

Subscribe to Post Comments [Atom]

<< Home