Ice is intermediate between an object-oriented platform for entry-ice, simple HelloWorld is essential.
Reproduced please specify http://www.cnblogs.com/zrtqsk/p/3745286.html, thank you.
First, write a definition of ice
ice is used to define an interface, the program skeleton, to facilitate remote calls. First, we want to use ice proprietary language to write a slice of ice-defined. as follows:
file_name:HelloWorld.ice
module myHelloWorld{ interface HelloWorld{ void say(string s); }; };
java package corresponds module, the definition of ice is essential.
Second, the definition of the compiler of this ice
To compile the definition of ice, ice must first install a server, which will be added to the bin folder environment variable. Jdk with similar configuration. Then use one of slice2java compile command.
For example HelloWorld.ice F files on my disk test folder, run , can appear in the test file directory as follows:
myHelloWorld folder name is the module name ice definition.
Third, write Servant class
ice servant class is provided by the server operating invoked behavior products, it provides the substance of ice object. Plainly, the interface is defined by the ice, the server side implementation class.
According to management, the servant class name is added to a ice interface name "I" suffix, as follows:
package client; import Ice.Current; import myHelloWorld._HelloWorldDisp; public class HelloWorldI extends _HelloWorldDisp{ private static final long serialVersionUID = 1L; @Override public void say(String s, Current __current) { System.out.println("Hello World!"+" the string s is " + s); } }
Above, servant class should extend the abstract class _HelloWorldDisp this slice2java compiler generates. Implementation parameter is the original form of Ice participate in a Current object.
Fourth, write remote server class
Package Client; public class Server { public static void main (String [] args) { int Status = 0 ; // Communicator example, ice run time is the main handle Ice.Communicator IC = null ; the try { // call Ice.Util .Initialize () initialization RUN Time Ice System.out.println ( "initialization RUN Time ... ICE" ); ic = Ice.Util.initialize (args); // create an object adapter, the adapter name and pass 10,000 port receives a request System.out.println ( "Creating an object adapter, monitor port 10000 ..." ); Adapter Ice.ObjectAdapter = ic.createObjectAdapterWithEndpoints ( "SimplePrinterAdapter", "default -p 10000" ); // instantiate a PrinterI object to create a servant Printer interface System.out.println ( "Create Interface servant for ..." ); Ice.Object Object = new new HelloWorldI (); // call adapter add, tell it has a new servant, parameter passing is just a servant, where "SimplePrinter" is the name of Servant System.out.println ( "Object adapter ... to join the servant is" ); adapter.add (Object, Ice.Util.stringToIdentity ( "SimplePrinter" )); // call the adapter's activate () method to activate the adapter. After being activated, the server starts processing requests from clients. System.out.println ( "activation adapter, server requests pending ... adapter.activate (); // This method suspends the calling thread until the server implementation termination date. Or issue a call ourselves off. ic.waitForShutdown (); } the catch (Ice.LocalException E) { e.printStackTrace (); Status =. 1 ; } the catch (Exception E) { e.printStackTrace (); Status =. 1 ; } the finally { IF (IC =! null ) { ic.destroy (); } } System.exit (Status); } }
The server class in fact, a few lines of code, comments have been very clear.
Fifth, write client class
Package Client; Import myHelloWorld.HelloWorldPrx; Import myHelloWorld.HelloWorldPrxHelper; public class Client { public static void main (String [] args) { int Status = 0 ; // Communicator example Ice.Communicator IC = null ; the try { // call Ice .Util.Initialize () initialization RUN Time Ice ic = Ice.Util.initialize (args); // get the remote printer agent Ice.ObjectPrx Base = ic.stringToProxy ( "SimplePrinter: default -p 10000" ); // The down-converted to proxy agent a Printer interface HelloWorldPrx the helloWorld = HelloWorldPrxHelper.checkedCast (Base); // If successful conversion IF (the helloWorld == null ) { the throw new new Error ( "Invalid Proxy" ); } / / invoke the proxy, the string passed to it helloWorld.say ( "bar" ); } the catch (Ice.LocalException E) { e.printStackTrace (); Status =. 1 ; } the catch (Exception E) { e.printStackTrace ( ); Status= 1; } finally { if (ic != null) { ic.destroy(); } } System.exit(status); } }
Sixth, run
First run the server, the server thread runs to wait, as follows:
Initialization ice run time ...
to create an object adapter, monitor port 10000 ...
create a servant for the interface ...
the object adapter to join servant ...
to activate the adapter, wait for the server to process the request ...
After running the client, it is shown below:
Initialization ice run time ...
to create an object adapter, monitor port 10000 ...
create a servant for the interface ...
the object adapter to join servant ...
to activate the adapter, wait for the server to process the request ...
the Hello World ! S IS bar at The String
The client will change it helloWorld.say ( "foo") ;, then run, is shown below:
初始化ice run time... 创建对象适配器,监听端口10000... 为接口创建servant... 对象适配器加入servant... 激活适配器,服务器等待处理请求... Hello World! the string s is bar Hello World! the string s is foo
(部分参考《Ice分布式程序设计》)
转载于:https://www.cnblogs.com/zrtqsk/p/3745286.html