Creating Contexts

Contexts are used for organizing communications between applications and a Solace event broker. Each client application must use a minimum of one context. A context acts as a container in which one or more sessions can be configured and session‑related events can be handled. It encapsulates threads that drive network I/O and message delivery notification for the sessions and session components associated with that context.

Context properties are used to dictate the behavior of a context. If no specific context properties are specified, the default values are used. For information on the context properties see the API Developer Online Reference documentation for the Solace .NET API.

Delegates

In the Solace .NET API, all delegate invocations (for example, message receive delegates, event delegates, and timer delegates) from the API to the application run from the context thread. For optimal performance, the following rules should be followed when developing your applications for use with Solace.

  • The context thread must run to detect relief from flow control and unblock waiting application threads.
  • The context thread must run to complete the session connection sequence and unblock applications waiting for the connection to complete.
  • The context thread must run to unblock applications waiting for confirmation of subscription requests.

Consequently, applications must not block in delegate routines because it can deadlock the application or at a minimum severely degrade receive performance. Deferring processing by running for excessively long periods of time in the delegate routines prevents the API from unblocking other application threads that might be waiting for confirmation of sent messages, or be blocked in flow-control situations.

To create a context, call the appropriate method. Once created, a context is automatically started.

To Create a Context

In the Solace .NET API, use:

ContextFactory.CreateContext(...)

Related Samples

For an example of how to create a context in the Solace .NET API, see the DirectPubSub sample.

To Destroy a Context

In the Solace .NET API, use:

IContext.Dispose()

Context Timers

In the Solace .NET API, contexts can use a timer to perform a scheduled task. These tasks are guaranteed to execute on the thread driving the context with which the timer tasks are registered.

The context timer relies on a defined delegate, which allows a delegate to be invoked within the context's thread when a set interval of time expires. A repeating or a one-shot (that is, one-time) delegate can be used. For example, a timer could be used to publish messages at set intervals or to perform an asynchronous operation just once.

To start a context timer, call the appropriate method. You must pass in the type of timer to start (one-shot or repeat), the requested timer duration (in milliseconds), and the delegate to invoke when the timer expires.

When the timer expires, the specified timer delegate is invoked. If it is a repeat timer, the timer is automatically rescheduled for the same duration, and it continues to run until the timer is stopped. If it is a one-shot timer, the timer is automatically canceled.

To Start a Context Timer

In the Solace .NET API, use:

TimerTask.Start(...)

Contexts, sessions, flows, or timers cannot be disposed in a delegate.