Zeroc ICE 之zeroc Registry(Java)

大憨熊 提交于 2019-12-02 04:52:19

zeroc文档很少,都是我采用“穷举法“踩坑,一个一个摸索。

 

 

 

 

 

 

 

 

1.首先在windows中安装zeroc ice,在eclipse中安装Ice Builder插件

2.新建一个java项目(如上图),然后用Ice Builder插件构建生成generated文件夹。在项目下新建文件夹slice

3.在slice文件夹下新建service.ice和SMSService.ice(如上图)

service.ice代码如下:

[["java:package:com.hp.tel.ice"]]
module book{
	struct Message{
		string name;
		int type;
		bool valid;
		double price;
		string content;
	};
	
	interface OnlineBook{
	
		Message bookTick(Message msg);
	};
	
} ;

SMSService.ice代码如下:

[["java:package:com.hp.tel.ice"]]
module message{
	interface SMSService{
	
		void sendSMs(string msg);
	};
};

然后,会在generated文件下生成代码。

4.OnlineBookI2.java 代码如下:

package com.hp.impl;

import com.hp.tel.ice.book.Message;
import com.hp.tel.ice.book._OnlineBookDisp;

import Ice.Communicator;
import Ice.Current;
import Ice.ObjectAdapter;
import IceBox.Service;

public class OnlineBookI2 extends _OnlineBookDisp implements Service{

	private ObjectAdapter _adapter;
	private static final long serialVersionUID = 3419811003893579926L;

	@Override
	public Message bookTick(Message s, Current __current) {

		System.out.println(" bookTick to call. "+this.getClass().getName());
		System.out.println(s.content);
		return s;
	}

	@Override
	public void start(String name, Communicator communicator, String[] args) {
		_adapter = communicator.createObjectAdapter(name);
		Ice.Object object = this;
		_adapter.add(object, communicator.stringToIdentity(name));
		_adapter.activate();
		System.out.println(name +" started");
	}

	@Override
	public void stop() {
		System.out.println(this._adapter.getName()+" stopped");
		_adapter.destroy();
	}

	
}

5.SMSServiceI2.java代码如下:

package com.hp.impl;

import com.hp.tel.ice.book.Message;
import com.hp.tel.ice.book.OnlineBookPrx;
import com.hp.tel.ice.book.OnlineBookPrxHelper;
import com.hp.tel.ice.message._SMSServiceDisp;

import Ice.Communicator;
import Ice.Current;
import Ice.ObjectAdapter;
import IceBox.Service;

public class SMSServiceI2 extends _SMSServiceDisp implements Service{

	private static final long serialVersionUID = 8374639098846448601L;
	private ObjectAdapter _adapter;
	
	
	@Override
	public void sendSMs(String msg, Current __current) {
		System.out.println("send msag "+msg);
		if(msg.startsWith("book")) {
			try {
				Ice.ObjectPrx base = _adapter.getCommunicator().stringToProxy("OnlineBook");
				OnlineBookPrx onlineBook = OnlineBookPrxHelper.checkedCast(base);
				Message bookMsg = new Message();
				bookMsg.name = "Mr Wang";
				bookMsg.type = 3;
				bookMsg.price = 99.99;
				bookMsg.valid = true;
				bookMsg.content = "abcdef";
				onlineBook.bookTick(bookMsg);
			} catch (Exception e) {

				throw new RuntimeException(e);
			}
		}
	}

	@Override
	public void start(String name, Communicator communicator, String[] args) {
		
		_adapter = communicator.createObjectAdapter(name);
		Ice.Object object = this;
		_adapter.add(object, communicator.stringToIdentity(name));
		_adapter.activate();
		System.out.println("SMS started");
	}

	@Override
	public void stop() {
		System.out.println("SMS stoped");
		_adapter.destroy();
	}

	

	
}

6.OnlineBookClient.java代码如下:

package com.hp.client;

import com.hp.tel.ice.book.Message;
import com.hp.tel.ice.book.OnlineBookPrx;
import com.hp.tel.ice.book.OnlineBookPrxHelper;

public class OnlineBookClient {

	public static void main(String[] args) {
		
		int status = 0;
		Ice.Communicator ic = null;
		
		try {
			ic = Ice.Util.initialize(args);
			Ice.ObjectPrx base = ic.stringToProxy("OnlineBook:default -p 10000");
			OnlineBookPrx onlineBook = OnlineBookPrxHelper.checkedCast(base);
			if(onlineBook==null) {
				throw new Error("Invalid proxy");
			}
			//调用服务方法
			Message msg = new Message();
			msg.name = "Mr Wang";
			msg.type = 3;
			msg.price = 99.99;
			msg.valid = true;
			msg.content = "aaaaa";
			System.out.println(onlineBook.bookTick(msg).content);
		} catch (Exception e) {
			e.printStackTrace();
			status = 1;
		}finally {
			if(ic!=null) {
				ic.destroy();
			}
		}
		
		System.exit(status);
	}
}

7.SMSServiceClient.java代码如下:

package com.hp.client;

