十九、共享对象函数

200 阅读3分钟

十九、共享对象函数

共享对象函数用于得到、注册共享对象,它们通常在分布式应用程序中使用。


1、SharedObjectDirectory()

功  能:得到用于共享的一组已注册对象的名称。

语  法:SharedObjectDirectory ( instancenames {,classnames} )

参  数:instancenames:String类型动态数组,用于存储已注册共享对象的名称;

        classnames:String类型动态数组,用于存储已注册共享对象的类名。

返回值:ErrorReturn枚举类型。函数执行成功时返回Success!,发生错误时返回FeatureNotSupportedError!

用  法:共享对象只能在服务器的主会话或在服务器上为每个客户连接创建的客户会话中访问。客户应用程序不能够直接使用共享对象,因此,要得到已注册共享对象的名称,必须在服务器的执行上下文中调用该函数,或者在服务器上的某个客户会话中调用该函数。

示  例:In this example, the application retrieves the list of shared objects and their class names:

integer status

string InstanceNames[]

string ClassNames[]

status = SharedObjectDirectory(InstanceNames,ClassNames)


2、SharedObjectGet()

功  能:得到指定共享对象的引用。

语  法:SharedObjectGet ( instancenames , objectinstance )

参  数:instancenames:指定要得到其引用的共享对象实例的名称;

        objectinstance:指定用于存储共享对象引用的对象变量。

返回值:ErrorReturn枚举类型。函数执行成功时返回Success!,不能创建共享对象的本地引用时返回SharedObjectCreateInstanceError!,指定的共享对象不存在或尚未注册返回SharedObjectNotExistsError!

用  法:SharedObjectGet()函数得到由SharedObjectRegister()函数注册的某共享对象的引用。得到对象引用后,就可以像使用其他对象那样使用该对象的方法、属性了。

示  例:This example shows how you might use a shared object to make an asynchronous request against an EAServer component method and return data back to a client application window. The client has a Retrieve button on a window, a SetDW function, a shared object, and a callback handler. The component deployed to EAServer retrieves employee information from a database.

The Retrieve button on the window creates a shared object that communicates with EAServer as well as an instance of a callback handler:

// instance variables

// uo_sharedobject iuo_sharedobject

// uo_callback iuo_callback

long ll_rv

SharedObjectRegister("uo_sharedobject","myshare")

SharedObjectGet("myshare",iuo_sharedobject)

iuo_callback = CREATE uo_callback

// Pass a reference to the window to

// the callback object

iuo_callback.passobject (parent)

iuo_sharedobject.post retrievedata(iuo_callback)

The SetDW function applies the contents of the DataWindow blob returned from the EAServer component to a DataWindow control in the window:

long ll_rv

ll_rv = dw_employee.SetFullState(ablb_data)

if ll_rv = -1 then

   MessageBox("Error", "SetFullState call failed!")

end if

return ll_rv

The Constructor event of the shared object uses a custom Connection object called n_jagclnt_connect to connect to the server. Then it creates an instance of the EAServer component:

// Instance variables

// uo_employee iuo_employee

// n_jagclnt_connect myconnect

Constructor event

long ll_rc

myconnect = create n_jagclnt_connect

ll_rc = myconnect.ConnectToServer()

ll_rv = myconnect.CreateInstance(iuo_employee, "uo_employee")

The shared object has a single function called RetrieveData that makes a synchronous call to the RetrieveData function on the EAServer component. When the function completes processing, it calls the Notify function on the callback object, passing it the DataWindow blob returned from the server component:

blob lblb_data

long ll_rv

ll_rv = iuo_employee.retrievedata(lblb_data)

auo_callback.notify(lblb_data)

return ll_rv

When the EAServer component has finished processing, the shared object notifies a user object called uo_callback, which in turns notifies the w_employee window. The uo_callback object has two functions, Notify and PassObject.The Notify function calls a function called SetDW on the w_employee window, passing it the DataWindow blob returned from the server component:

long ll_rv

ll_rv = iw_employee.setdw(ablb_data)

if ll_rv = -1 then

   MessageBox("Error", "SetDW call failed!")

end if

return ll_rv

The callback handler's PassObject function caches a reference to the w_employee window in the iw_employee instance variable. The function takes the argument aw_employee, which is of type w_employee, and returns a long value:

iw_employee = aw_employee

return 1

The EAServer component is a PowerBuilder user object called uo_employee. The uo_employee object has a function called RetrieveData that uses a DataStore to retrieve employee rows from the database:

// instance variables

// protected TransactionServer txnsrv

// protected DataStore ids_datastore

long ll_rv

ll_rv = ids_datastore.Retrieve()

ll_rv = ids_datastore.GetFullState(ablb_data)

txnsrv.SetComplete()

return ll_rv


3、SharedObjectRegister()

功  能:将指定的用户对象注册为共享对象。

语  法:SharedObjectRegister ( classnames ,instancename )

参  数:classnames:指定希望共享的用户对象的名称;

        instancename:指定要赋值给该共享对象的类名。

返回值:ErrorReturn枚举类型。各返回值的意义为:

        Success! -- 函数执行成功;

        SharedObjectExistsError! -- 指定的共享对象名称已经被使用;

        SharedObjectCreateInstanceError! -- 不能创建指定的共享对象;

        SharedObjectCreatePBSessionError! -- 不能创建共享对象对话;

用  法:当调用SharedObjectRegister()函数时,PowerBuilder为共享对象打开一个运行时会话,并创建该共享对象。该函数中指定的对象名称用于通过SharedObjectGet()函数访问这个共享对象。

示  例:In this example, the user object uo_customers is registered so that it can be shared. The name assigned to the shared object instance is share1. After registering the object, the application uses the SharedObjectGet function to store an instance of the object in an object variable:

SharedObjectRegister("uo_customers", "share1")

SharedObjectGet("share1",shared_object)