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

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

map

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

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

type描述map类型,可为空

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

先定义一个接口:

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

]

猜你喜欢

转载自lobin.iteye.com/blog/2368199
今日推荐