PHPHessian調用Java Spring Hessian

來源:互聯網
上載者:User

 
使用PHPHessian調用Java Spring Hessian暴露服務
收藏

<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部分:


1.
     [B]使用zend studio for eclipse 6.1
建立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;

?>

運行結果:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.