`
gaojingsong
  • 浏览: 1158347 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

ZeroICE入门示例

阅读更多

1、编写*.ice 文件

module myHelloWorld{

    interface HelloWorld{

         void say(string s);

     };

 };



 

2、生成存根 ,命令slice2java ice文件



 

 

3、开发服务端

导入生成的存根文件



 

引入ICE安装目录下面相应的jar包:Ice.jar

 

4、开发服务端代码如下

步骤一:编写Servant类即带有Disp的存根文件也就是说继承_HelloWorldDisp 这个类,这个类是个抽象类定义如下:

public abstract class _HelloWorldDisp extends Ice.ObjectImpl implements HelloWorld{}

servant类是ice所定义的接口,在服务器端的实现类。我们在该类中可以编写服务器端对请求的具体处理。

代码如下:

package myHelloWorld.server;

import Ice.Current;

 import myHelloWorld._HelloWorldDisp;

 

  public class HelloWorld_gaojs extends _HelloWorldDisp{

 

      private static final long serialVersionUID = 1L;

 

     public void say(String s, Current __current) {

         System.out.println("Hello World!"+" the string s is " + s);

      }

  }

步骤二:创建远程的服务器类

package myHelloWorld.server;

 

public class ServerStart {

 

/**

* @param args

*/

public static void main(String[] args) {

int status = 0;

// Communicator实例,是ice run time的主句柄

Ice.Communicator ic = null;

try {

// 调用Ice.Util.Initialize()初始化Ice run time

System.out.println("初始化ice run time...");

ic = Ice.Util.initialize(args); // args参数可传可不传

 

// 创建一个对象适配器,传入适配器名字和在10000端口处接收来的请求

System.out.println("创建对象适配器,监听端口10000...");

Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints(

"SimplePrinterAdapter", "default -p 10000");

 

// 实例化一个PrinterI对象,为Printer接口创建一个servant

System.out.println("为接口创建servant...");

Ice.Object object = new HelloWorld_gaojs();

 

// 调用适配器的add,告诉它有一个新的servant,传递的参数是刚才的servant,这里的“gaojs”是Servant的名字

System.out.println("对象适配器加入servant...");

adapter.add(object, Ice.Util.stringToIdentity("gaojs"));

 

// 调用适配器的activate()方法,激活适配器。被激活后,服务器开始处理来自客户的请求。

System.out.println("激活适配器,服务器等待处理请求...");

adapter.activate();

 

// 这个方法挂起发出调用的线程,直到服务器实现终止为止。或我们自己发出一个调用关闭。

ic.waitForShutdown();

} catch (Ice.LocalException e) {

e.printStackTrace();

status = 1;

} catch (Exception e) {

e.printStackTrace();

status = 1;

} finally {

if (ic != null) {

ic.destroy();

}

}

System.exit(status);

}

 

}

 

 

5、开发客服端,导入存根文件和jar包,其中存根文件可以比服务端少三个。

import myHelloWorld.HelloWorldPrx;

import myHelloWorld.HelloWorldPrxHelper;

public class Client {

public static void main(String[] args) {

int status = 0;

// Communicator实例

Ice.Communicator ic = null;

try {

// 调用Ice.Util.Initialize()初始化Ice run time

ic = Ice.Util.initialize(args);

 

// 根据servant的名称以及服务器ip、端口获取远程服务代理

Ice.ObjectPrx base = ic.stringToProxy("gaojs:default -p 10000");

 

// 将上面的代理向下转换成一个Printer接口的代理

HelloWorldPrx helloWorld = HelloWorldPrxHelper.checkedCast(base);

 

// 如果转换成功

if (helloWorld == null) {

throw new Error("Invalid proxy");

}

 

// 调用这个代理,将字符串传给它

helloWorld.say("客户端测试");

 

} catch (Ice.LocalException e) {

e.printStackTrace();

status = 1;

} catch (Exception e) {

e.printStackTrace();

status = 1;

} finally {

if (ic != null) {

ic.destroy();

}

}

System.exit(status);

}

}

 

6、运行测试

1)启动服务端



 

2)启动客户端



 

  • 大小: 51.3 KB
  • 大小: 30.9 KB
  • 大小: 33.8 KB
  • 大小: 70.4 KB
  • 大小: 62.4 KB
  • 大小: 62.8 KB
0
0
分享到:
评论
2 楼 andot 2016-11-30  
ZeroC ICE 生成的代码有点丑陋。

Hprose 的 [url=
http://github.com/hprose/hprose-java/wiki/Hello-World]Hello World[/url] 比这个简洁多了,也不需要中间语言生成丑陋的代码。而且服务器和客户端还是松耦合关系。

服务器

package hprose.tcphelloexam;

import hprose.server.HproseTcpServer;

public class TCPHelloServer {
    public static String hello(String name) {
        return "Hello " + name + "!";
    }
    public static void main(String[] args) throws Exception {
        HproseTcpServer server = new HproseTcpServer("tcp://localhost:4321");
        server.add("hello", TCPHelloServer.class);
        server.start();
        System.out.println("START");
        System.in.read();
        server.stop();
        System.out.println("STOP");
    }
}


客户端

package hprose.tcphelloexam;

import hprose.server.HproseTcpServer;

public class TCPHelloServer {
    public static String hello(String name) {
        return "Hello " + name + "!";
    }
    public static void main(String[] args) throws Exception {
        HproseTcpServer server = new HproseTcpServer("tcp://localhost:4321");
        server.add("hello", TCPHelloServer.class);
        server.start();
        System.out.println("START");
        System.in.read();
        server.stop();
        System.out.println("STOP");
    }
}


1 楼 andot 2016-11-30  
ZeroC ICE 生成的代码有点丑陋。

Hprose 的 [url=
https://github.com/hprose/hprose-java/wiki/Hello-World]Hello World[/url] 比这个简洁多了,也不需要中间语言生成丑陋的代码。而且服务器和客户端还是松耦合关系。

服务器


package hprose.tcphelloexam;

import hprose.server.HproseTcpServer;

public class TCPHelloServer {
    public static String hello(String name) {
        return "Hello " + name + "!";
    }
    public static void main(String[] args) throws Exception {
        HproseTcpServer server = new HproseTcpServer("tcp://localhost:4321");
        server.add("hello", TCPHelloServer.class);
        server.start();
        System.out.println("START");
        System.in.read();
        server.stop();
        System.out.println("STOP");
    }
}


客户端


package hprose.tcphelloexam;

import hprose.server.HproseTcpServer;

public class TCPHelloServer {
    public static String hello(String name) {
        return "Hello " + name + "!";
    }
    public static void main(String[] args) throws Exception {
        HproseTcpServer server = new HproseTcpServer("tcp://localhost:4321");
        server.add("hello", TCPHelloServer.class);
        server.start();
        System.out.println("START");
        System.in.read();
        server.stop();
        System.out.println("STOP");
    }
}

相关推荐

Global site tag (gtag.js) - Google Analytics