ice box 以配置文件为主,所以首先得先会配置,有什么不懂的就往官方网站上拾到拾到
https://doc.zeroc.com/display/Doc/Home
run cmd:
java IceBox.Server --Ice.Config=config
java -classpath example.jar IceBox.Server --Ice.Config=example.properties
example.properties:
IceBox.Service.ExampleService=/classes:PrinterServiceI --Ice.Trace.Network=1
IceBox.UseSharedCommunicator.ExampleService=1
ExampleServer.Endpoints=tcp -p 10001
#you can define servel services
#thread pool properties
Ice.ThreadPool.Server.Size=8
Ice.ThreadPool.Server.SizeMax=100
Ice.ThreadPool.Server.SizeWarn=40
Ice.ThreadPool.Client.Size=8
Ice.ThreadPool.Client.SizeMax=100
Ice.ThreadPool.Client.SizeWarn=40
Ice.ACM.Close=0
IceBox.InheritProperties=1
#if exist define serval services, assign the startup order, and print when startup
IceBox.LoadOrder=ExampleService,order 2, order 3
IceBox.PrintServicesReady=ExampleService 1, order 2, order 3
Define your demo slice:
module Demo{
interface Printer {
void printString(string s);
};
};
Genarated your code of slice:
$ mkdir generated
$ slice2java --output-dir generated demo.ice
so you can found a class name Demo._PrinterDisp, and imply this class, but i do not suggest this method in the paper, the principle of slice:
IceBox Interface:
module IceBox {
local interface Service {
void start(string name, Ice::Communicator communicator, Ice::StringSeq args);
void stop();
};
};
Define your Ice Box Service:
public class PrinterI extends Demo._PrinterDisp implements IceBox.Service {
public void printString(String s, Ice.Current current)
{
System.out.println(s);
}
@Override
public void start(String name, Communicator communicator, String[] args) {
adapter = communicator.createObjectAdapter(name);
Ice.Object obj = this;
adapter.add(obj, communicator.stringToIdentity(name));
adapter.activate();
}
@Override
public void stop() {
adapter.destroy();
}
}
so a service in ice box like this,
Ofcouse, you can define servral services:
at last, the client invoke:
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("ExampleService:default -p 10001");
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);
}
}