Per-Proxy Request Contexts
Instead of passing a context explicitly with an invocation, you can also use a per-proxy context. Per-proxy contexts allow you to set a context on a particular proxy once and, thereafter, whenever you use that proxy to invoke an operation, the previously-set context is sent with each invocation.
Configuring a Per-Proxy Request Context Programmatically
The proxy methods ice_context
and ice_getContext
set and retrieve the context, respectively. ice_context
creates a new proxy that stores the given context. Calling ice_getContext
returns the stored context, or an empty dictionary if no per-proxy context has been configured for the proxy.
python
As the example illustrates, once we have created the p2
proxy, any invocation via p2
automatically sends the configured context. The final line of the example shows that it is also possible to explicitly send a context for an invocation even if the proxy is configured with a context — an explicit context always overrides any per-proxy context.
Note that, once you have set a per-proxy context, that context becomes immutable: if you subsequently change the context you have passed to ice_context
, such a change does not affect the per-proxy context of any proxies you previously created with that context because each proxy on which you set a per-proxy context stores its own copy of the dictionary.
Configuring a Per-Proxy Request Context using Properties
You can also configure a context with proxy properties when you use the communicator method propertyToProxy
. Suppose we obtain a proxy like this:
python
We can configure a context for this proxy using the following properties:
PersonProxy=person:tcp -p 5000
PersonProxy.Context.write policy=immediate
PersonProxy.Context.failure mode=persistent
The Context property has the form name.Context.key=value
, where key
and value
can be any legal property symbols.
The proxy returned by propertyToProxy
already contains the context key/value pairs specified in the configuration properties. To make any modifications to the context at run time, you'll need to retrieve the proxy's context dictionary using ice_getContext
, modify the dictionary as necessary, and finally obtain a new proxy by calling ice_context
, as we described above.