Skip to content

Commit f842223

Browse files
committed
Add example of batch context usage
1 parent d8c1409 commit f842223

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/docs/asciidoc/usage.adoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ include::{test-examples}/Api.java[tag=connection-settings]
2626
<1> Use the `guest` user by default
2727
<2> Use the `admin` user for this connection
2828

29+
=== Settling Messages in Batch
30+
31+
.Settling messages in batch
32+
[source,java,indent=0]
33+
--------
34+
include::{test-examples}/Api.java[tag=settling-message-in-batch]
35+
--------
36+
<1> Declare batch context property
37+
<2> Create a new batch context instance
38+
<3> Add the current message context to the batch context if processing is successful
39+
<4> Settle the batch context once it contains 10 messages
40+
<5> Reset the batch context
41+
<6> Discard the current message context if processing fails
42+
43+
2944
=== Subscription Listener
3045

3146
The client provides a `SubscriptionListener` interface callback to add behavior before a subscription is created.

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,40 @@ void metricsCollectorMicrometerPrometheus() {
8686
// end::metrics-micrometer-prometheus[]
8787
}
8888

89+
void settlingMessagesInBatch() {
90+
Connection connection = null;
91+
92+
// tag::settling-message-in-batch[]
93+
Consumer.MessageHandler handler = new Consumer.MessageHandler() {
94+
volatile Consumer.BatchContext batch = null; // <1>
95+
@Override
96+
public void handle(Consumer.Context context, Message message) {
97+
if (batch == null) {
98+
batch = context.batch(); // <2>
99+
}
100+
boolean success = process(message);
101+
if (success) {
102+
batch.add(context); // <3>
103+
if (batch.size() == 10) {
104+
batch.accept(); // <4>
105+
batch = null; // <5>
106+
}
107+
} else {
108+
context.discard(); // <6>
109+
}
110+
}
111+
};
112+
Consumer consumer = connection.consumerBuilder()
113+
.queue("some-queue")
114+
.messageHandler(handler)
115+
.build();
116+
// end::settling-message-in-batch[]
117+
}
118+
119+
boolean process(Message message) {
120+
return true;
121+
}
122+
89123
void micrometerObservation() {
90124
ObservationRegistry observationRegistry = ObservationRegistry.NOOP;
91125
// tag::micrometer-observation[]

0 commit comments

Comments
 (0)