Interface RequestId.Generator

  • Enclosing interface:
    RequestId

    public static interface RequestId.Generator
    Generates, or assigns, an ID for requests.
    
     import ratpack.core.handling.RequestId;
     import ratpack.test.embed.EmbeddedApp;
    
     import java.util.concurrent.atomic.AtomicLong;
    
     import static org.junit.jupiter.api.Assertions.assertEquals;
    
     public class Example {
       public static void main(String... args) throws Exception {
         AtomicLong counter = new AtomicLong();
         EmbeddedApp.of(s -> s
             .registryOf(r -> r
                 .add(RequestId.Generator.class, request -> RequestId.of(Long.toString(counter.incrementAndGet())))
             )
             .handlers(c -> c
                 .get(ctx ->
                     ctx.render("ID: " + ctx.get(RequestId.class))
                 )
             )
         ).test(http -> {
           assertEquals(http.getText(), "ID: 1");
           assertEquals(http.getText(), "ID: 2");
         });
       }
     }
     
    See Also:
    randomUuid(), header(CharSequence)
    • Field Detail

      • TYPE

        static final com.google.common.reflect.TypeToken<RequestId.Generator> TYPE
        A type token for this type.
        Since:
        1.1
    • Method Detail

      • randomUuid

        static RequestId.Generator randomUuid()
        Generates IDs based of a random UUID.

        This strategy is installed into the server registry. It is used if no other strategy is provided.

        Internally ThreadLocalRandom.current() is used to produce values. Please consult its documentation for the nature of the randomness of the UUIDs.

        Returns:
        a request id generator
      • header

        static RequestId.Generator header​(CharSequence headerName)
        Creates a generator that uses the value for the given header, falling back to a randomUuid() generator if the header is not present.

        This strategy is particularly useful in any kind of distributed environment, where a logical ID for the work is generated (or known) by the thing making the request. This applies to cloud environments like Heroku, where the edge router assigns a request ID.

        
         import ratpack.core.handling.RequestId;
         import ratpack.test.embed.EmbeddedApp;
        
         import static org.junit.jupiter.api.Assertions.assertEquals;
        
         public class Example {
           public static void main(String... args) throws Exception {
             EmbeddedApp.of(s -> s
                 .registryOf(r -> r
                     .add(RequestId.Generator.header("X-Request-ID"))
                 )
                 .handlers(c -> c
                     .get(ctx ->
                         ctx.render("ID: " + ctx.get(RequestId.class))
                     )
                 )
             ).test(http -> {
               http.requestSpec(r -> r.getHeaders().add("X-Request-ID", "foo"));
               assertEquals(http.getText(), "ID: foo");
             });
           }
         }
         
        Parameters:
        headerName - the name of the header containing the request ID
        Returns:
        a request ID generator
        See Also:
        header(CharSequence, RequestId.Generator)
      • header

        static RequestId.Generator header​(CharSequence headerName,
                                          RequestId.Generator fallback)
        Creates a generator that uses the value for the given header, using the given fallback generator if the header is not present.
        Parameters:
        headerName - the name of the header containing the request ID
        fallback - the generator to use if the header is not present
        Returns:
        a request ID generator
        See Also:
        header(CharSequence)
      • generate

        RequestId generate​(Request request)
        Generate the ID for the request.
        Parameters:
        request - the request
        Returns:
        a request ID