简要说明:这是一个比较简单的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;
    }
    
}

最新文章

  1. Gulp 入门
  2. Windows Server 2008 R2 组策略基本设置
  3. win7 ins 30131 oracle 12c
  4. php大力力 [038节] 全栈工程师的含义
  5. Verilog HDL那些事_建模篇笔记(实验一,实验二)
  6. Codeforces723E One-Way Reform【欧拉回路】
  7. HDU 1248 冰封王座(dp)
  8. centos修改SSH端口并禁用root远程登录
  9. 美图App的移动端DNS优化实践:HTTPS请求耗时减小近半
  10. Java开发笔记(六十五)集合:HashSet和TreeSet
  11. JavaScript中函数立即调用的写法
  12. HBase简介及原理
  13. Repair the Wall
  14. Scala断言
  15. 【BZOJ】3573: [Hnoi2014]米特运输
  16. 通过putty进行端口映射并且启动jupyter notebook
  17. 修改oralce数据库用户名和密码
  18. 使用cglib实现数据库框架的级联查询
  19. 如何使用 grunt
  20. elementUI和iview兼容么

热门文章

  1. PHP程序的常见漏洞攻击分析
  2. 2016暑假多校联合---Another Meaning
  3. HTTP 方法:GET 对比 POST
  4. 一个完整的类用来读取OpenSSL生成的pem格式的x509证书
  5. java 多态奇怪现象,子类还没有构造完成就开始干活了,谁来帮我解释?
  6. 模拟Executor策略的实现
  7. ajax的再次封装!(改进版) —— new与不 new 有啥区别?
  8. 选中没有选中的复选框,匹配含有某个字符串的正则,json取值的两种方法,把变量定义在外面跟里面的区别
  9. 免费 PSD 下载: 20个精美的登录和注册表单
  10. 轻松掌握:JavaScript享元模式