概述
之前使用rpc都是webservice、hessian、dubbo之类,现在公司里使用ICE,以前都没有怎么听过的,自己花一些时间学习下ice,并写一个demo,java里基础的数据类型传输都没太大问题,对象的传输却麻烦一些,自己在查了很多资料才实现,网上很少找到一个传输对象类型的完整实例。费话不多说,现在贴代码。
slice文件,hello.ice:
module com{
module test{
// 预定义
module bean{
class Response;
};
module bean{
class Request;
};
module operator{
// 执行者
interface Hello{
// 获取Response对象
idempotent com::test::bean::Response sayHello(com::test::bean::Request request);
};
};
};
};
RequestDef.ice文件:
module com{
module test{
module bean{
class Request{
// Fields
string traceid;
string message;
// Methods
string gettraceid();
void settraceid(string traceid);
string getmessage();
void setmessage(string message);
};
};
};
};
ResponseDef.ice文件:
module com{
module test{
module bean{
class Response{
// Fields
string code;
string message;
// Methods
string getcode();
void setcode(string code);
string getmessage();
void setmessage(string message);
};
};
};
};
HelloI.java
package com.test.impl;
import java.io.Serializable;
import Ice.Current;
import com.test.bean.Request;
import com.test.bean.Response;
import com.test.operator._HelloDisp;
import com.test.response.ResponseI;
public class HelloI extends _HelloDisp implements Serializable {
private static final long serialVersionUID = 1L;
@Override
public Response sayHello(Request request, Current __current) {
Response response=new ResponseI();
response.setcode("x001");
response.setmessage("test response");
return response;
}
}
HelloServer.java:
package com.test.server;
import java.io.Serializable;
import Ice.ObjectAdapter;
import com.test.bean.Request;
import com.test.impl.HelloI;
import com.test.request.ObjectFactory4Request;
public class HelloServer implements Serializable {
private static final long serialVersionUID = -691557224337330222L;
public static void main(String[] args) {
//执行状态
int status = 0;
Ice.Communicator ic = null;
try {
// 初始化环境
ic = Ice.Util.initialize();
// 添加自定义工厂类,用于请求信息的组装,如果请求参数不是引用类型的话,就不需要了
ic.addObjectFactory(new ObjectFactory4Request(), Request.ice_staticId());
//初始化Adapter
String name = "HelloServer";
String endpoints = "default -h 127.0.0.1 -p 9999";
ObjectAdapter objAdapter = ic.createObjectAdapterWithEndpoints(
name, endpoints);
//创建伺服者
Ice.Object servant = new HelloI();
//添加伺服者至适配器
objAdapter.add(servant,
Ice.Util.stringToIdentity("HelloId"));
//激活
objAdapter.activate();
System.out.println("HelloId 服务启动,等待接收信息....");
//等待关闭
ic.waitForShutdown();
} catch (Exception e) {
e.printStackTrace();
status = 1;
} finally {
if (ic != null) {
ic.destroy();
}
System.exit(status);
}
}
}
HelloClient.java
/**
*
*/
package com.test.client;
import java.io.Serializable;
import com.test.bean.Request;
import com.test.bean.Response;
import com.test.operator.HelloPrx;
import com.test.operator.HelloPrxHelper;
import com.test.request.RequestI;
import com.test.response.ObjectFactory4Response;
import Ice.ObjectPrx;
public class HelloClient implements Serializable{
private static final long serialVersionUID = 1L;
private Ice.Communicator ic = null;
public static void main(String[] args) {
HelloClient client = new HelloClient();
Request request=new RequestI();
request.settraceid("002");
request.setmessage("test");
Response response = client.sayHello(request);
StringBuffer info = new StringBuffer();
if (response == null) {
info.append("无返回信息");
} else {
info.append("code=" + response.code);
info.append(",message=" + response.getmessage());
}
System.out.println(info.toString());
//退出jvm
System.exit(0);
}
/**
*/
public Response sayHello(Request request){
Response response = null;
try {
// 获取代理
HelloPrx helloPrx=null;
if (this.ic == null) {
//初始化环境
ic = Ice.Util.initialize();
}
// 创建代理基类对象
String str = "HelloId:default -h 127.0.0.1 -p 9999";
ObjectPrx objPrx = this.ic.stringToProxy(str);
helloPrx = HelloPrxHelper.checkedCast(objPrx);
// 添加自定义类
Ice.ObjectFactory factory = new ObjectFactory4Response();
//增加工厂类,用于返回信息的组装,如果返回参数不是引用类型的话,就不需要了
this.ic.addObjectFactory(factory, com.test.response.ResponseI.ice_staticId());
response = helloPrx.sayHello(request);
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
}
ObjectFactory4Request.java这个很重要
/**
*
*/
package com.test.request;
import Ice.ObjectFactory;
public class ObjectFactory4Request implements ObjectFactory {
@Override
public Ice.Object create(String type) {
if (type.equals(com.test.request.RequestI.ice_staticId())) {
return new RequestI();
}
return null;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
RequestI.java
/**
*
*/
package com.test.request;
import java.io.Serializable;
import Ice.Current;
import com.test.bean.Request;
public class RequestI extends Request implements Serializable{
/**
*
*/
private static final long serialVersionUID = -4990773688048548492L;
@Override
public String gettraceid(Current __current) {
return this.traceid;
}
@Override
public void settraceid(String traceid, Current __current) {
this.traceid=traceid;
}
@Override
public String getmessage(Current __current) {
return this.message;
}
@Override
public void setmessage(String message, Current __current) {
this.message=message;
}
}
ObjectFactory4Response.java 这个也重要
/**
*
*/
package com.test.response;
import Ice.ObjectFactory;
public class ObjectFactory4Response implements ObjectFactory {
@Override
public Ice.Object create(String type) {
if (type.equals(com.test.response.ResponseI.ice_staticId())) {
return new ResponseI();
}
return null;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
ResponseI.java
/**
*
*/
package com.test.response;
import java.io.Serializable;
import Ice.Current;
import com.test.bean.Response;
public class ResponseI extends Response implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String getcode(Current __current) {
return this.code;
}
@Override
public void setcode(String code, Current __current) {
this.code=code;
}
@Override
public String getmessage(Current __current) {
return this.message;
}
@Override
public void setmessage(String message, Current __current) {
this.message=message;
}
}
其他的slice生成的java就不贴出来了,先启动HelloServer服务,然后启动客户端HelloClient,就可以测试了。
源码下载
最后
以上就是奋斗八宝粥为你收集整理的RPC之ICE java简单使用的全部内容,希望文章能够帮你解决RPC之ICE java简单使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复