import com.hp.tel.ice.book.Message;
import com.hp.tel.ice.book.OnlineBookPrx;
import com.hp.tel.ice.book.OnlineBookPrxHelper;
import com.hp.tel.ice.message.SMSServicePrx;
import com.hp.tel.ice.message.SMSServicePrxHelper;

import Ice.AsyncResult;
import Ice.Callback;

public class SMSServiceClient {

public static void main(String[] args) {
		
		int status = 0;
		Ice.Communicator ic = null;
		
		try {
//			
			ic = Ice.Util.initialize(new String[] {"--Ice.Default.Locator=DemoIceGrid/Locator:tcp -h localhost -p 4061"});
			Ice.ObjectPrx objectPrx = ic.stringToProxy("SMSService@SMSServiceAdapter");
			SMSServicePrx smsService = SMSServicePrxHelper.checkedCast(objectPrx);
			if(smsService==null) {
				throw new Error("Invalid proxy");
			}
			System.out.println("123");
			
			smsService.sendSMs("aook");
			/*OnlineBookPrx onlineBook = OnlineBookPrxHelper.checkedCast(base);
			if(onlineBook==null) {
				throw new Error("Invalid proxy");
			}
			//调用服务方法
			Message msg = new Message();
			msg.name = "Mr Wang";
			msg.type = 3;
			msg.price = 99.99;
			msg.valid = true;
			msg.content = "aaaaa";
			System.out.println(onlineBook.bookTick(msg).content);*/
		} catch (Exception e) {
			e.printStackTrace();
			status = 1;
		}finally {
			if(ic!=null) {
				ic.destroy();
			}
		}
		
		System.exit(status);
	}
}

8.config.properties

#server properties
IceBox.InstanceName = MyAppIceBox 1
IceBox.InheritProperties = 1
IceBox.PrintServicesReady = MyAppIceBox 1
IceBox.ServiceManager.Endpoints =tcp -p 9998 -h localhost
#performance properties
Ice.ThreadPool.Server.Size = 4
Ice.ThreadPool.Server.SizeMax = 100
Ice.ThreadPool.Server.SizeWarn = 40
Ice.ThreadPool.Client.Size = 4
Ice.ThreadPool.Client.SizeMax = 100
Ice.ThreadPool.Client.SizeWarn = 40
#for system stronger
Ice.ACM.Client = 300
Ice.ACM.Server = 300
#log and trace
Ice.PrintStackTraces = 1
Ice.Trace.Retry = 2
Ice.Trace.Network = 2
Ice.Trace.ThreadPool = 1
Ice.Trace.Locator = 2
Ice.Warn.Connections = 1
Ice.Warn.Dispatch = 1
Ice.Warn.Endpoints = 1
#service define begin
IceBox.Service.OnlineBook = com.hp.impl.OnlineBookI2 prop1=1 prop2=2 prop3=3
OnlineBook.Endpoints =tcp -p 10000 -h localhost

IceBox.Service.SMSService = com.hp.impl.SMSServiceI2
SMSService.Endpoints = tcp -p 10001 -h localhost

#service load order
IceBox.LoadOrder=OnlineBook,SMSService
IceBox.UseSharedCommunicator.OnlineBook=1
IceBox.UseSharedCommunicator.SMSService=1

#Ice Registry
Ice.Default.Locator=DemoIceGrid/Locator:tcp -h localhost -p 4061
OnlineBook.AdapterId=OnlineBookAdapter
SMSService.AdapterId=SMSServiceAdapter

注意注意这个DemoIceGrid与后面的registry.cfg中的IceGrid.InstanceName=DemoIceGrid相对应

重要的事情说三遍!!!

9.

 

 

 

 

 

 

 

 

 

在E盘下新建TestIce文件夹,在这个文件夹下新建registry.cfg和node1.cfg,配置文件中有路径设置,请自己设置,否则报错

registry.cfg代码如下:

#IceGrid instance name.  实例名
IceGrid.InstanceName=DemoIceGrid
IceGrid.Registry.Client.Endpoints=tcp -h localhost -p 4061
IceGrid.Registry.Server.Endpoints=tcp -h localhost
IceGrid.Registry.Internal.Endpoints=tcp -h localhost
IceGrid.Registry.PermissionsVerifier=DemoIceGrid/NullPermissionsVerifier
IceGrid.Registry.Data=E:\registry
IceGrid.Registry.DynamicRegistration=1

node1.cfg代码如下:


Ice.Default.Locator=DemoIceGrid/Locator:tcp -h localhost -p 4061
IceGrid.Node.Data=E:\TestIce\node\data
IceGrid.Node.Endpoints=tcp -p 5062
#指定节点1的名称
IceGrid.Node.Name=node1
Ice.StdErr E:\TestIce\node\node.stderr.log
#指定错误日志文件
Ice.StdOut E:\TestIce\node\node.stdout.log

10.在E:\TestIce目录下打开cmd.exe

输入icegridregistry --Ice.Config=registry.cfg     启动registry

输入 icegridnode --Ice.Config=node1.cfg    启动node

11.eclipse中启动配置如图:

点击项目名称--> run as -->

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OK完结

 

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!