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

2009年01月13日

基于apache mina 2.0.0 M4和google Protocol Buffers 2.0.3的java RPC实例(2.service端)

Filed under: Java,RPC — 标签:, , , , — lizongbo @ 23:51

接上一篇:http://618119.com/archives/2009/01/10/124.html
1.接口功能实现:

[code]
package com.lizongbo.mobileqq;

import com.lizongbo.mobileqq.QQUserInfoProtos.*;
import com.google.protobuf.RpcController;
import com.google.protobuf.RpcCallback;

public class UserInfoServiceImpl extends UserInfoService {
public void getUserInfo(RpcController controller,
GetUserInfoRequest request,
RpcCallback < com.lizongbo.mobileqq. QQUserInfoProtos.GetUserInfoResponse > done) {

GetUserInfoResponse.Builder builder = GetUserInfoResponse.
newBuilder();
builder.setQqNo(request.getQqNo());

if (request.getQqNo() == 123456) {
builder.setResult(0);
QQUser.Builder qb = QQUser.newBuilder();
qb.setQqNo(request.getQqNo());
qb.setNickNmae(“lizongbo”);
qb.setPostCode(“618100”);
qb.setSignName(“google Protocol Buffers 远程调用”);
qb.setAddress(“google”);
builder.setUserInfo(qb);
} else {
builder.setResult( -1);
}
done.run(builder.build());
}

public void updateUserInfo(RpcController controller,
UpdateUserInfoRequest request,
RpcCallback < com.lizongbo.mobileqq. QQUserInfoProtos.UpdateUserInfoResponse > done) {
UpdateUserInfoResponse.Builder builder = UpdateUserInfoResponse.
newBuilder();
builder.setQqNo(request.getQqNo());

if (request.getQqNo() == 123456) {
builder.setResult(0);
builder.setMessage(“成功!”);
} else {
builder.setResult( -1);
builder.setMessage(“失败!”);
}
done.run(builder.build());
}

}

[/code]

2.RpcController接口的实现:

[code]
package com.lizongbo.mobileqq;

import com.google.protobuf.RpcController;
import com.google.protobuf.RpcCallback;

public class UserInfoServiceRpcController implements RpcController {

public String errorText() {
return “no error”;
}

public boolean failed() {
return false;
}

public boolean isCanceled() {
return false;
}

public void notifyOnCancel(RpcCallback callback) {
System.out.println(“run RpcCallback by notifyOnCancel”);
}

public void reset() {
System.out.println(“do nothing”);
}

public void setFailed(String reason) {
System.out.println(“setFailed ” + reason);
}

public void startCancel() {
System.out.println(“run startCancel”);
}
}

[/code]

没有评论 »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress