Zeroc Ice 学习笔记
1、Slice语言基本介绍
基本类型:bool、byte、short、int、long、float、double、string
const string test =“hello”;常量定义
enum fruit {Test,Hello};
const fruit f = Test;
module Test{};模块
struct 结构体定义:struct Test{int id;...};
Sequence 集合类型,相当于List,指定返回Java ArrayList需要预先定义 ["java:type:java.util.ArrayList"]sequence
List; Dictionary Map类型,类似Java HashMap
interface 定义接口:interface Test{void sayHelloWorld();};
exception 定义异常:exception Test{};
#include
,没有设置includepath时需使用全路径:D:\xxx\xxxx.ice
2、第一个程序(Slice2Java)
HelloWorld.ice文件:
通过Ice-ant方式编译如下:
org.apache.maven.plugins
maven-antrun-plugin
1.8
default-cli
generate-sources
run
com.zeroc
ant-ice
4.0.0
system
${project.basedir}/lib/ant-ice-4.0.0.jar
实现_HelloWorldDisp中sayHelloWorld:
HelloWorld服务:
public class HelloWorldServer {
public static void main(String... args) {
int status = 0;
Communicator communicator = null;
try {
communicator = Util.initialize(args);//初始化Communicator,args可以传初始化参数
//创建adapter 视同tcp/ip协议 端口1000
ObjectAdapter objectAdapter = communicator.createObjectAdapterWithEndpoints("MyHelloWorld", "default -p 1000");
HelloWorldImpl helloWorld = new HelloWorldImpl();
objectAdapter.add(helloWorld, Util.stringToIdentity("HelloWorldService"));
objectAdapter.activate();//激活adapter
System.out.println("===========HelloWorld Server Started");
communicator.waitForShutdown();
} catch (Exception e) {
e.printStackTrace();
status = 1;
} finally {
if (communicator != null)
communicator.destroy();
}
System.exit(status);
}
}
Client调用服务:
public class HelloWorldClient {
public static void main(String... args) {
int status = 0;
Communicator communicator = null;
try {
communicator = Util.initialize(args);//初始化
ObjectPrx objectPrx = communicator.stringToProxy("HelloWorldService:default -p 1000");
HelloWorldPrx helloWorldPrx = HelloWorldPrxHelper.checkedCast(objectPrx);
if(helloWorldPrx == null)
throw new Error("Invalid proxy");
System.out.println(helloWorldPrx.sayHelloWorld());
} catch (Exception e) {
e.printStackTrace();
status = 1;
} finally {
if (communicator != null)
communicator.destroy();
}
System.exit(status);
}
}