您的位置:首页 > 编程语言 > Java开发

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

2017-04-09 09:21 495 查看
基于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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: