Package io.aeron

Class Aeron.Context

All Implemented Interfaces:
Cloneable
Enclosing class:
Aeron

public static class Aeron.Context extends CommonContext
Provides a means to override configuration for an Aeron client via the Aeron.connect(Aeron.Context) method and its overloads. It gives applications some control over the interactions with the Aeron Media Driver. It can also set up error handling as well as application callbacks for image information from the Media Driver.

A number of the properties are for testing and should not be set by end users.

Note: Do not reuse instances of the context across different Aeron clients.

The context will be owned by ClientConductor after a successful Aeron.connect(Context) and closed via Aeron.close().

  • Constructor Details

    • Context

      public Context()
  • Method Details

    • clone

      public Aeron.Context clone()
      Perform a shallow copy of the object.
      Overrides:
      clone in class CommonContext
      Returns:
      a shallow copy of the object.
    • conclude

      public Aeron.Context conclude()
      This is called automatically by Aeron.connect(Aeron.Context) and its overloads. There is no need to call it from a client application. It is responsible for providing default values for options that are not individually changed through field setters.
      Overrides:
      conclude in class CommonContext
      Returns:
      this for a fluent API.
    • clientId

      public long clientId()
      Get the client identity that has been allocated for communicating with the media driver.
      Returns:
      the client identity that has been allocated for communicating with the media driver.
    • clientName

      public Aeron.Context clientName(String clientName)
      Sets the name used to identify this client among other clients connected to the media driver.
      Parameters:
      clientName - to use.
      Returns:
      this for a fluent API.
      Since:
      1.44.0
      See Also:
    • clientName

      public String clientName()
      Returns the name of this client.
      Returns:
      name of this client or empty String if not configured.
      Since:
      1.44.0
      See Also:
    • useConductorAgentInvoker

      public Aeron.Context useConductorAgentInvoker(boolean useConductorAgentInvoker)
      Should an AgentInvoker be used for running the ClientConductor rather than run it on a thread with a AgentRunner.
      Parameters:
      useConductorAgentInvoker - use AgentInvoker be used for running the ClientConductor?
      Returns:
      this for a fluent API.
    • useConductorAgentInvoker

      public boolean useConductorAgentInvoker()
      Should an AgentInvoker be used for running the ClientConductor rather than run it on a thread with a AgentRunner.
      Returns:
      true if the ClientConductor will be run with an AgentInvoker otherwise false.
    • preTouchMappedMemory

      public Aeron.Context preTouchMappedMemory(boolean preTouchMappedMemory)
      Should mapped-memory be pre-touched to avoid soft page faults.
      Parameters:
      preTouchMappedMemory - true if mapped-memory should be pre-touched otherwise false.
      Returns:
      this for a fluent API.
      See Also:
    • preTouchMappedMemory

      public boolean preTouchMappedMemory()
      Should mapped-memory be pre-touched to avoid soft page faults.
      Returns:
      true if mapped-memory should be pre-touched otherwise false.
      See Also:
    • driverAgentInvoker

      public Aeron.Context driverAgentInvoker(AgentInvoker driverAgentInvoker)
      Set the AgentInvoker for the Media Driver to be used while awaiting a synchronous response.

      Useful for when running on a low thread count scenario.

      Parameters:
      driverAgentInvoker - to be invoked while awaiting a response in the client.
      Returns:
      this for a fluent API.
    • driverAgentInvoker

      public AgentInvoker driverAgentInvoker()
      Get the AgentInvoker that is used to run the Media Driver while awaiting a synchronous response.
      Returns:
      the AgentInvoker that is used for running the Media Driver.
    • clientLock

      public Aeron.Context clientLock(Lock lock)
      The Lock that is used to provide mutual exclusion in the Aeron client.

      If the useConductorAgentInvoker() is set and only one thread accesses the client then the lock can be set to NoOpLock to elide the lock overhead.

      Parameters:
      lock - that is used to provide mutual exclusion in the Aeron client.
      Returns:
      this for a fluent API.
    • clientLock

      public Lock clientLock()
      Get the Lock that is used to provide mutual exclusion in the Aeron client.
      Returns:
      the Lock that is used to provide mutual exclusion in the Aeron client.
    • epochClock

      public Aeron.Context epochClock(EpochClock clock)
      Set the EpochClock to be used for tracking wall clock time when interacting with the driver.
      Parameters:
      clock - EpochClock to be used for tracking wall clock time when interacting with the driver.
      Returns:
      this for a fluent API.
    • epochClock

      public EpochClock epochClock()
      Get the EpochClock used by the client for the epoch time in milliseconds.
      Returns:
      the EpochClock used by the client for the epoch time in milliseconds.
    • nanoClock

      public Aeron.Context nanoClock(NanoClock clock)
      Set the NanoClock to be used for tracking high resolution time.
      Parameters:
      clock - NanoClock to be used for tracking high resolution time.
      Returns:
      this for a fluent API.
    • nanoClock

      public NanoClock nanoClock()
      Get the NanoClock to be used for tracking high resolution time.
      Returns:
      the NanoClock to be used for tracking high resolution time.
    • idleStrategy

      public Aeron.Context idleStrategy(IdleStrategy idleStrategy)
      Provides an IdleStrategy for the thread responsible for the client duty cycle.
      Parameters:
      idleStrategy - Thread idle strategy for the client duty cycle.
      Returns:
      this for a fluent API.
    • idleStrategy

      public IdleStrategy idleStrategy()
      Get the IdleStrategy employed by the client for the client duty cycle.
      Returns:
      the IdleStrategy employed by the client for the client duty cycle.
    • awaitingIdleStrategy

      public Aeron.Context awaitingIdleStrategy(IdleStrategy idleStrategy)
      Provides an IdleStrategy to be used when awaiting a response from the Media Driver.
      Parameters:
      idleStrategy - Thread idle strategy for awaiting a response from the Media Driver.
      Returns:
      this for a fluent API.
    • awaitingIdleStrategy

      public IdleStrategy awaitingIdleStrategy()
      The IdleStrategy to be used when awaiting a response from the Media Driver.

      This can be changed to a BusySpinIdleStrategy or YieldingIdleStrategy for lower response time, especially for adding counters or releasing resources, at the expense of CPU usage.

      Returns:
      the IdleStrategy to be used when awaiting a response from the Media Driver.
    • toClientBuffer

      public CopyBroadcastReceiver toClientBuffer()
      The buffer used for communicating from the media driver to the Aeron client.
      Returns:
      the buffer used for communicating from the media driver to the Aeron client.
    • toDriverBuffer

      public RingBuffer toDriverBuffer()
      Get the RingBuffer used for sending commands to the media driver.
      Returns:
      the RingBuffer used for sending commands to the media driver.
    • driverProxy

      public DriverProxy driverProxy()
      Get the proxy for communicating with the media driver.
      Returns:
      the proxy for communicating with the media driver.
    • errorHandler

      public Aeron.Context errorHandler(ErrorHandler errorHandler)
      Handle Aeron exceptions in a callback method. The default behavior is defined by Aeron.Configuration.DEFAULT_ERROR_HANDLER. This is the error handler which will be used if an error occurs during the callback for poll operations such as Subscription.poll(FragmentHandler, int).

      The error handler can be reset after Aeron.connect() and the latest version will always be used so that the bootstrapping process can be performed such as replacing the default one with a CountedErrorHandler.

      Parameters:
      errorHandler - Method to handle objects of type Throwable.
      Returns:
      this for a fluent API.
      See Also:
    • errorHandler

      public ErrorHandler errorHandler()
      Get the error handler that will be called for errors reported back from the media driver or during poll operations.
      Returns:
      the error handler that will be called for errors reported back from the media driver or poll operations.
    • subscriberErrorHandler

      public Aeron.Context subscriberErrorHandler(ErrorHandler errorHandler)
      The error handler which will be used if an error occurs during the callback for poll operations such as Subscription.poll(FragmentHandler, int). The default will be errorHandler() if not set.
      Parameters:
      errorHandler - Method to handle objects of type Throwable.
      Returns:
      this for a fluent API.
      See Also:
    • subscriberErrorHandler

      public ErrorHandler subscriberErrorHandler()
      This is the error handler which will be used if an error occurs during the callback for poll operations such as Subscription.poll(FragmentHandler, int). The default will be errorHandler() if not set. To have Subscription.poll(FragmentHandler, int) not delegate then set with RethrowingErrorHandler.
      Returns:
      the error handler that will be called for errors reported back from the media driver.
    • availableImageHandler

      public Aeron.Context availableImageHandler(AvailableImageHandler handler)
      Set up a default callback for when an Image is available.
      Parameters:
      handler - Callback method for handling available image notifications.
      Returns:
      this for a fluent API.
    • availableImageHandler

      public AvailableImageHandler availableImageHandler()
      Get the default callback handler for notifying when Images become available.
      Returns:
      the callback handler for notifying when Images become available.
    • unavailableImageHandler

      public Aeron.Context unavailableImageHandler(UnavailableImageHandler handler)
      Set up a default callback for when an Image is unavailable.
      Parameters:
      handler - Callback method for handling unavailable image notifications.
      Returns:
      this for a fluent API.
    • unavailableImageHandler

      public UnavailableImageHandler unavailableImageHandler()
      Get the callback handler for when an Image is unavailable.
      Returns:
      the callback handler for when an Image is unavailable.
    • availableCounterHandler

      public Aeron.Context availableCounterHandler(AvailableCounterHandler handler)
      Set up a callback for when a counter is available. This will be added to the list first before additional handler are added with Aeron.addAvailableCounterHandler(AvailableCounterHandler).
      Parameters:
      handler - to be called for handling available counter notifications.
      Returns:
      this for a fluent API.
    • availableCounterHandler

      public AvailableCounterHandler availableCounterHandler()
      Get the callback handler for when a counter is available.
      Returns:
      the callback handler for when a counter is available.
    • unavailableCounterHandler

      public Aeron.Context unavailableCounterHandler(UnavailableCounterHandler handler)
      Set up a callback for when a counter is unavailable. This will be added to the list first before additional handler are added with Aeron.addUnavailableCounterHandler(UnavailableCounterHandler).
      Parameters:
      handler - to be called for handling unavailable counter notifications.
      Returns:
      this for a fluent API.
    • unavailableCounterHandler

      public UnavailableCounterHandler unavailableCounterHandler()
      Get the callback handler for when a counter is unavailable.
      Returns:
      the callback handler for when a counter is unavailable.
    • closeHandler

      public Aeron.Context closeHandler(Runnable handler)
      Set a Runnable that is called when the client is closed by timeout or normal means.

      It is not safe to call any API functions from any threads after this hook is called. In addition, any in flight calls may still cause faults. Thus treating this as a hard error and terminate the process in this hook as soon as possible is recommended.

      Parameters:
      handler - that is called when the client is closed.
      Returns:
      this for a fluent API.
    • closeHandler

      public Runnable closeHandler()
      Get the Runnable that is called when the client is closed by timeout or normal means.
      Returns:
      the Runnable that is called when the client is closed.
    • keepAliveIntervalNs

      public Aeron.Context keepAliveIntervalNs(long value)
      Set the interval in nanoseconds for which the client will perform keep-alive operations.
      Parameters:
      value - the interval in nanoseconds for which the client will perform keep-alive operations.
      Returns:
      this for a fluent API.
    • keepAliveIntervalNs

      public long keepAliveIntervalNs()
      Get the interval in nanoseconds for which the client will perform keep-alive operations.
      Returns:
      the interval in nanoseconds for which the client will perform keep-alive operations.
    • driverTimeoutMs

      public Aeron.Context driverTimeoutMs(long value)
      Set the amount of time, in milliseconds, that this client will wait until it determines the Media Driver is unavailable. When this happens a DriverTimeoutException will be generated for the error handler.
      Overrides:
      driverTimeoutMs in class CommonContext
      Parameters:
      value - Number of milliseconds.
      Returns:
      this for a fluent API.
      See Also:
    • interServiceTimeoutNs

      public long interServiceTimeoutNs()
      Return the timeout between service calls to the duty cycle for the client.

      When exceeded, errorHandler() will be called and the active Publications, Images, and Counters will be closed.

      This value is controlled by the driver and included in the CnC file. It can be configured by adjusting the aeron.client.liveness.timeout property set on the media driver.

      Returns:
      the timeout in nanoseconds between service calls as an allowed maximum.
    • idleSleepDurationNs

      public Aeron.Context idleSleepDurationNs(long idleSleepDurationNs)
      Duration to sleep when conductor is idle.
      Parameters:
      idleSleepDurationNs - to sleep when conductor is idle.
      Returns:
      this for a fluent API.
      See Also:
    • idleSleepDurationNs

      public long idleSleepDurationNs()
      Duration to sleep when conductor is idle.
      Returns:
      duration in nanoseconds to sleep when conductor is idle.
      See Also:
    • resourceLingerDurationNs

      public Aeron.Context resourceLingerDurationNs(long resourceLingerDurationNs)
      Duration to wait while lingering an entity such as an Image before deleting underlying resources such as memory mapped files.
      Parameters:
      resourceLingerDurationNs - to wait before deleting an expired resource.
      Returns:
      this for a fluent API.
      See Also:
    • resourceLingerDurationNs

      public long resourceLingerDurationNs()
      Duration to wait while lingering an entity such as an Image before deleting underlying resources such as memory mapped files.
      Returns:
      duration in nanoseconds to wait before deleting an expired resource.
      See Also:
    • closeLingerDurationNs

      public Aeron.Context closeLingerDurationNs(long closeLingerDurationNs)
      Duration to linger on closing to allow publishers and subscribers time to notice closed resources.

      This value can be increased from the default to a few seconds to better cope with long GC pauses or resource starved environments. Issues could manifest as seg faults using files after they have been unmapped from publishers or subscribers not noticing the close in a timely fashion.

      Parameters:
      closeLingerDurationNs - to wait before deleting resources when closing.
      Returns:
      this for a fluent API.
      See Also:
    • closeLingerDurationNs

      public long closeLingerDurationNs()
      Duration to linger on closing to allow publishers and subscribers time to notice closed resources.

      This value can be increased from the default to a few seconds to better cope with long GC pauses or resource starved environments. Issues could manifest as seg faults using files after they have been unmapped from publishers or subscribers not noticing the close in a timely fashion.

      Returns:
      duration in nanoseconds to wait before deleting resources when closing.
      See Also:
    • aeronDirectoryName

      public Aeron.Context aeronDirectoryName(String dirName)
      Set the top level Aeron directory used for communication between the client and Media Driver, and the location of the data buffers.
      Overrides:
      aeronDirectoryName in class CommonContext
      Parameters:
      dirName - New top level Aeron directory.
      Returns:
      this for a fluent API.
    • threadFactory

      public Aeron.Context threadFactory(ThreadFactory threadFactory)
      Specify the thread factory to use when starting the conductor thread.
      Parameters:
      threadFactory - thread factory to construct the thread.
      Returns:
      this for a fluent API.
    • threadFactory

      public ThreadFactory threadFactory()
      The thread factory to be used to construct the conductor thread.
      Returns:
      the specified thread factory or Thread(Runnable) if none is provided.
    • publicationErrorFrameHandler

      public Aeron.Context publicationErrorFrameHandler(PublicationErrorFrameHandler publicationErrorFrameHandler)
      Set the handler to receive error frames that have been received by the local driver for publications added by this client.
      Parameters:
      publicationErrorFrameHandler - to be called back when an error frame is received.
      Returns:
      this for a fluent API.
      Since:
      1.47.0
    • publicationErrorFrameHandler

      public PublicationErrorFrameHandler publicationErrorFrameHandler()
      Get the handler to receive error frames that have been received by the local driver for publications added by this client.
      Returns:
      the PublicationErrorFrameHandler to call back on to.
      Since:
      1.47.0
    • filePageSize

      public int filePageSize()
      Get file page size from running media driver.
      Returns:
      file page size or zero (if not connected to the media driver).
      Since:
      1.48.0
    • close

      public void close()
      Clean up all resources that the client uses to communicate with the Media Driver.
      Overrides:
      close in class CommonContext
    • toString

      public String toString()
      Overrides:
      toString in class Object