基于hessian协议调用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

猜你喜欢

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