【1】基于Linux CentOS-7的安装
cd /etc/yum.repos.dsudo
wget https://zeroc.com/download/rpm/zeroc-ice-el7.reposudo
yum install ice-all-runtime ice-all-devel
【2】编写一个基本的slice,保存为Printer.ice
module Demo {
interface Printer {
void printString(string s);
};
};
编译slice
$ mkdir generated
$ slice2java --output-dir generated Printer.ice
编写一个基本实现
public class PrinterI extends Demo._PrinterDisp {
public void
printString(String s, Ice.Current current)
{
System.out.println(s);
}
}
【2】编写Server代码
public class Server {
public static void
main(String[] args)
{
int status = 0;
Ice.Communicator ic = null;
try {
ic = Ice.Util.initialize(args);
Ice.ObjectAdapter adapter =
ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000");
Ice.Object object = new PrinterI();
adapter.add(object, ic.stringToIdentity("SimplePrinter"));
adapter.activate();
ic.waitForShutdown();
} catch (Ice.LocalException e) {
e.printStackTrace();
status = 1;
} catch (Exception e) {
System.err.println(e.getMessage());
status = 1;
}
if (ic != null) {
// Clean up
//
try {
ic.destroy();
} catch (Exception e) {
System.err.println(e.getMessage());
status = 1;
}
}
System.exit(status);
}
}
编译代码
$ mkdir classes
$ javac -d classes -classpath classes:/usr/share/java/ice.jar \
Server.java PrinterI.java generated/Demo/*.java
这里注意想一下ice.jar的jar的位置,一定要是正确的位置
【3】编写Client代码
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("SimplePrinter:default -p 10000");
Demo.PrinterPrx printer = Demo.PrinterPrxHelper.checkedCast(base);
if (printer == null)
throw new Error("Invalid proxy");
printer.printString("Hello World!");
} catch (Ice.LocalException e) {
e.printStackTrace();
status = 1;
} catch (Exception e) {
System.err.println(e.getMessage());
status = 1;
}
if (ic != null) {
// Clean up
//
try {
ic.destroy();
} catch (Exception e) {
System.err.println(e.getMessage());
status = 1;
}
}
System.exit(status);
}
}
编译Client
$ javac -d classes -classpath classes:/usr/share/java/ice.jar \
Client.java PrinterI.java generated/Demo/*.java
【4】执行Server和Client
cd classes
java Server &
java Client &
出现错误
Error: A JNI error has occurred, please check your installation and try again
出现这个错误,检查发现CLASSPATH中没有设置ice.jar,搜索系统ice.jar,发现在/usr/share/java/ice.jar,将其添加到classpath中,问题解决,可以使用下面的脚本setenv.sh
#! /bin/bash
JAVA_HOME=/usr/java/jdk1.8.0_131
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:/usr/share/java/ice.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
执行source setenv.sh,设置好相关变量
java Server &
java Client &
出现结果
Hello World!
调用成功
【5】使用python客户端调用java的服务端
安装pip和ice-python
sudo yum install python-pip
pip install zeroc-ice
出现错误
#include
compilation terminated.
error: command 'gcc' failed with exit status 1
安装python开发环境解决
sudo yum install python-devl
出现错误
#include
compilation terminated.
error: command 'gcc' failed with exit status 1
安装bzip2开发库
sudo yum install bzip2-devel
编译python版的ice文件
slice2py Printer.ice
编写python的Client.py
import sys, traceback, Ice
import Demo
status = 0
ic = None
try:
ic = Ice.initialize(sys.argv)
base = ic.stringToProxy("SimplePrinter:default -p 10000")
printer = Demo.PrinterPrx.checkedCast(base)
if not printer:
raise RuntimeError("Invalid proxy")
printer.printString("Hello World!")
except:
traceback.print_exc()
status = 1
if ic:
# Clean up
try:
ic.destroy()
except:
traceback.print_exc()
status = 1
sys.exit(status)
然后直接调用
python Client.py
结果
Hello World!