`
lobin
  • 浏览: 417714 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Hessian

 
阅读更多

Hessian

 

基于hessian, Burlap协议调用java方法

一个最简单的例子

基于hessian协议调用java方法

先定义一个接口:

public interface TestService {

    public void test(String name);

}

 

实现这个接口:

public class TestServiceImpl implements TestService {

    @Override

    public void test(String name) {

        System.out.println("test:" + name);

    }

}

 

基于hessian模拟rpc怎么调用呢?

 

 

public class HessianSkeletonTest {

 

private static TestServiceImpl testService;

 

private static HessianSkeleton skeleton;

 

@BeforeClass

public static void initialize() {

testService = new TestServiceImpl();

 

skeleton = new HessianSkeleton(testService, TestService.class);

}

 

/**

 * m 0x00 0x04 test

 * S 0x00 0x0A helloworld

 * z

 * 

 * @throws IOException

 */

@Test

public void invoke() throws IOException {

ByteArrayOutputStream bos = new ByteArrayOutputStream();

 

// m 0x00 0x04 test

bos.write('m');

bos.write(0x00);

bos.write(0x04);

bos.write("test".getBytes());

 

// S 0x00 0x0A helloworld

String s = "helloworld";

int length = 10;

bos.write('S');

length = (length     <<           16)              >>>           16;

bos.write(length           >>>                8);

bos.write((length      <<     8)            >>>                8);

bos.write(s.getBytes());

 

// z

bos.write('z');

 

InputStream isToUse = new ByteArrayInputStream(bos.toByteArray());

OutputStream osToUse = new ByteArrayOutputStream();

 

AbstractHessianInput in = new HessianInput(isToUse);

AbstractHessianOutput out = new HessianOutput(osToUse);

try {

skeleton.invoke(in, out);

} catch (Throwable e) {

e.printStackTrace();

}

}

}

 

运行输出:

test:helloworld

 

 

基于hessian协议调用java方法-一个map例子

基于hessian协议调用java方法-一个map例子

 

map

map ::= M t b16 b8 type-string (object, object)* z

 

Map类型,同时支持对象Object。

type描述map类型,可为空

如果是对象,type表示类全名

 

 

先定义一个接口:

 

 

public interface TestService {

 

public void testMap(Map<String, Object> map);

 

}

 

实现这个接口:

 

public class TestServiceImpl implements TestService {

 

 

public void testMap(Map<String, Object> map) {

if (map != null) {

for (Entry<String, Object> entry : map.entrySet()) {

System.out.println("key: " + entry.getKey());

System.out.println("value: " + entry.getValue());

}

} else {

System.out.println("null");

}

}

 

}

 

基于hessian模拟rpc怎么调用呢?

 

public class HessianSkeletonTest {

 

private static TestServiceImpl testService;

 

private static HessianSkeleton skeleton;

 

@BeforeClass

public static void initialize() {

testService = new TestServiceImpl();

 

skeleton = new HessianSkeleton(testService, TestService.class);

}

 

 

/**

* m 0x00 0x07 testMap

* M

* S 0x00 0x04 name

* S 0x00 0x06 whoami

* S 0x00 0x03 age

* I 0x00 0x00 0x02 0x0C #0000 0000 0010 1100 #44

* S 0x00 0x04 male

* T

* z #end of map

* z #end of method

* @throws IOException

*/

@Test

public void invokeTestMap() throws IOException {

ByteArrayOutputStream bos = new ByteArrayOutputStream();

 

String skey = null;

String svalue = null;

int ivalue = 0;

boolean bvalue = false;

 

// m 0x00 0x07 testMap

bos.write('m');

bos.write(0x00);

bos.write(0x07);

bos.write("testMap".getBytes());

 

// M

bos.write('M');

// S 0x00 0x04 name

bos.write('S');

skey = "name";

int length = 4;

length = (length << 16) >>> 16;

bos.write(length >>> 8);

bos.write((length << 8) >>> 8);

bos.write(skey.getBytes());

 

 

// S 0x00 0x06 whoami

bos.write('S');

svalue = "whoami";

length = 6;

length = (length << 16) >>> 16;

bos.write(length >>> 8);

bos.write((length << 8) >>> 8);

bos.write(svalue.getBytes());

 

 

// S 0x00 0x03 age

bos.write('S');

skey = "age";

length = 3;

length = (length << 16) >>> 16;

bos.write(length >>> 8);

bos.write((length << 8) >>> 8);

bos.write(skey.getBytes());

 

 

// I 0x00 0x00 0x02 0x0C #0000 0000 0000 0000 0000 0000 0010 1100 #44

bos.write('I');

ivalue = 44;

bos.write(ivalue >>> 24);

 

ivalue = (ivalue << 8) >>> 8;

bos.write(ivalue >>> 16);

 

ivalue = (ivalue << 8) >>> 8;

bos.write(ivalue >>> 8);

 

ivalue = (ivalue << 8) >>> 8;

bos.write(ivalue);

 

 

// S 0x00 0x04 male

bos.write('S');

skey = "male";

length = 4;

length = (length << 16) >>> 16;

bos.write(length >>> 8);

bos.write((length << 8) >>> 8);

bos.write(skey.getBytes());

 

 

// T

bvalue = true;

bos.write(bvalue ? 'T' : 'F');

 

// z

bos.write('z');

 

 

// z

bos.write('z');

 

InputStream isToUse = new ByteArrayInputStream(bos.toByteArray());

OutputStream osToUse = new ByteArrayOutputStream();

 

AbstractHessianInput in = new HessianInput(isToUse);

AbstractHessianOutput out = new HessianOutput(osToUse);

try {

skeleton.invoke(in, out);

} catch (Throwable e) {

e.printStackTrace();

}

}

 

}

 

 

 

m 0x00 0x07 testMap

M

S 0x00 0x04 name

S 0x00 0x06 whoami

S 0x00 0x03 age

I 0x00 0x00 0x02 0x0C #0000 0000 0010 1100 #44

S 0x00 0x04 male

T

z #end of map

z #end of method

 

 

 

 

 

运行输出:

 

key: name

value: whoami

key: age

value: 44

key: male

value: true

 

基于Burlap协议调用java方法

public interface TestService {
    public void test(String name);
}

 

public class TestServiceImpl implements TestService {
    @Override
    public void test(String name) {
        System.out.println("test:" + name);
    }
}

 

public class BurlapSkeletonTest {

	private static TestServiceImpl testService;

	private static BurlapSkeleton skeleton;

	@BeforeClass
	public static void initialize() {
		testService = new TestServiceImpl();

		skeleton = new BurlapSkeleton(testService, TestService.class);
	}
	
	private Document initBurlapRequest() throws BurlapException {
		DocumentBuilder domBuilder = null;
		try {
			domBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			throw new BurlapException(e);
		}
		return domBuilder.newDocument();
	}
	
	private InputStream getBurlapInputStream(Document doc) throws BurlapException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		
		Transformer transformer = null;
		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			transformer = transFactory.newTransformer();
		} catch (Exception e) {
			throw new BurlapException(e);
		}
		
		transformer.setOutputProperty("encoding", "gb2312");
		transformer.setOutputProperty("indent", "yes");
		
		// omit xml declaration
		// <?xml version="1.0" encoding="GB2312" standalone="no"?>
		transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
		
		
		DOMSource source = new DOMSource();
		source.setNode(doc);
		StreamResult result = new StreamResult();
		result.setOutputStream(bos);
		try {
			transformer.transform(source, result);
		} catch (Exception e) {
			throw new BurlapException(e);
		}
		
		System.out.println(new String(bos.toByteArray()));

		return new ByteArrayInputStream(bos.toByteArray());
	}

	/**
	 * <burlap:call>
	 *   <method>test</method>
	 *   <string>helloworld</string>
	 * </burlap:call>
	 * 
	 * @throws IOException
	 */
	@Test
	public void invoke() throws IOException {
		Document doc = null;
		try {
			doc = initBurlapRequest();
		} catch (BurlapException e) {
			throw new IOException(e);
		}
		
		Element root = doc.createElement("burlap:call");
		
		Element element = doc.createElement("method");
		element.setTextContent("test");
		root.appendChild(element);
		
		element = doc.createElement("string");
		element.setTextContent("helloworld");
		root.appendChild(element);
		
		doc.appendChild(root);


		InputStream isToUse;
		try {
			isToUse = getBurlapInputStream(doc);
		} catch (BurlapException e) {
			throw new IOException(e);
		}
		OutputStream osToUse = new ByteArrayOutputStream();

		BurlapInput in = new BurlapInput(isToUse);
		BurlapOutput out = new BurlapOutput(osToUse);
		try {
			skeleton.invoke(in, out);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
}

 

 

Hessian (RPC)序列化:对象类型(Java)

要实现一个远程方法调用,最基本的是要考虑调用方法的序列化,包括方法名,传参(参数类型以及传值),返回类型以及返回值。

 

方法名很简单,就是个字符串,传参和返回就比较麻烦。

 

传参和返回有类型,光这些类型表示就比较多,尤其是对象类型,特别是复杂对象类型。

 

Hessian针对对象的序列化不同于Java,对象的序列化形式如下:

map

map ::= M t b16 b8 type-string (object, object)* z

 

Map类型,同时支持对象Object。

type描述map类型,可为空

如果是对象,type表示类全名

 

 

 

 基于Hessian RPC序列化(协议)调用java方法: 针对对象(Java)类型序列化的例子。

 在这个例子中,仅根据Hessian RPC序列化模拟一个本地Java方法的调用,并不实现远程调用,和调用本地方法效果是一样的。

 

 

 

先定义一个接口:

 

package com.chos.test.service;
 
public class Block {
 
    private long id;
 
    private int account;
 
    /**
     * expenses and receipts
     * 
     * 收入还是支出
     */
    private boolean receipt;
 
    private String signature;
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public int getAccount() {
        return account;
    }
 
    public void setAccount(int account) {
        this.account = account;
    }
 
    public boolean isReceipt() {
        return receipt;
    }
 
    public void setReceipt(boolean receipt) {
        this.receipt = receipt;
    }
 
    public String getSignature() {
        return signature;
    }
 
    public void setSignature(String signature) {
        this.signature = signature;
    }
}

 

public interface TestService {
 
    public void testObjectBlock(Block block);
 
}

 

 

实现这个接口:

public class TestServiceImpl implements TestService {
 
    public void testObjectBlock(Block block) {
        System.out.println(ToStringBuilder.reflectionToString(block, ToStringStyle.MULTI_LINE_STYLE));
    }
 
}

 

 

基于hessian模拟rpc怎么调用呢?

 

public class HessianSkeletonTest {

 

private static TestServiceImpl testService;

 

private static HessianSkeleton skeleton;

 

@BeforeClass

public static void initialize() {

testService = new TestServiceImpl();

 

skeleton = new HessianSkeleton(testService, TestService.class);

}

 

 

@Test

public void invokeTestObjectBlock() throws IOException {

ByteArrayOutputStream bos = new ByteArrayOutputStream();

 

// m 0x00 0x0F testObjectBlock

bos.write('m');

bos.write(0x00);

bos.write(0x0F);

bos.write("testObjectBlock".getBytes());

 

// M

bos.write('M');

 

// t x00 x1B com.chos.test.service.Block

bos.write('t');

String className = Block.class.getName();

int classNameLength = 27;

classNameLength = (classNameLength << 16) >>> 16;

bos.write(classNameLength >>> 8);

bos.write((classNameLength << 8) >>> 8);

bos.write(className.getBytes());

 

 

// S 0x00 0x02 id

bos.write('S');

String fieldId = "id";

int fieldIdLength = 2;

fieldIdLength = (fieldIdLength << 16) >>> 16;

bos.write(fieldIdLength >>> 8);

bos.write((fieldIdLength << 8) >>> 8);

bos.write(fieldId.getBytes());

 

 

// L 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x0C #0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0010 1100 #44

bos.write('L');

long fieldIdValue = 44;

bos.write((int) (fieldIdValue >>> 56));

 

fieldIdValue = (fieldIdValue << 8) >>> 8;

bos.write((int) (fieldIdValue >>> 48));

 

fieldIdValue = (fieldIdValue << 8) >>> 8;

bos.write((int) (fieldIdValue >>> 40));

 

fieldIdValue = (fieldIdValue << 8) >>> 8;

bos.write((int) (fieldIdValue >>> 32));

 

fieldIdValue = (fieldIdValue << 8) >>> 8;

bos.write((int) (fieldIdValue >>> 24));

 

fieldIdValue = (fieldIdValue << 8) >>> 8;

bos.write((int) (fieldIdValue >>> 16));

 

fieldIdValue = (fieldIdValue << 8) >>> 8;

bos.write((int) (fieldIdValue >>> 8));

 

fieldIdValue = (fieldIdValue << 8) >>> 8;

bos.write((int) fieldIdValue);

 

// S 0x00 0x07 account

bos.write('S');

String fieldAccount = "account";

int fieldAccountLength = 7;

fieldAccountLength = (fieldAccountLength << 16) >>> 16;

bos.write(fieldAccountLength >>> 8);

bos.write((fieldAccountLength << 8) >>> 8);

bos.write(fieldAccount.getBytes());

 

 

// I 0x00 0x00 0x04 0xD2 #0000 0000 0000 0000 0000 0100 1101 0010 #1234

bos.write('I');

int fieldAccountValue = 1234;

bos.write(fieldAccountValue >>> 24);

 

fieldAccountValue = (fieldAccountValue << 8) >>> 8;

bos.write(fieldAccountValue >>> 16);

 

fieldAccountValue = (fieldAccountValue << 8) >>> 8;

bos.write(fieldAccountValue >>> 8);

 

fieldAccountValue = (fieldAccountValue << 8) >>> 8;

bos.write(fieldAccountValue);

 

 

// S 0x00 0x07 receipt

bos.write('S');

String fieldReceipt = "receipt";

int fieldReceiptLength = 7;

fieldReceiptLength = (fieldReceiptLength << 16) >>> 16;

bos.write(fieldReceiptLength >>> 8);

bos.write((fieldReceiptLength << 8) >>> 8);

bos.write(fieldReceipt.getBytes());

 

 

// T

boolean fieldReceiptValue = true;

bos.write(fieldReceiptValue ? 'T' : 'F');

 

// S 0x00 0x09 signature

bos.write('S');

String fieldSignature = "signature";

int fieldSignatureLength = 9;

fieldSignatureLength = (fieldSignatureLength << 16) >>> 16;

bos.write(fieldSignatureLength >>> 8);

bos.write((fieldSignatureLength << 8) >>> 8);

bos.write(fieldSignature.getBytes());

 

 

// S 0x00 0x0F signature value

bos.write('S');

String fieldSignatureValue = "signature value";

int fieldSignatureValueLength = 15;

fieldSignatureValueLength = (fieldSignatureValueLength << 16) >>> 16;

bos.write(fieldSignatureValueLength >>> 8);

bos.write((fieldSignatureValueLength << 8) >>> 8);

bos.write(fieldSignatureValue.getBytes());

 

// z

bos.write('z');

 

// z

bos.write('z');

 

InputStream isToUse = new ByteArrayInputStream(bos.toByteArray());

OutputStream osToUse = new ByteArrayOutputStream();

 

AbstractHessianInput in = new HessianInput(isToUse);

AbstractHessianOutput out = new HessianOutput(osToUse);

try {

skeleton.invoke(in, out);

} catch (Throwable e) {

e.printStackTrace();

}

}

}

 

 

 

 

m 0x00 0x0F testObjectBlock

M

t x00 x1B com.chos.test.service.Block

S 0x00 0x02 id

L 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x0C #0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0010 1100 #44

S 0x00 0x07 account

I 0x00 0x00 0x04 0xD2 #0000 0000 0000 0000 0000 0100 1101 0010 #1234

S 0x00 0x07 receipt

T

S 0x00 0x09 signature

S 0x00 0x0F signature value

z #end of map

z #end of method

 

 

 

运行输出:

com.chos.test.service.Block@1064425[
  id=44
  account=1234
  receipt=true
  signature=signature value
]

 

1、基于hessian协议调用java方法:https://lobin.iteye.com/blog/2367731

2、基于hessian协议调用java方法-一个map例子:https://lobin.iteye.com/blog/2368200

3、基于hessian协议进行rpc调用(http方式):https://lobin.iteye.com/blog/2368721

0
0
分享到:
评论

相关推荐

    多尺度hessian滤波器图像增强

    在图像处理领域,多尺度Hessian滤波器是一种高级的图像增强技术,它主要用于检测图像中的线性结构,特别是对于微弱或者噪声较大的图像特征有很好的识别能力。这个技术是基于数学形态学的Hessian矩阵理论,由V.S. ...

    hessian-4.0.63-API文档-中英对照版.zip

    赠送jar包:hessian-4.0.63.jar; 赠送原API文档:hessian-4.0.63-javadoc.jar; 赠送源代码:hessian-4.0.63-sources.jar; 赠送Maven依赖信息文件:hessian-4.0.63.pom; 包含翻译后的API文档:hessian-4.0.63-...

    dubbo-hessian协议http请求demo(java)

    - 在实际开发中,我们可能会需要编写一些辅助工具类来处理Hessian的相关操作,例如Hessian2Input和Hessian2Output,它们用于读写Hessian序列化的二进制流。 6. **安全性与优化**: - 虽然Hessian协议效率高,但其...

    基于Hessian矩阵增强的心血管分割_hessian_hessian血管_hessian血管分割_血管分割_Hessian矩阵

    在IT领域,尤其是在医学图像分析中,Hessian矩阵是一个重要的数学工具,用于图像特征检测,特别是在血管分割的应用中。本文将深入探讨标题和描述中提到的"基于Hessian矩阵增强的心血管分割"这一主题。 Hessian矩阵...

    hessian案例,hessian案例

    Hessian是一种高效的二进制序列化协议,常用于实现轻量级的远程过程调用(RPC)。这个案例涉及到了Hessian在Java和Python之间的跨语言通信。以下是对Hessian技术及其应用的详细解释: 1. **Hessian简介**:Hessian...

    Spring中集成Hessian的问题

    在Spring框架中集成Hessian是为了实现远程方法调用(Remote Method Invocation, RMI),这是一种轻量级的序列化协议,可以高效地传输Java对象。Hessian使得服务提供者和服务消费者之间能够通过网络进行快速的数据...

    Hessian矩阵以及在血管增强中的应用—OpenCV3和c++版本代码工程

    Hessian矩阵是图像处理和计算机视觉领域中一个重要的数学工具,尤其在特征检测和结构分析中扮演着关键角色。在本项目中,它被应用于血管增强,这是一个在医学成像和图像分析中常见的任务,目的是突出血管结构,以...

    Jerman增强滤波器 可行.zip_Hessian 滤波器_Jerman_hessian增强_血管增强_血管滤波器

    本文将详细探讨"Jerman增强滤波器",它是一种基于Hessian矩阵的滤波器,旨在突出图像中的血管结构。Hessian矩阵在图像处理中起到关键作用,因为它可以有效地检测图像的局部特征,特别是边缘和曲线。 首先,我们来...

    hessian学习基础篇——序列化和反序列化

    本文将深入探讨Hessian框架的基础知识,它是一个高效的二进制序列化协议,广泛应用于Java和.NET之间跨语言通信。通过学习Hessian,我们可以更有效地处理数据传输,提高应用性能。 首先,让我们理解什么是序列化。...

    java+hessian 远程调用

    Java Hessian远程调用是一种高效的、轻量级的RPC(Remote Procedure Call)解决方案,它允许Java应用程序通过网络进行跨语言的通信。Hessian是BEA Systems开发的一种二进制Web服务协议,它提供了序列化和远程方法...

    基于matlab使用 hessian 特征值增强 2D3D 图像中的血管脊状结构

    这篇教程主要探讨了如何利用MATLAB编程环境以及Hessian矩阵来增强2D和3D图像中的血管脊状结构。Hessian矩阵是一种数学工具,用于计算图像中像素点的二阶导数,从而帮助识别图像的边缘和特征点。 首先,我们要理解...

    基于python的Hessian Frangi滤波算法设计与实现

    在图像处理领域,Hessian Frangi滤波算法是一种用于血管检测和结构分析的高级技术,尤其在医学成像中有着广泛的应用。它基于Hessian矩阵,可以有效地检测图像中的线性和圆状结构。本篇文章将深入探讨如何使用Python...

    hessian

    Hessian是一种二进制Web服务协议,由Caucho Technology公司开发,主要用于提供轻量级、高效的远程方法调用(Remote Method Invocation, RMI)机制。它结合了HTTP协议的可扩展性和Java序列化机制的易用性,使得在...

    Hessian多个版本打包下载

    这个压缩包包含了Hessian的多个版本,分别是Hessian3.1.6、Hessian3.2.1以及Hessian4.0.7。每个版本都有其特定的功能改进和优化,了解这些版本的区别和适用场景对于开发者来说至关重要。 Hessian3.1.6是Hessian的一...

    hessian服务端 客户端 可运行

    在这个“hessian服务端 客户端 可运行”的压缩包文件中,很可能包含了一个完整的示例,让我们来详细探讨Hessian在服务端和客户端的实现及其重要性。 首先,我们来理解一下Hessian服务端。在Java中,Hessian服务端...

    Hessian异步请求访问包

    在IT行业中,尤其是在移动开发领域,Hessian是一种广泛使用的二进制协议,它允许远程对象调用(Remote Object Invocation)。本知识点主要关注的是在Android平台上,如何使用Hessian进行异步请求访问,以便提高应用...

    hessian-4.0.63-API文档-中文版.zip

    赠送jar包:hessian-4.0.63.jar; 赠送原API文档:hessian-4.0.63-javadoc.jar; 赠送源代码:hessian-4.0.63-sources.jar; 赠送Maven依赖信息文件:hessian-4.0.63.pom; 包含翻译后的API文档:hessian-4.0.63-...

    hessian使用小例子

    在IT行业中,Hessian是一种轻量级的二进制序列化协议,主要用于远程方法调用(RMI)和Web服务。Hessian由Caucho Technology开发,它的设计目标是提供比XML更快、更简洁的数据传输方式,从而提高网络通信效率。在Java...

    hessian_homework1_hessian_matlab中心线_hessianmatlab_

    在图像处理领域,Hessian矩阵是一种非常重要的工具,尤其在边缘检测和纹理分析中发挥着关键作用。本项目“hessian_homework1_hessian_matlab中心线_hessianmatlab_”是利用MATLAB实现的Hessian矩阵计算,用于寻找...

Global site tag (gtag.js) - Google Analytics