Hessian Tutorial

If you want to know more about what Hessian is, read this official documentation.

This tutorial will show you how to make a PHP client talk to a Java server via Hessian.
The PHP client can be easily switched to a .NET or jsp client.

Since the official documentation at caucho.com is good I will not spend a lot of time explaining everything.
Instead, I will focus on examples becuase that was what I really was missing when trying to implement Hessian for the first time.

Java Server
Assuming that you have all the server installations in place and downloaded the hessian dist from caucho, the first thing we are going to do is create a couple of empty files.

Java File Structure
Java file structure

Let’s begin with BasicExample.java. This is an interface, the interface which will be made available to the PHP client.
Let’s put some code in it.

BasicExample.java
package example;
public interface BasicExample
{
public String helloWorld();
}

Ok, moving on to the implementation, HessianBasicExample.java:

HessianBasicExample.java
package example;
public class HessianBasicExample implements BasicExample
{
private String anyString = "Hello World!";
public String helloWorld()
{
return this.anyString;
}
}

All done. This is all the java code we need for this to work. There is just one more thing to put into the java application: web.xml

web.xml
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>example.HessianBasicExample</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>example.BasicExample</param-value>
</init-param>
</servlet>
<servlet-mapping>
<url-pattern>/hello</url-pattern>
<servlet-name>hello</servlet-name>
</servlet-mapping>
</web-app>

What does this web.xml tell you? It quite simple but why dont you have a look at cauchos guide to web.xml for more information.

The quick explination is that when you call you java application and suffix with /hello (ie. http://localhost:8080/app_name/hello) Hessian will know what service you want.

Now that your java application is all done, build a .war file and put it in your server directory so you can run it.

PHP Client

The server is done but I said we were going to talk with it from a PHP Client.
This is even easier than building the java application.

First of all you need to download PHPHessian and put it on your include_path.
Secondly you will need one php file, let’s call hessianClient.php and fill it with some nice code.

hessianClient.php
<?php
require_once('HessianClient.php');
$sUrl = 'http://localhost:8080/app_name/';
$sRemoteService = '/hello';
$oProxy = &new HessianClient($sUrl . $sRemoteService);
$sHelloString = $oProxy->helloWorld();
echo $sHelloString;
?>

That’s all there is to it! Now you should be able to call your php file and you will see the string “Hello World” echoed out. Simple as pie.

A copule of things that are good to know:
1. When returning java objects from the server the java class must implement java.io.Serializable.
1.2. The return value to PHP will be an array representing the object.
2. PHPHessian did give me some trouble becuase it tries to overload the DateTime class. You have two options here:
a. Install the overload extension (see php.net for more information)
b. Rename the Hessian DateTime class. It a quick search and replace and works just fine, this is what I did.

That’s all for now, I really hope this will help someone out there.
Please send me feed back so that I can improve this tutorial :)

Write Comment

CAPTCHA image


Comment Preview


#

10 Most Recent Twits

Loading twits...