<1>Java 部分:
1.写Spring服务接口以及实现类,这里暂且叫为TadgetManager,TadgetManagerImpl。这些都是我自己实现的暴露
TadgetManager:
2.配置spring mvc和spring hessian组件,配置使用还是比较方便。暴露java服务:
1. spring 配置文件 tadgets-service.xml:
//这里是我们的tadgetManager接口服务实现类bean名称
<bean id="tadgetManager"
class="org.springframework.remoting.caucho.TadgetManagerimpl">
</bean>
//把我们的bean放到spring hessian服务bean把
<bean id="tadgetManagerService"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service">
<ref bean="tadgetManager" />
</property>
<property name="serviceInterface">
<value>com.luodongfu.test.service.TadgetManager</value>
</property>
</bean>
//对外服务请求拦截器,类似java sevlet类
<bean id="publicUrlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/ldfTest=tadgetManagerService
</value>
</property>
</bean>
2. web.xml:记得在我们的j2ee工程里面 web.xml配置请求URL拦截器,就是我们上面配置的publicUrlMapping
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/ ldfTest </url-pattern>
</servlet-mapping>
<2>PHP部分:
[B]3. 建立Hessian返回结果的php DO类:
[B]1. 建立Hessian返回结果的php DO类:
<一> DbRoute:DbRoute.class.php 分库技术使用到
class DbRoute
2. {
3. protected $userId;
4. protected $itemId;
5. protected $xid;
6. protected $routingStratery;
7.
8. public function getItemId()
9. {
10. return $this->itemId;
11. }
12. public function getRoutingStratery()
13. {
14. return $this->routingStratery;
15. }
.................................
BaseDO:BaseDO.class.php,对应com.taobao.common.dao.persistence.BaseDO的java类:
class BaseDO
{
protected $dbRoute;
function __construct()
{
$this->dbRoute = new DbRoute ( );
}
public function getDbRoute()
{
return $this->dbRoute;
}
//数组循环遍历设置dbRoute中各个属性值
public function setDbRoute($dbRoute)
{
foreach ( $dbRoute as $key => $value )
{
$this->dbRoute->__set( $key, $value );
}
}
}
BaseTadgetsDO:BaseTadgetsDO.class.php,对应com.luodongfu.test.domain. BaseTadgetsDO 的java类:
class BaseTadgetsDO extends BaseDO
{
protected $id;
[B]19. 省略各个get,set属性函数这里
protected $catalogId;
protected $tadgetCode;
protected $isvId;
protected $title;
protected $description;
protected $detailRef;
。。。。。。。。。。。。。。。。。。。。。。。。。。。
public function getAppkey()
{
return $this->appkey;
}
public function getCatalogId()
{
return $this->catalogId;
}
public function getDescription()
{
return $this->description;
}
省略各个get,set属性函数这里
TadgetsDO类,对应com.luodongfu.test. TadgetsDO的java类
class TadgetsDO extends BaseTadgetsDO
{
protected $isv;
public function __construct()
{
parent::__construct ();
}
public function getIsv()
{
return $this->isv;
}
public function setIsv($isv)
{
$this->$isv = $isv;
}
public function __set($propName, $propValue)
{
if ("dbRoute" == $propName && count ( $propValue ) > 0)
{
$this->setDbRoute ( $propValue );
} else
{
$this->$propName = $propValue;
}
}
public function __get($propName)
{
return $this->$propName;
}
public function __toString()
{
}
}
4.建立对phphessian客户端对Top服务封装,终于到我们的最重点考察类,呵呵,这里需要详细注释了:
LdfPhpHessianClient,LdfPhpHessianClient.class.php:
require_once 'lib/HessianClient.php';
class LdfPhpHessianClient
{
private $serviceUrl; //hessian服务地址URL
private $proxy; //HessianClient实例,反射机制代理类
private $result; // hessian结果
//构造函数,地球人都知道,呵呵
public function __construct($serviceUrl)
{
//检验hessian服务地址URL
$this->initUrl ( $serviceUrl );
//初始化HessianClient实例,注意这里的&传址调用哦,没有这个&就挂掉哦
$this->proxy = &new HessianClient ( $this->serviceUrl );
}
//小试一把,调用Top TadgetManager中getTadgetById服务来看看个究竟
public function getTadgetById($tadgetId)
{
//检查参数,初始化失败就抛出异常去
if (! isset ( $this->proxy ))
{
throw new NullPointException ( "The hessianClient object is not initial.", Constants::EXCEPTION_NULL_POINT );
}
//检查参数,Null值就异常
if (empty ( $tadgetId ) && ! is_numeric ( $tadgetId ))
{
throw new NullPointException ( "The parameter tadgetId is null or is not a number.", Constants::EXCEPTION_NULL_POINT );
}
//先把结果清空一下,如果是局部变量就不用了
unset($this->result);
//开始调用服务getTadgetById,可能出现异常哦,注意
try {
$this->result = $this->proxy->getTadgetById ( $tadgetId );
}catch(TopException $e)
{
//异常直接返回,打印简单日志
echo "TopException".$e->printStack();
return null;
}
//如果有结果,开始转换结果
if (isset($this->result))
{
//转换结果
return $this->convertObjectValues (new TadgetsDO(),$this->result );
}
return null;
}
//使用反射机制从服务端返回的数组中一个一个调用setXXXXX($obj)函数
private function convertObjectValues($returnObject, $valueObject)
{
//对返回数组的对象反射类
$object = new ReflectionClass ( $returnObject );
//遍历数组中的索引,找到TadgetsDO对应的setXXX($XX)函数
foreach ( $valueObject as $key => $value )
{
//得到setXXX函数名
$methodName = "set" . ucfirst ( $key );
//从TadgetsDO实例中反射一把看看有没有这个函数methodName
$method = $object->getMethod ( $methodName );
//没有该函数就直接返回
if (! $method)
{
continue;
}
//判断函数如果是public和非抽象则调用,和java反射机制一样,呵呵
if ($method->isPublic () && ! $method->isAbstract ())
{
$method->invoke ( $returnObject, $value );
}
}
//其实还有另外一种调用方法不用反射机制,如下调用
/* foreach ( $valueObject as $key => $value )
{
$returnObject->__set ( $key, $value );
}
不过你先要在TadgetsDO类中增加如下方法哦:
public function __set($propName, $propValue)
{
$this->$propName = $propValue;
}
*/
return $returnObject;
}
public function getServiceUrl()
{
return $this->serviceUrl;
}
public function setServiceUrl($serviceUrl)
{
$this->serviceUrl = $serviceUrl;
}
//判断tophessian服务url是否为空
private function initUrl($serviceUrl)
{
if (! isset ( $serviceUrl ))空抛异
{
throw new NullPointException ( "The hessian service url is null.", Constants::EXCEPTION_NULL_POINT );
}
$this->setServiceUrl ( $serviceUrl );
}
public function __autoload()
{
}
}
1. 开始看看我们的结果,终于熬到头了:
2. <?php
//引入我们自己建立类,类似java中的import或.net中的using用法
include_once 'LdfPhpHessianClient.class.php';
3. $testurl = 'http://127.0.0.1/top/tadgetTest';
//创建我们LdfPhpHessianClient实例
4. $LdfPhpHessianClient = new LdfPhpHessianClient ($testurl);
//小试我们的tadget
5. $result=$LdfPhpHessianClient->getTadgetById(22);
//打印tadget内部结果看看
6. print_r($result);
8. $LdfPhpHessianClient = null;
?>
运行结果:
1. [B]使用zend studio for eclipse 6.1建立php工程: