Skip to content

Commit 8d90de2

Browse files
committed
Include pulsar-header.adoc in the message-consumption.adoc
Signed-off-by: onobc <[email protected]>
1 parent 6d33c6c commit 8d90de2

File tree

2 files changed

+50
-50
lines changed

2 files changed

+50
-50
lines changed

spring-pulsar-docs/src/main/antora/modules/ROOT/pages/reference/pulsar-header.adoc

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
1-
21
include::../attributes/attributes-variables.adoc[]
32

4-
=== Pulsar Headers
3+
[[pulsar-headers]]
4+
== Pulsar Headers
55
Pulsar does not have a first-class "`header`" concept but instead provides a map for custom user properties as well as methods to access the message metadata typically stored in a message header (eg. `id` and `event-time`).
66
As such, the terms "`Pulsar message header`" and "`Pulsar message metadata`" are used interchangeably.
77
The list of available message metadata (headers) can be found in {github}/blob/main/spring-pulsar/src/main/java/org/springframework/pulsar/support/PulsarHeaders.java[PulsarHeaders.java].
88

99
=== Spring Headers
1010
Spring Messaging provides first-class "`header`" support via its `MessageHeaders` abstraction.
1111

12+
The Pulsar message metadata can be consumed as Spring message headers.
13+
The list of available headers can be found in {github}/blob/main/spring-pulsar/src/main/java/org/springframework/pulsar/support/PulsarHeaders.java[PulsarHeaders.java].
14+
15+
=== Accessing in Single Record based Consumer
16+
17+
The following example shows how you can access the various Pulsar Headers in an application that uses the single record mode of consuming:
18+
19+
[source,java]
20+
----
21+
@PulsarListener(topics = "simpleListenerWithHeaders")
22+
void simpleListenerWithHeaders(String data, @Header(PulsarHeaders.MESSAGE_ID) MessageId messageId,
23+
@Header(PulsarHeaders.RAW_DATA) byte[] rawData,
24+
@Header("foo") String foo) {
25+
26+
}
27+
----
28+
29+
In the preceding example, we access the values for the `messageId` and `rawData` message metadata as well as a custom message property named `foo`.
30+
The Spring `@Header` annotation is used for each header field.
31+
32+
You can also use Pulsar's `Message` as the envelope to carry the payload.
33+
When doing so, the user can directly call the corresponding methods on the Pulsar message for retrieving the metadata.
34+
However, as a convenience, you can also retrieve it by using the `Header` annotation.
35+
Note that you can also use the Spring messaging `Message` envelope to carry the payload and then retrieve the Pulsar headers by using `@Header`.
36+
37+
=== Accessing in Batch Record based Consumer
38+
39+
In this section, we see how to access the various Pulsar Headers in an application that uses a batch consumer:
40+
41+
[source,java]
42+
----
43+
@PulsarListener(topics = "simpleBatchListenerWithHeaders", batch = true)
44+
void simpleBatchListenerWithHeaders(List<String> data,
45+
@Header(PulsarHeaders.MESSAGE_ID) List<MessageId> messageIds,
46+
@Header(PulsarHeaders.TOPIC_NAME) List<String> topicNames, @Header("foo") List<String> fooValues) {
47+
48+
}
49+
----
50+
51+
In the preceding example, we consume the data as a `List<String>`.
52+
When extracting the various headers, we do so as a `List<>` as well.
53+
Spring for Apache Pulsar ensures that the headers list corresponds to the data list.
54+
55+
You can also extract headers in the same manner when you use the batch listener and receive payloads as `List<org.apache.pulsar.client.api.Message<?>`, `org.apache.pulsar.client.api.Messages<?>`, or `org.springframework.messaging.Messsge<?>`.
56+
1257
=== Message Header Mapping
1358
The `PulsarHeaderMapper` strategy is provided to map headers to and from Pulsar user properties and Spring `MessageHeaders`.
1459

@@ -66,12 +111,12 @@ IMPORTANT: The object mapper in the example above should be an instance of `com.
66111
IMPORTANT: The xref:reference/custom-object-mapper.adoc#jackson2vs3[same limitations] regarding Jackson 2 vs. Jackson 3 apply here }
67112

68113
=== Inbound/Outbound Patterns
69-
On the inbound side, by default, all Pulsar headers (message metadata plus user properties) are mapped to `MessageHeaders`.
114+
On the inbound side, by default, all Pulsar headers (message metadata plus user properties) are mapped to `MessageHeaders`.
70115
On the outbound side, by default, all `MessageHeaders` are mapped, except `id`, `timestamp`, and the headers that represent the Pulsar message metadata (i.e. the headers that are prefixed with `pulsar_message_`).
71116
You can specify which headers are mapped for inbound and outbound messages by configuring the `inboundPatterns` and `outboundPatterns` on a mapper bean you provide.
72117
You can include Pulsar message metadata headers on the outbound messages by adding the exact header name to the `outboundPatterns` as patterns are not supported for metadata headers.
73118
Patterns are rather simple and can contain a leading wildcard (`\*`), a trailing wildcard, or both (for example, `*.cat.*`).
74119
You can negate patterns with a leading `!`.
75-
The first pattern that matches a header name (whether positive or negative) wins.
120+
The first pattern that matches a header name (whether positive or negative) wins.
76121

77122
IMPORTANT: When you provide your own patterns, we recommend including `!id` and `!timestamp`, since these headers are read-only on the inbound side.

spring-pulsar-docs/src/main/antora/modules/ROOT/pages/reference/pulsar/message-consumption.adoc

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -379,52 +379,7 @@ public void listen4(List<Foo> messages) {
379379
When you use this type of `PulsarListener`, the framework detects that you are in batch mode.
380380
Since it already received the data in batches by using the Consumer's `batchReceive` method, it hands off the entire batch to the listener method through an adapter for `PulsarBatchMessageListener`.
381381

382-
[[pulsar-headers]]
383-
== Pulsar Headers
384-
The Pulsar message metadata can be consumed as Spring message headers.
385-
The list of available headers can be found in {github}/blob/main/spring-pulsar/src/main/java/org/springframework/pulsar/support/PulsarHeaders.java[PulsarHeaders.java].
386-
387-
=== Accessing in Single Record based Consumer
388-
389-
The following example shows how you can access the various Pulsar Headers in an application that uses the single record mode of consuming:
390-
391-
[source,java]
392-
----
393-
@PulsarListener(topics = "simpleListenerWithHeaders")
394-
void simpleListenerWithHeaders(String data, @Header(PulsarHeaders.MESSAGE_ID) MessageId messageId,
395-
@Header(PulsarHeaders.RAW_DATA) byte[] rawData,
396-
@Header("foo") String foo) {
397-
398-
}
399-
----
400-
401-
In the preceding example, we access the values for the `messageId` and `rawData` message metadata as well as a custom message property named `foo`.
402-
The Spring `@Header` annotation is used for each header field.
403-
404-
You can also use Pulsar's `Message` as the envelope to carry the payload.
405-
When doing so, the user can directly call the corresponding methods on the Pulsar message for retrieving the metadata.
406-
However, as a convenience, you can also retrieve it by using the `Header` annotation.
407-
Note that you can also use the Spring messaging `Message` envelope to carry the payload and then retrieve the Pulsar headers by using `@Header`.
408-
409-
=== Accessing in Batch Record based Consumer
410-
411-
In this section, we see how to access the various Pulsar Headers in an application that uses a batch consumer:
412-
413-
[source,java]
414-
----
415-
@PulsarListener(topics = "simpleBatchListenerWithHeaders", batch = true)
416-
void simpleBatchListenerWithHeaders(List<String> data,
417-
@Header(PulsarHeaders.MESSAGE_ID) List<MessageId> messageIds,
418-
@Header(PulsarHeaders.TOPIC_NAME) List<String> topicNames, @Header("foo") List<String> fooValues) {
419-
420-
}
421-
----
422-
423-
In the preceding example, we consume the data as a `List<String>`.
424-
When extracting the various headers, we do so as a `List<>` as well.
425-
Spring for Apache Pulsar ensures that the headers list corresponds to the data list.
426-
427-
You can also extract headers in the same manner when you use the batch listener and receive payloads as `List<org.apache.pulsar.client.api.Message<?>`, `org.apache.pulsar.client.api.Messages<?>`, or `org.springframework.messaging.Messsge<?>`.
382+
include::../pulsar-header.adoc[leveloffset=+]
428383

429384
== Message Acknowledgment
430385

0 commit comments

Comments
 (0)