What is Hessian?
Hessian is an open-source remote communication protocol provided by caucho.
The binary RPC protocol is used for HTTP transmission. The server does not need to open any firewall port.
The protocol specification is public and can be used in any language.
Use the Client/Server mode.
A request is a client, and a service provider is a server.
The client calls the process to send a call with process parameters to the service process, and then waits for the response information.
On the server side, the process remains asleep until the call information reaches.
When a call Information arrives, the server obtains the process parameters, calculates the results, sends the reply information, and waits for the next call Information. Finally, the client calls the process to receive the reply information,
Obtain the process results, and then call the execution to continue.
Hessian protocol Workflow
Client Requests Server Functions
1. Call the client handle to execute the transfer parameters.
2. Call the local system kernel to send network messages.
3. Send messages to the remote host.
4. The server handle obtains the message and obtains parameters.
5. Execute the remote process.
The server function returns the result to the client.
1. the execution process returns the result to the server handle.
2. The server handle returns the result and calls the remote system kernel.
3. The message is sent back to the local host.
4. The client handle is used by the kernel to receive messages.
5. The customer receives the data returned by the handle.
Source code
1. reference the configuration file, including the website root directory and the Hessian address.
Copy codeThe Code is as follows:
<? Php
/**
* File name: config. php
* Purpose: Hessian configuration file
*
* @ Package system. core. code applied to the whole site
* @ Copyright Copyright (c) 2012
* @ Since 1.0
*/
// Root directory
Define ('path', dirname (_ FILE _). DIRECTORY_SEPARATOR );
// Hessian Url
Define ('hessian _ url', 'HTTP: // qx.com/server.php ');
// IDE: Zend Studio 9.0
// IDE Extension: Toggle Vrapper
?>
2. Configure the server.
Copy codeThe Code is as follows:
<? Php
/**
* File name: server. php
*
* References:
* 1. http://hessian.caucho.com/(Hessian homepage)
* 2. http://hessianphp.sourceforge.net/(Hessian PHP)
* 3. http://sourceforge.net/projects/hessianphp/ (Hessian PHP open source)
* 4. http://baike.baidu.com/view/1859857.htm (Singleton Mode)
*
* @ Author wu1_qing <xinxiangmo@gmail.com>
* @ Package system. core applied to the whole site
* @ Copyright Copyright (c) 2012
* @ Since 1.0
*/
Require_once (dirname (_ FILE _). DIRECTORY_SEPARATOR. 'config. php ');
Require_once (PATH. 'Extensions/HessianPHP/HessianService. php ');
Class HessianServer
{
Public function _ construct (){}
/**
* Product details APi Interface
* @ Param string $ title
* @ Param int $ price
*/
Public function goodsInfomationApi ($ title, $ price ){
$ Price = (int) $ price;
Return '
}
}
$ Server = new HessianService (new HessianServer ());
// $ Server-> displayInfo ();
$ Server-> handle ();
// IDE: Zend Studio 9.0
// IDE Extension: Toggle Vrapper
?>
3. You can use the displayInfo method in the HessianService class to view how many communication methods are enabled.
If you need to use the handle method to set up the server, if the prompt "Hessian Requires POST" appears, the server has been set up successfully.
4. encapsulate the Hessian Interface
Copy codeThe Code is as follows:
<? Php
/**
* Class Name: HessianApi
*
* References:
* 1. http://hessian.caucho.com/(Hessian homepage)
* 2. http://hessianphp.sourceforge.net/(Hessian PHP)
* 3. http://sourceforge.net/projects/hessianphp/ (Hessian PHP open source)
* 4. http://baike.baidu.com/view/1859857.htm (Singleton Mode)
*
* @ Author wu1_qing <xinxiangmo@gmail.com>
* @ Package system. core applied to the whole site
* @ Copyright Copyright (c) 2012
* @ Since 1.0
*/
Class HessianApi
{
/**
* @ Var string interface address
*/
Private $ _ url = NULL;
/**
* @ Var result handle
*/
Private $ _ handle = NULL;
/**
* @ Var array stores the singleton mode array
*/
Private static $ _ objects = array ();
/**
* Set the URL address
* Instantiate the HessianClient class
* Parameter: (1) url, 2
*
* 2. Java call Field
* @ Param string $ url
*/
Public function _ construct ($ url)
{
$ This-> setUrl ($ url );
$ Handler = new HessianClient ($ this-> getUrl (), $ this-> getOptions ());
$ This-> setHandler ($ handler );
}
/**
* @ Return result $ _ handle
*/
Public function getHandler (){
Return $ this-> _ handle;
}
/**
* Set the handle
* @ Param result $ _ handle
*/
Public function setHandler ($ _ handle ){
$ This-> _ handle = $ _ handle;
}
/**
* Obtain the URL
*/
Public function getUrl (){
Return $ this-> _ url;
}
/**
* Set the URL address
* @ Param string $ url
*/
Public function setUrl ($ url ){
$ This-> _ url = $ url;
}
/**
* TypeMap maps platform objects such as Java
* @ Return array
*/
Public function getOptions (){
Return array (
'Version' => 1,
'Saveraw' => TRUE,
'Typemap' => array (
'Javanullpointexception '=> 'java. lang. nullpointerexception ',
'Stacktraceelement' => 'java. lang. stacktraceelement ')
);
}
/**
* Record interface call Information
* @ Param string $ method call method
* @ Param string $ returnMsg the text information to be recorded in the log
*/
Public function resultLog ($ method, $ returnMsg)
{
$ LogPath = PATH. '/runtime/hessian /';
If (! Is_dir ($ logPath )){
Mkdir ($ logPath, 0777 );
}
Error_log (date ('ymd H: I: s', time ()). '| '. $ method. '| '. $ returnMsg. "\ n", 3, $ logPath. date ('Y-m-d', time ()). '. log ');
}
/**
* The static factory method generates a unique instance of a single URL
* @ Param string $ url
*/
Public static function start ($ url)
{
$ Key = md5 ($ url );
If (isset (self ::$ _ objects [$ key]) {
Return self: $ _ objects [$ key];
}
Self: $ _ objects [$ key] = new HessianApi ($ url );
Return self: $ _ objects [$ key];
}
}
Class JavaNullPointException extends Exception {}
Class StackTraceElement extends Exception {}
// IDE: Zend Studio 9.0
// IDE Extension: Toggle Vrapper
?>
5. encapsulate the client request method and inherit the HessianApi class
Copy codeThe Code is as follows:
<? Php
/**
* Class Name: Goods
* Inheritance class: HessianApi
* Purpose: Call the server. php method.
*
* @ Author wu1_qing <xinxiangmo@gmail.com>
* @ Package system. core. code applied to the whole site
* @ Copyright Copyright (c) 2012
* @ Since 1.0
*/
Class Goods extends HessianApi
{
/**
* Set the interface address
* @ Param string $ url
*/
Public function _ construct ($ url ){
Parent: :__ construct ($ url );
}
/**
* Obtain product information
* Call the goodsInfomationApi method in the server. php file
* @ Param string $ title
* @ Param string $ title price
*/
Public function getGoodsInfomation ($ title, $ price)
{
// If you call the hessian service on the java platform, you must specify the type of parameters you want to pass, especially integer and string types.
$ Price = (int) $ price;
$ Result = $ this-> getHandler ()-> goodsInfomationApi ($ title, $ price );
$ This-> resultLog ('getgoodsinfomation ', 'Access interface, but the interface does not perform logic verification .');
Return $ result;
}
}
// IDE: Zend Studio 9.0
// IDE Extension: Toggle Vrapper
?>
6. Modify index. php to request the server interface
Copy codeThe Code is as follows:
<? Php
/**
* File name: index. php
*
* References:
* 1. http://hessian.caucho.com/(Hessian homepage)
* 2. http://hessianphp.sourceforge.net/(Hessian PHP)
* 3. http://sourceforge.net/projects/hessianphp/ (Hessian PHP open source)
* 4. http://baike.baidu.com/view/1859857.htm (Singleton Mode)
*
* @ Author wu1_qing <xinxiangmo@gmail.com>
* @ Package system. core applied to the whole site
* @ Copyright Copyright (c) 2012
* @ Since 1.0
*/
Require_once (dirname (_ FILE _). DIRECTORY_SEPARATOR. 'config. php ');
// Hessian extension and configuration file
Require_once (PATH. 'Extensions/HessianPHP/HessianClient. php ');
Require_once (PATH. 'class/HessianApi. php ');
// Call the server. php Method
Require_once (PATH. 'class/Goods. php ');
// Request the interface to obtain data
$ Goods = new Goods (HESSIAN_URL );
// Set the product title and price.
$ Title = 'Beijing mobile recharge platform ';
$ Price = '50 ';
// Request the Hessian Protocol
$ GoodsInfo = $ goods-> getGoodsInfomation (string) $ title, (int) $ price );
// Print the request result
Echo ($ goodsInfo );
// IDE: Zend Studio 9.0
// IDE Extension: Toggle Vrapper
?>