基于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
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
相关文章推荐
- dwr例子6调用返回List、Set或者Map的java方法
- [Java] 演示RMI(远程方法调用) 的一个例子
- 一个在java运行时利用反射动态调用方法的例子
- ABAP--一个极好的调用外部java程序的Search Help Exit的实例(RFC好例子)
- oracle调用java方法的例子
- 动态调用一个类的简单例子(java反射)
- DWR 对DATA的理解(调用返回List、Set或者Map的java方法)
- 一个基于RSA算法的Java数字签名例子
- 基于Ajax和JSON从javascript中调用后台java方法的JsonGateway
- oracle调用java方法的例子(下面所有代码都是在sql/plus
- Java使用JNI调用VC的DLL的一个小例子
- 一个在windows和基于SWT的java程序间拖拽的例子
- 一个基于RSA算法的Java数字签名例子
- ABAP--一个极好的调用外部java程序的Search Help Exit的实例(RFC好例子)
- .NET调用Java方法时出现的一个错误及其原因
- java中查找一个方法都是哪些方法调用的代码
- ABAP--一个极好的调用外部java程序的Search Help Exit的实例(RFC好例子)
- JAVA调用Tuxedo服务——使用JNI封装Tuxedo客户端的一个例子
- dwr例子5调用有JavaBean参数的java方法
- ABAP--一个极好的调用外部java程序的Search Help Exit的实例(RFC好例子)