ice eclipse安装和项目初建

1.安装ice

2.在eclipse中安装ice插件

在help->Eclipse Marketplace中搜索ice,并下载


如下图所示,如果出现Ice Builder表示安装成功,这时设置SDK Location为上面的安装路径即可。

3.建立ice项目

(1)建立java工程

(2)右单击工程ice_hello->Ice Builder->Add Ice Builder

(3)在slice中建立hello.ice文件

[["java:package:com.rain.service"]] // 定义java包名  
module demo  
{  
    interface Hello   
    {  
        string sayHello(string s);  
    };  
}; 
然后会在genetated目录中生成想对应的ice的java文件

(4)编写hello的java类,继承ice生成的_*Disp文件

package testICE;

import com.rain.service.demo._HelloDisp;

import Ice.Current;

public class Hello extends _HelloDisp {

	private static final long serialVersionUID = 1L;

	@Override
	public String sayHello(String s, Current __current) {
		return "Hello :" + s;  
	}
	
}

(5)编写service类(12000为端口号)

package testICE;

public class Service {
	public static void main(String[] args) {
		int status = 0;
		Ice.Communicator ic = null;
		try{
			System.out.println("Server starting...");
			ic = Ice.Util.initialize(args);
			Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("HelloAdapter", "default -p 12000");
			Ice.Object object = new Hello();
			adapter.add(object, ic.stringToIdentity("hello"));
			adapter.activate();
			System.out.println("Server start success.");
			ic.waitForShutdown();
		}catch(Ice.LocalException e){
			e.printStackTrace();
			status = 1;
		}catch(Exception e){
			System.err.println(e.getMessage());
			status = 1;
		}
		if(ic != null){
			try{
				ic.destroy();
			}catch(Exception e){
				System.err.println(e.getMessage());
				status = 1;
			}
		}
		System.exit(status);
	}
}


(6)编写client类


package testICE;

import com.rain.service.demo.HelloPrx;
import com.rain.service.demo.HelloPrxHelper;

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("hello:default -p 12000");
			HelloPrx hello = HelloPrxHelper.checkedCast(base);
			if(hello == null){
				throw new Error("Invalid proxy");
			}
			String s = hello.sayHello("World!");
			System.out.println(">>"+s);
		}catch(Ice.LocalException e){
			e.printStackTrace();
			status = 1;
		}catch(Exception e){
			System.err.println(e.getMessage());
			status = 1;
		}
		if(ic != null){
			try{
				ic.destroy();
			}catch(Exception e){
				System.err.println(e.getMessage());
				status = 1;
			}
		}
		System.exit(status);
	}

}
文件目录为:


(7)测试

运行service->运行client
service控制台输出


client控制台输出














  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值