1.安装ice
2.在eclipse中安装ice插件
在help->Eclipse Marketplace中搜索ice,并下载
如下图所示,如果出现Ice Builder表示安装成功,这时设置SDK Location为上面的安装路径即可。
3.建立ice项目
(1)建立java工程
(2)右单击工程ice_hello->Ice Builder->Add Ice Builder
(3)在slice中建立hello.ice文件
[["java:package:com.rain.service"]] // 定义java包名
module demo
{
interface Hello
{
string sayHello(string s);
};
};
然后会在genetated目录中生成想对应的ice的java文件
(4)编写hello的java类,继承ice生成的_*Disp文件
package testICE;
import com.rain.service.demo._HelloDisp;
import Ice.Current;
public class Hello extends _HelloDisp {
private static final long serialVersionUID = 1L;
@Override
public String sayHello(String s, Current __current) {
return "Hello :" + s;
}
}
(5)编写service类(12000为端口号)
package testICE;
public class Service {
public static void main(String[] args) {
int status = 0;
Ice.Communicator ic = null;
try{
System.out.println("Server starting...");
ic = Ice.Util.initialize(args);
Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("HelloAdapter", "default -p 12000");
Ice.Object object = new Hello();
adapter.add(object, ic.stringToIdentity("hello"));
adapter.activate();
System.out.println("Server start success.");
ic.waitForShutdown();
}catch(Ice.LocalException e){
e.printStackTrace();
status = 1;
}catch(Exception e){
System.err.println(e.getMessage());
status = 1;
}
if(ic != null){
try{
ic.destroy();
}catch(Exception e){
System.err.println(e.getMessage());
status = 1;
}
}
System.exit(status);
}
}
(6)编写client类
package testICE;
import com.rain.service.demo.HelloPrx;
import com.rain.service.demo.HelloPrxHelper;
public class Client {
public static void main(String[] args) {
int status = 0;
Ice.Communicator ic = null;
try{
ic = Ice.Util.initialize(args);
Ice.ObjectPrx base = ic.stringToProxy("hello:default -p 12000");
HelloPrx hello = HelloPrxHelper.checkedCast(base);
if(hello == null){
throw new Error("Invalid proxy");
}
String s = hello.sayHello("World!");
System.out.println(">>"+s);
}catch(Ice.LocalException e){
e.printStackTrace();
status = 1;
}catch(Exception e){
System.err.println(e.getMessage());
status = 1;
}
if(ic != null){
try{
ic.destroy();
}catch(Exception e){
System.err.println(e.getMessage());
status = 1;
}
}
System.exit(status);
}
}
文件目录为:
(7)测试
运行service->运行client
service控制台输出
client控制台输出