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.

On this page:

Configuring a Per-Proxy Request Context Programmatically

The proxy methods ice_context and ice_getContext set and retrieve the context, respectively. The Slice definitions of these methods would look as follows:

Object* ice_context(Context ctx);
Context ice_getContext();

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.

Here is an example in C++:

Ice::Context ctx;
ctx["write policy"] = "immediate";

PersonPrx p1 = ...;
PersonPrx p2 = p1->ice_context(ctx);

Address a = ...;

p1->setAddress(a);       // Sends no context

p2->setAddress(a);       // Sends ctx implicitly

Ice::Context ctx2;
ctx2["write policy"] = "delayed";

p2->setAddress(a, ctx2); // Sends ctx2 instead

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

As of Ice 3.6, you can also configure a context with proxy properties when you use the communicator operation propertyToProxy. Suppose we obtain a proxy like this:

Ice::CommunicatorPtr communicator = ...;
Ice::ObjectPrx proxy = communicator->propertyToProxy("PersonProxy");

We can statically 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.

See Also