1. 下载 ice
https://download.zeroc.com/Ice/3.6/Ice-3.6.2.msi
我的安装地址为
eclipse 安装 ICE builder for eclipse
安装完成后配置
到此环境准备好了
下面建工程 ,写代码了
2. 建ice_hello project
在project下建目录 slice
然后建 hello.ice 文件
文件名大小写无所谓
文件内容
[["java:package:com.stone.hello"]] // 定义java包名
module testice
{
interface Hello
{
string sayHello(string s);
};
};
3. 然后 project上右键 -》 ice builder -》 add ice builder
则在项目下生成java代码
4. 然后建接口的实现
package com.stone.hello.testice.impl;
import com.stone.hello.testice._HelloDisp;
import Ice.Current;
public class HelloServiceImpl extends _HelloDisp {
@Override
public String sayHello(String s, Current __current) {
// TODO Auto-generated method stub
return " 这是我的 一个 zeroc ice 的测试 ";
}
}
5. 建服务端 并 启动
package ice_hello;
import com.stone.hello.testice.impl.HelloServiceImpl;
public class Server {
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("MyHelloAdapter", "default -p 9999");
Ice.Object object = new HelloServiceImpl();
adapter.add(object, ic.stringToIdentity("helloA"));
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. 建客户端 并启动
package ice_hello;
import com.stone.hello.testice.HelloPrx;
import com.stone.hello.testice.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("helloA:default -h localhost -p 9999");
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);
}
}
控制台输出
demo下载地址
http://download.youkuaiyun.com/detail/stonexmx/9544474
结束