IceBox的基本概念作用就不用说了。
Hello.ice 文件内容
#ifndef HELLO_ICE
#define HELLO_ICE
[["java:package:icebox.demo"]]
module slice2java
{
interface Hello{
void sayHello(string xx);
};
};
#endif
生成java代码结构图如下:
业务实现类icebox.servant.HelloI.java 代码如下:
package icebox.servant;
import Ice.Current;
import icebox.demo.slice2java._HelloDisp;
public class HelloI extends _HelloDisp {
@Override
public void sayHello(String xx, Current __current) {
// TODO Auto-generated method stub
System.out.println("Hello "+xx);
}
}
IceBox服务类icebox.service.HelloServiceI.java
package icebox.service;
import icebox.servant.HelloI;
import Ice.Communicator;
public class HelloServiceI implements IceBox.Service{
private Ice.ObjectAdapter _adapter;
@Override
public void start(String arg0, Ice.Communicator communicator, String[] arg2) {
// TODO Auto-generated method stub
_adapter = communicator.createObjectAdapter(arg0);
_adapter.add(new HelloI(), communicator.stringToIdentity("hello"));
_adapter.activate();
}
@Override
public void stop() {
// TODO Auto-generated method stub
_adapter.destroy();
}
}
客户端实现类 icebox.client.HelloClient.java
package icebox.client;
// **********************************************************************
//
// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved.
//
// **********************************************************************
import icebox.demo.slice2java.HelloPrx;
import icebox.demo.slice2java.HelloPrxHelper;
public class HelloClient extends Ice.Application
{
class ShutdownHook extends Thread
{
@Override
public void
run()
{
try
{
communicator().destroy();
}
catch(Ice.LocalException ex)
{
ex.printStackTrace();
}
}
}
private void menu()
{
System.out.println(
"usage:\n" +
"t: send greeting as twoway\n" +
"o: send greeting as oneway\n" +
"O: send greeting as batch oneway\n" +
"d: send greeting as datagram\n" +
"D: send greeting as batch datagram\n" +
"f: flush all batch requests\n" +
"x: exit\n" +
"?: help\n");
}
@Override
public int run(String[] args)
{
if(args.length > 0)
{
System.err.println(appName() + ": too many arguments");
return 1;
}
//
// Since this is an interactive demo we want to clear the
// Application installed interrupt callback and install our
// own shutdown hook.
//
setInterruptHook(new ShutdownHook());
HelloPrx twoway = HelloPrxHelper.checkedCast(
communicator().propertyToProxy("Hello.Proxy").ice_twoway().ice_timeout(-1).ice_secure(false));
if(twoway == null)
{
System.err.println("invalid object reference");
return 1;
}
HelloPrx oneway = HelloPrxHelper.uncheckedCast(twoway.ice_oneway());
HelloPrx batchOneway = HelloPrxHelper.uncheckedCast(twoway.ice_batchOneway());
HelloPrx datagram = HelloPrxHelper.uncheckedCast(twoway.ice_datagram());
HelloPrx batchDatagram = HelloPrxHelper.uncheckedCast(twoway.ice_batchDatagram());
menu();
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
String line = null;
do
{
try
{
System.out.print("==> ");
System.out.flush();
line = in.readLine();
if(line == null)
{
break;
}
if(line.equals("t"))
{
twoway.sayHello("t");
}
else if(line.equals("o"))
{
oneway.sayHello("o");
}
else if(line.equals("O"))
{
batchOneway.sayHello("0");
}
else if(line.equals("d"))
{
datagram.sayHello("d");
}
else if(line.equals("D"))
{
batchDatagram.sayHello("D");
}
else if(line.equals("f"))
{
batchOneway.ice_flushBatchRequests();
batchDatagram.ice_flushBatchRequests();
}
else if(line.equals("x"))
{
// Nothing to do
}
else if(line.equals("?"))
{
menu();
}
else
{
System.out.println("unknown command `" + line + "'");
menu();
}
}
catch(java.io.IOException ex)
{
ex.printStackTrace();
}
catch(Ice.LocalException ex)
{
ex.printStackTrace();
}
}
while(!line.equals("x"));
return 0;
}
public static void main(String[] args)
{
HelloClient app = new HelloClient();
String client_cnf="C:\\tydic-wk-myeclipse\\NX_Pay\\bin\\config.client";
int status = app.main("Client", args, client_cnf);
System.exit(status);
}
}
config.client 配置文件内空
#
# The client reads this property to create the reference to the
# "hello" object in the server.
#
Hello.Proxy=hello:tcp -h localhost -p 20000:udp -h localhost -p 20000
#
# Warn about connection exceptions
#
Ice.Warn.Connections=1
#
# Network Tracing
#
# 0 = no network tracing
# 1 = trace connection establishment and closure
# 2 = like 1, but more detailed
# 3 = like 2, but also trace data transfer
#
#Ice.Trace.Network=1
#
# Protocol Tracing
#
# 0 = no protocol tracing
# 1 = trace protocol messages
#
#Ice.Trace.Protocol=1
config.icebox 配置文件
Ice.Admin.Endpoints=tcp -p 9996 -h localhost
Ice.Admin.InstanceName=DemoIceBox
#
# The hello service
#
IceBox.Service.Hello=C:\\tydic-wk-myeclipse\\NX_Pay\\bin\\hello.jar:icebox.service.HelloServiceI --Ice.Config=config.service
#IceBox.Service.Hello=classpath:icebox.service.HelloServiceI --Ice.Config=config.service
#
# Warn about connection exceptions
#
config.service 配置文件内容:
#
# The server creates one single object adapter with the name of
# the service. The following line sets the endpoints for this
# adapter.
#
Hello.Endpoints=tcp -h localhost -p 20000:udp -h localhost -p 20000
#
# Warn about connection exceptions
#
Ice.Warn.Connections=1
#
# Network Tracing
#
# 0 = no network tracing
# 1 = trace connection establishment and closure
# 2 = like 1, but more detailed
# 3 = like 2, but also trace data transfer
#
#Ice.Trace.Network=1
#
# Protocol Tracing
#
# 0 = no protocol tracing
# 1 = trace protocol messages
#
#Ice.Trace.Protocol=1
至此所有代码和配置完成,把项目打包生成hello.jar 文件,放在C:\\tydic-wk-myeclipse\\NX_Pay\\bin 目录中。
在cmd下,cd C:\tydic-wk-myeclipse\NX_Pay\\bin
执行
java -Djava.ext.dirs=D:\ZeroC\Ice-3.6.1\lib\ -cp D:\ZeroC\Ice-3.6.1\lib\icebox-3.6.1.jar IceBox.Server --Ice.Config=config.icebox
-Djava.ext.dirs 设置依赖ice jar包目录
-cp 因jar里没有 manifest 没设置main-class
icebox运行是执行icebox-3.6.1.jar 的 IceBox.Server类,该类会根据 --Ice.Config= config.icebox 配置文件,部署相关服务。
运行结果: