[Java]Hessian客户端和服务端代码例子

时间:2023-01-12 21:10:50

简要说明:这是一个比较简单的hessian客户端和服务端,主要实现从客户端发送指定的数据量到服务端,然后服务端在将接收到的数据原封不动返回到客户端。设计该hessian客户端和服务端的初衷是为了做一个转发系统的性能测试,通过利用该客户端和服务端来作为转发系统的测试脚本和测试服务端。同时,该hessian客户端和服务端也可作为简单的hessian例子进行学习。

1、客户端

工具类1:byte数组长度返回

package tool;

public class ByteActualLength {

public static int returnActualLength(byte[] data) {
        int i = 0;
        for (; i < data.length; i++) {
            if (data[i] == '\0')
                break;
        }
        return i;
    }

}

工具类2:将文件数据写入byte数组

package tool;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

public class FileWriteInByte {

@SuppressWarnings("resource")
    public static byte[] toByte(String filePath) throws IOException {

FileChannel fc = null;
        byte[] result = null;

try {
            fc = new RandomAccessFile(filePath, "r").getChannel();
            MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0,
                    fc.size()).load();
            System.out.println(byteBuffer.isLoaded());
            // 返回true代表传入为空;flase代表有数据
            result = new byte[(int) fc.size()];
            // System.out.println("返回数组长度为:"+fc.size());
            if (byteBuffer.remaining() > 0) {
                // System.out.println("remain");
                byteBuffer.get(result, 0, byteBuffer.remaining());
            }

} catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            fc.close();
        }
        return result;

}

}

测试客户端:

package test_nwwhl_client;

import java.io.IOException;
import java.net.MalformedURLException;
import service.DataTransmissionService;
import tool.ByteActualLength;
import tool.FileWriteInByte;

import com.caucho.hessian.client.HessianProxyFactory;

public class PerformanceTest {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
       
String url = "http://192.168.2.166:8088/nwwhlsystem_server/test";
        byte[] result=null;
        try {
            result = FileWriteInByte.toByte("D:\\test100.txt");
           
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HessianProxyFactory factory = new HessianProxyFactory();

DataTransmissionService DataTransmissionService=null;
       
        //服务调用开始时间
        long startTime=System.currentTimeMillis();
       
        try {
            DataTransmissionService = (DataTransmissionService) factory
                    .create(DataTransmissionService.class, url);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        byte[] data = DataTransmissionService.test(result);
       
        //服务调用结束时间
        long endTime=System.currentTimeMillis();
       
        System.out.println("程序运行时间: "+(endTime-startTime)+"ms");  
       
        int bytelength = ByteActualLength.returnActualLength(data);
        System.out.println("返回的数据长度为:" + bytelength);

}
}

2、服务端

第一个类:

package service;

public interface DataTransmissionService {
     public byte[] test(byte[] data);   
}

第二个类:

package service.impl;

import service.DataTransmissionService;

import com.caucho.hessian.server.HessianServlet;

@SuppressWarnings("serial")
public class DataTransmissionServiceImpl extends HessianServlet implements DataTransmissionService  {

public byte[] test(byte[] data) {
       
        return data;
    }
    
}