lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2009年01月15日

基于apache mina 2.0.0 M4和google Protocol Buffers 2.0.3的java RPC实例(6.Client端的接口调用)

Filed under: Java,RPC — 标签:, , , , — lizongbo @ 01:03

接上一篇:http://618119.com/archives/2009/01/15/130.html
1.ProtobufRpcChannel.(相当于thrift的TBinaryProtocol).
[code]
package com.lizongbo.protobufrpc;

import com.google.protobuf.RpcChannel;
import java.net.URL;
import com.google.protobuf.Descriptors.MethodDescriptor;
import com.google.protobuf.RpcController;
import com.google.protobuf.Message;
import com.google.protobuf.RpcCallback;
import java.net.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class ProtobufRpcChannel implements RpcChannel {
private URL serviceUrl = null;
public ProtobufRpcChannel(String url) {
try {
try {
URL.setURLStreamHandlerFactory(new
HessianURLStreamHandlerFactory());
} catch (Exception ex) {
ex.printStackTrace();

}
serviceUrl = new URL(url);
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}

public void callMethod(MethodDescriptor method, RpcController controller,
Message request, Message responsePrototype,
RpcCallback done) {
try {
System.out.println(method.getService().getFullName());
System.out.println(method.getFullName());
method.toProto().toByteArray();
System.out.println(responsePrototype.getClass());
if (“hessiantcp”.equalsIgnoreCase(serviceUrl.getProtocol()) ||
“http”.equalsIgnoreCase(serviceUrl.getProtocol())) {
Socket socket = new Socket(serviceUrl.getHost(),
serviceUrl.getPort());
DataOutputStream out = new DataOutputStream(socket.
getOutputStream());
DataInputStream in = new DataInputStream(socket.getInputStream());
ProtobufRPCRequest req = new ProtobufRPCRequest();
req.setServiceName(method.getService().getFullName());
req.setMethodName(method.getName());
req.setRequestMessage(request.toByteArray());
out.write(req.toByteArray()); //传数据包
out.flush(); //例子此处尚不完善,会有1448错误
int len = in.readInt();
System.out.println(“应答包长:” + len);
byte[] buf = new byte[len – 4];
in.read(buf, 0, buf.length);
Message res = responsePrototype.newBuilderForType().mergeFrom(
buf).build();
done.run(res);
in.close();
out.close();
} else {
//@todo
}
}

catch (Exception e) {
throw new RuntimeException(e);
}

}
}

[/code]

2.RPCTest.java(客户端接口调用)
[code]
package com.lizongbo.protobufrpc;

import java.net.*;
import com.google.protobuf.RpcCallback;
import com.lizongbo.mobileqq.UserInfoServiceRpcController;
import com.lizongbo.mobileqq.QQUserInfoProtos;

public class RPCTest {

public static void main(String[] args) {
for (int i = 123456; i < 123458; i++) { try { ProtobufRpcChannel rpcChannel = new ProtobufRpcChannel( "hessiantcp://127.0.0.1:8080/mobileqq"); UserInfoServiceRpcController controller = new UserInfoServiceRpcController(); QQUserInfoProtos.UserInfoService service = QQUserInfoProtos. UserInfoService.newStub(rpcChannel); QQUserInfoProtos.GetUserInfoRequest.Builder builder = QQUserInfoProtos.GetUserInfoRequest.newBuilder(); builder.setQqNo(123456); QQUserInfoProtos.GetUserInfoRequest req = builder.build(); service.getUserInfo(controller, req, new RpcCallback() {
public void run(QQUserInfoProtos.GetUserInfoResponse
response) {
System.out.println(response.getQqNo() + “,result” +
response.getResult() +
“\t” +
response.getUserInfo());
}
});

} catch (Exception ex) {
ex.printStackTrace();
}

}

}
}

[/code]

没有评论 »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress