Class AffinityContext

  • All Implemented Interfaces:
    org.forgerock.services.context.Context

    public final class AffinityContext
    extends org.forgerock.services.context.AbstractContext
    An affinity context can be used to bypass any load-balancing and always direct a requests to the same server. For example, this can be useful when several data dependent requests need to be executed on the same server. If they were executed on different servers, replication delays may make one of them fail due to missing data.

    Routing of requests is performed by hashing the provided affinity value. Given a fixed set of servers, it is guaranteed that requests presenting the same affinity value will always be directed to the same server. However, if the set of servers change, then it is very likely that a new request will go to a different server than previously, and subsequent requests will always be routed to that same new server.

    Sample usage:

    
     // This example assumes the set of servers behind a load balancing proxy do not change
     final Connection conn = newConnection();
    
     final ReadRequest request1 = ...
     final ResourceResponse resourceFromServerA1 =
         conn.read(new AffinityContext(parentCtx, "routeToServerA"), request1);
     final ReadRequest request2 = ...
     final ResourceResponse resourceFromServerA2 =
         conn.read(new AffinityContext(parentCtx, "routeToServerA"), request2);
    
     final ReadRequest request3 = ...
     final ResourceResponse resourceFromServerB1 =
         conn.read(new AffinityContext(parentCtx, "routeToServerB"), request3);
     final ReadRequest request4 = ...
     final ResourceResponse resourceFromServerB2 =
         conn.read(new AffinityContext(parentCtx, "routeToServerB"), request4);
    
     final ReadRequest request5 = ...
     final ResourceResponse resourceFromServerA3 =
         conn.read(new AffinityContext(parentCtx, "routeToServerA"), request5);
     
    • Constructor Detail

      • AffinityContext

        public AffinityContext​(org.forgerock.services.context.Context parent,
                               String affinityValue)
        Constructs a new AffinityContext.
        Parameters:
        parent - The parent context
        affinityValue - The affinity value to use in this context
    • Method Detail

      • getAffinityValue

        public String getAffinityValue()
        Returns the affinity value.
        Returns:
        the affinity value
      • toJsonValue

        public JsonValue toJsonValue()
        Specified by:
        toJsonValue in interface org.forgerock.services.context.Context
        Overrides:
        toJsonValue in class org.forgerock.services.context.AbstractContext