1. 程式人生 > >Hessian服務端和客戶端示例

Hessian服務端和客戶端示例

一、服務端

1、建立web專案,建立客戶端呼叫的hessian介面和實現類。

介面:

package com.ymx.hessian.service;

import com.ymx.hessian.service.entity.User;

public interface UserService {
    
    User getUser(String name);

}
View Code

實現類:

package com.ymx.hessian.service.impl;

import com.ymx.hessian.service.UserService;
import com.ymx.hessian.service.entity.User;

public class UserServiceImpl implements UserService{ @Override public User getUser(String name) { User user = new User(); user.setName(name); user.setAddress("南京"); return user; } }
View Code

傳輸物件,要實現序列化:

package com.ymx.hessian.service.entity;

import
java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = 5084037062770113475L; private String name; private String address; public String getName() { return name; } public void setName(String name) {
this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [name=" + name + ", address=" + address + "]"; } }
View Code

2、配置hessian服務端介面釋出的配置檔案。

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>hessianserver</display-name>
  <servlet>
    <servlet-name>HessianServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
                /WEB-INF/hessian-server.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>HessianServlet</servlet-name>
      <url-pattern>/hessian/service/*</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app>
View Code

配置hessian-server.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    
    <bean id="userServiceImpl" class="com.ymx.hessian.service.impl.UserServiceImpl" />
    
    <!-- 使用HessianServiceExporter 將普通bean匯出成Hessian服務 -->
    <bean name="/user"
        class="org.springframework.remoting.caucho.HessianServiceExporter">        
        <property name="service" ref="userServiceImpl" />
        <!-- Hessian服務的介面 -->
        <property name="serviceInterface" value="com.ymx.hessian.service.UserService" />
    </bean>

</beans>
View Code

 二、客戶端

呼叫前把服務端釋出介面和實體打成jar包提供給客戶端。

1、第一種呼叫服務端hessian方式

呼叫的url為:http://ip:port/工程名/web.xml中所配置的url-pattern/hessian-server.xml所配置的bean的name;

package com.ymx.client;

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.caucho.hessian.client.HessianProxyFactory;
import com.ymx.hessian.service.UserService;

public class Test {

    public static void main(String[] args) throws IOException {
        first();
    }
    
    public static void first() throws IOException{
        String url = "http://localhost:8080/hessianserver/hessian/service/user";
        HessianProxyFactory factory = new HessianProxyFactory();
        UserService userService = (UserService) factory.create(UserService.class, url);
        System.out.println(userService.getUser("jack"));
    }

}

2、第二種呼叫方式

建立hessian-client.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="hessianClient"
        class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl">
            <value>http://localhost:8080/hessianserver/hessian/service/user</value>
        </property>
        <property name="serviceInterface">
            <value>com.ymx.hessian.service.UserService</value>
        </property>
    </bean>

</beans>

java程式碼呼叫:

package com.ymx.client;

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.caucho.hessian.client.HessianProxyFactory;
import com.ymx.hessian.service.UserService;

public class Test {

    public static void main(String[] args) throws IOException {
        second();
    }
    
    public static void second() throws IOException{
        ApplicationContext context = new ClassPathXmlApplicationContext("hessian-client.xml");
        UserService userService = (UserService) context.getBean("hessianClient");
        System.out.println(userService.getUser("rose"));
    }

}

 第三種呼叫方式

 可以使用註解的方式把介面service注入到要呼叫介面的地方。

package com.ymx.client;

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.caucho.hessian.client.HessianProxyFactory;
import com.ymx.hessian.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

public class Test {
    
    @Autowired
    private UserService userService;

    public static void main(String[] args) throws IOException {
        third();
    }
    
    public static void third() throws IOException{
        System.out.println(userService.getUser("rose"));
    }

}
View Code