Skip to content

Setting up PHP 5 + Gearman + Java on Ubuntu Linux 9.10 (Karmic)

April 10, 2010

Bellow are stops to setup a running Gearman based job server which can be used to have various work-distribution clusters especially creating an interoperability between your web layer (esp PHP5) and backing business layer (esp. in Java).

Steps:
Prepare Gearman job server

You may decide to get the source and compile or use Ubunut’s PPA repositories to get them pre-built. I go with the second and added this repository ( deb
http://ppa.launchpad.net/gearman-developers/ppa/ubuntu karmic main ) to my /etc/apt/source.list .

Install Job Server

sudo aptitude update
sudo aptitude install gearman-job-server

Prepare PHP environment

sudo aptitude install uuid-dev php5-dev
sudo pecl install channel://pecl.php.net/gearman-0.7.0

And also add extension=gearman.so to both:

  • /etc/php5/apache2/php.ini
  • /etc/php5/cli/php.ini

Check Gearman PHP extension
Get phpinfo() and see if gearman exists there

<?php
phpinfo();
?>

Setup Java/Gearman Binding

Get the project from https://launchpad.net/gearman-java

Startup Gearmand

sudo /etc/init.d/gearman-job-server start

Sample Java based Worker

import org.gearman.client.*;
import org.gearman.common.GearmanJobServerConnection;
import org.gearman.common.GearmanNIOJobServerConnection;
import org.gearman.worker.AbstractGearmanFunction;
import org.gearman.worker.GearmanWorker;
import org.gearman.worker.GearmanWorkerImpl;

public class SampleGearmanWorker {

  public static void main(String[] args) {
    final GearmanJobServerConnection connection = new
    GearmanNIOJobServerConnection("localhost", 4730);

    GearmanWorker worker = new GearmanWorkerImpl();
    worker.addServer(connection);

    worker.registerFunction(SampleFunction.class);

    worker.work();
  }

  private static final byte[] EMPTY_BYTES = new byte[]{};

  public static class SampleFunction extends AbstractGearmanFunction {

  @Override
  public String getName() {
    return "test";
  }

  @Override
  public GearmanJobResult executeFunction() {
    final byte[] hello = "hello ".getBytes();
    final byte[] name = (byte[]) this.data;
    final byte[] resp = new byte[hello.length + name.length];

    System.arraycopy(hello, 0, resp, 0, hello.length);
    System.arraycopy(name, 0, resp, hello.length, name.length);

    final GearmanJobResult jr = new
        GearmanJobResultImpl(this.jobHandle, true, resp, EMPTY_BYTES, EMPTY_BYTES, 0, 0);

    return jr;
    }
  }
}

Sample PHP Client

<?php
$client= new GearmanClient();
$client->addServer();
print $client->do("test", "amin")."\n";
?>

Run the example

run Java code and also php gearman-client.php. It will print “hello amin” :)

Hope it helps!

Advertisements
7 Comments
  1. very informative..thanks a lot..james
    javajobs.net

  2. AlexT permalink

    For some reasons, Ubuntu installed libgearman2… And

    pecl install channel://pecl.php.net/gearman-0.7.0

    fails with

    checking whether to enable gearman support… yes, shared
    not found
    configure: error: Please install libgearman
    ERROR: `/tmp/pear/temp/gearman/configure’ failed

    :(

  3. jose permalink

    you need
    sudo aptitude install php-pear
    otherwise sudo pecl does not work

    after that I am stock with the same error as Alex
    configure: error: Please install libgearman
    ERROR: `/tmp/pear/temp/gearman/configure’ failed

  4. If you are running Ubuntu 10.04, this should get you up and running:

    EDIT: /etc/apt/source.list and add the following:
    deb http://ppa.launchpad.net/gearman-developers/ppa/ubuntu lucid main

    sudo apt-key adv –recv-keys –keyserver keyserver.ubuntu.com
    sudo apt-get update
    sudo apt-get install gearman-job-server libgearman2 libgearman-dev uuid-dev php5-dev libgearman-dev
    sudo pecl install channel://pecl.php.net/gearman-0.7.0

  5. Luke permalink

    @wilmoore

    After the “apt-get install” I get the following error (I’m on 10.04):

    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    uuid-dev is already the newest version.
    php5-dev is already the newest version.
    Some packages could not be installed. This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The following information may help to resolve the situation:

    The following packages have unmet dependencies:
    gearman-job-server: Depends: libdrizzle1 (>= 2010.10.01) but it is not installable
    E: Broken packages

  6. @Luke:

    =========================================================
    Install Drizzle (Gearman Dependency)
    =========================================================

    1. Add the signing key to your keyring:
    > sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys 06899068
    or see 1a. (especially if dealing with an outbound proxy server)

    2. Add the PPA:
    > sudo sh -c ‘echo “deb http://ppa.launchpad.net/drizzle-developers/ppa/ubuntu lucid main” > /etc/apt/sources.list.d/drizzle.list’

    3. Update the APT cache
    > sudo apt-get update

    4. Install drizzle-dev (retrieves libdrizzle1)
    > sudo apt-get install -y drizzle

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: