Skip to content

Commit bdfc899

Browse files
committed
Document Context#isRequesterAlive(Message)
1 parent f1a1167 commit bdfc899

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/docs/asciidoc/usage.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,18 @@ The prefix is different for each `Requester` instance and the suffix is incremen
242242
* it sets the _request_ https://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-properties[`message-id` property] to the generated correlation ID.
243243
* it extracts the correlation ID from the _reply_ https://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-properties[`correlation-id` property] to correlate a reply with the appropriate request.
244244

245+
If the responder will perform expensive work, it can proactively check whether the requester is still present with the `Context#isRequesterAlive(Message)` method:
246+
247+
.Checking the requester
248+
[source,java,indent=0]
249+
--------
250+
include::{test-examples}/RequestResponseApi.java[tag=is-requester-alive]
251+
--------
252+
<1> Check if the requester is still connected
253+
254+
The requester can check the requester at any time - when a request comes in or even during a long processing - and react accordingly by stopping the processing.
255+
Note each call to `Context#isRequesterAlive(Message)` implies a request to the broker.
256+
245257
Let's see how to customize some of the request/response support mechanics.
246258
Imagine the request `message-id` property is a critical piece of information and we do not want to use it as the correlation ID.
247259
The request can use the `correlation-id` property and the responder just has to extract the correlation ID from this property (instead of the `message-id` property by default).

src/test/java/com/rabbitmq/client/amqp/docs/RequestResponseApi.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ void withDefaults() throws Exception {
1919
Responder responder = connection.responderBuilder() // <1>
2020
.requestQueue("request-queue") // <2>
2121
.handler((ctx, req) -> { // <3>
22-
String in = new String(req.body(), UTF_8);
23-
String out = "*** " + in + " ***";
24-
return ctx.message(out.getBytes(UTF_8)); // <4>
22+
String in = new String(req.body(), UTF_8);
23+
String out = "*** " + in + " ***";
24+
return ctx.message(out.getBytes(UTF_8)); // <4>
2525
}).build();
2626
// end::responder-creation[]
2727

@@ -39,7 +39,24 @@ void withDefaults() throws Exception {
3939
// end::requester-request[]
4040
}
4141

42-
void withCustomSettings() {
42+
void isRequesterAlive() {
43+
Connection connection = null;
44+
// tag::is-requester-alive[]
45+
Responder responder = connection.responderBuilder()
46+
.requestQueue("request-queue")
47+
.handler((ctx, req) -> {
48+
if (ctx.isRequesterAlive(req)) { // <1>
49+
String in = new String(req.body(), UTF_8);
50+
String out = "*** " + in + " ***";
51+
return ctx.message(out.getBytes(UTF_8));
52+
} else {
53+
return null;
54+
}
55+
}).build();
56+
// end::is-requester-alive[]
57+
}
58+
59+
void withCustomSettings() {
4360
Connection connection = null;
4461
// tag::custom-requester-creation[]
4562
String replyToQueue = connection.management().queue()
@@ -61,9 +78,9 @@ void withCustomSettings() {
6178
.correlationIdExtractor(Message::correlationId) // <1>
6279
.requestQueue("request-queue")
6380
.handler((ctx, req) -> {
64-
String in = new String(req.body(), UTF_8);
65-
String out = "*** " + in + " ***";
66-
return ctx.message(out.getBytes(UTF_8));
81+
String in = new String(req.body(), UTF_8);
82+
String out = "*** " + in + " ***";
83+
return ctx.message(out.getBytes(UTF_8));
6784
}).build();
6885
// end::custom-responder-creation[]
6986
}

0 commit comments

Comments
 (0)