public static interface RequestId.Generator
import ratpack.handling.RequestId;
import ratpack.test.embed.EmbeddedApp;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.Assert.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");
});
}
}
randomUuid()
,
header(CharSequence)
Modifier and Type | Field and Description |
---|---|
static com.google.common.reflect.TypeToken<RequestId.Generator> |
TYPE
A type token for this type.
|
Modifier and Type | Method and Description |
---|---|
RequestId |
generate(Request request)
Generate the ID for the request.
|
static RequestId.Generator |
header(java.lang.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. |
static RequestId.Generator |
header(java.lang.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.
|
static RequestId.Generator |
randomUuid()
Generates IDs based of a random UUID.
|
static final com.google.common.reflect.TypeToken<RequestId.Generator> TYPE
static RequestId.Generator randomUuid()
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.
static RequestId.Generator header(java.lang.CharSequence headerName)
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.handling.RequestId;
import ratpack.test.embed.EmbeddedApp;
import static org.junit.Assert.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");
});
}
}
headerName
- the name of the header containing the request IDheader(CharSequence, RequestId.Generator)
static RequestId.Generator header(java.lang.CharSequence headerName, RequestId.Generator fallback)
headerName
- the name of the header containing the request IDfallback
- the generator to use if the header is not presentheader(CharSequence)