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

 

posted @ 2017-05-15 13:55  吉良吉影的冒险  阅读(483)  评论(0编辑  收藏  举报