Ice Session的使用

来源:互联网 发布:查英语单词的软件 编辑:程序博客网 时间:2024/05/21 01:52

IceGrid提供了一种资源分配的方案,用于协调访问IceGrid应用的对象和服务。客户端创建session首先要通过IceGrid注册服务或者Glacier2路由器的验证,才能获得资源服务的使用权。如果客户端在一段时间内(默认60秒)没有keepAlive或者主动放弃session,资源服务会被释放(注:资源服务被分配,资源服务会被激活启动,释放的时候会被关闭),这时其它客户端才能有机会获得资源服务(资源服务没被释放之前,其它客户端要获取资源服务是被阻塞的,所以我在想这种应用场景是不是可以用来实现服务器分布式锁?)。

 

客户端的代码:

// DemoIceGrid/Registry是注册服务启动时默认创建的对象IceGrid::RegistryPrxregistry = IceGrid::RegistryPrx::checkedCast(       communicator()->stringToProxy("DemoIceGrid/Registry"));string username = “user1”; string password = “cw”; IceGrid::SessionPrx session; try {      //username和password在服务器端配置    session = registry->createSession(username, password); } catch (const IceGrid::PermissionDeniedException & ex) {     cout << "permission denied:\n" << ex.reason << endl; } HelloPrx hello;try{//获得session之后,我们就可以通过session获得服务对象代理了         hello =HelloPrx::checkedCast(session->allocateObjectById(communicator()->stringToIdentity("hello")));}catch(constIceGrid::ObjectNotRegisteredException&){         hello =HelloPrx::checkedCast(session->allocateObjectByType("::Demo::Hello"));}

Session是有时效的,默认是60秒,要保持不失效,就得通过调用session->keepAlive()方法。可通过调用session->destroy()方法主动销毁session,释放资源对象。

 

服务器端的配置:

1)        Username和password是需要配置的,首先创建一个存储username和password的文件passwords,存储的格式(username+空白符+password,password是通过openssl passwd命令加密过了)如:

user1  G57Smspmieflg

user2  G57Smspmieflg

user3  G57Smspmieflg

 

2)        然后编辑启动注册服务的配置config.registry,如果有配置IceGrid.Registry.PermissionsVerifier,注释掉,然后增加IceGrid.Registry.CryptPasswords,重启注册服务即可。

#IceGrid.Registry.PermissionsVerifier=DemoIceGrid/NullPermissionsVerifier

IceGrid.Registry.CryptPasswords=passwords

 

3)        配置application.xml

通过allocatable项配置object id

<allocatableidentity="hello" type="::Demo::Hello"property="Identity"/>

 

参考官方文档:

http://doc.zeroc.com/display/Ice/Resource+Allocation+using+IceGrid+Sessions

官方源码例子:

Ice-3.5.1/cpp/demo/IceGrid/sessionActivation

0 0
原创粉丝点击