Spring中使用Hessian暴露服務


Spring Framework中有對RMI,Hessian,Burlap,JAX-RPC,JAX-WS.JMS的服務支持,更方便的用於開發異構的服務系統,自身也有HTTPinvoker技術提供遠端服務.

以下示例來自SpringFramework-doc文檔, 我們先建立用於測試的實體、服務接口類:

public class Account implements Serializable{

    private String name;

    public String getName(){

        return name;

    }

    public void setName(String name) {

      this.name = name;

    }

}

public interface AccountService {

    public void insertAccount(Account account);

    public List<Account> getAccounts(String name);

}

// 服務的具體實現類

public class AccountServiceImpl implements AccountService {

    public void insertAccount(Account acc) {

        // do something...

    }

  

    public List<Account> getAccounts(String name) {

        // do something...

    }

}

下面我們就用caucho公司的hessian技術來提供一個基於HTTP的服務:

首先需要下載hessian包:http://www.caucho.com.

然后配置web.xml

<servlet>

    <servlet-name>remoting</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

    <servlet-name>remoting</servlet-name>

    <url-pattern>/remoting/*</url-pattern>

</servlet-mapping>

建立spring xml 配置remoting-servlet.xml通過Spring包裝一個接口為hessian服務:

<bean id="accountService" class="example.AccountServiceImpl">

    <!-- any additional properties, maybe a DAO? -->

</bean>

<bean name="/AccountService" class="org.springframework.remoting.caucho.HessianServiceExporter">

    <property name="service" ref="accountService"/>

    <property name="serviceInterface" value="example.AccountService"/>

</bean>

這樣 hessian的服務就通過'http://HOST:8080/remoting/AccountService'.來暴露給客戶端調用

在客戶端中調用

通過Spring來配置測試BEAN

public class SimpleObject {

    private AccountService accountService;

    public void setAccountService(AccountService accountService) {

        this.accountService = accountService;

    }

    // additional methods using the accountService

}

<bean class="example.SimpleObject">

    <property name="accountService" ref="accountService"/>

</bean>

<bean id="accountService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">

    <property name="serviceUrl" value="http://remotehost:8080/remoting/AccountService"/>

    <property name="serviceInterface" value="example.AccountService"/>

</bean>

servicePort屬性被省略了(默認為0)。這意味着匿名端口將用於與服務通信。

相關閱讀:

Spring3 RMI 使用介紹

PHP調用Hessian Java 遠端WEB服務


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM