|
1 | | - |
2 | 1 | include::../attributes/attributes-variables.adoc[] |
3 | 2 |
|
4 | | -=== Pulsar Headers |
| 3 | +[[pulsar-headers]] |
| 4 | +== Pulsar Headers |
5 | 5 | 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`). |
6 | 6 | As such, the terms "`Pulsar message header`" and "`Pulsar message metadata`" are used interchangeably. |
7 | 7 | 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]. |
8 | 8 |
|
9 | 9 | === Spring Headers |
10 | 10 | Spring Messaging provides first-class "`header`" support via its `MessageHeaders` abstraction. |
11 | 11 |
|
| 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 | + |
12 | 57 | === Message Header Mapping |
13 | 58 | The `PulsarHeaderMapper` strategy is provided to map headers to and from Pulsar user properties and Spring `MessageHeaders`. |
14 | 59 |
|
@@ -66,12 +111,12 @@ IMPORTANT: The object mapper in the example above should be an instance of `com. |
66 | 111 | IMPORTANT: The xref:reference/custom-object-mapper.adoc#jackson2vs3[same limitations] regarding Jackson 2 vs. Jackson 3 apply here } |
67 | 112 |
|
68 | 113 | === 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`. |
70 | 115 | 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_`). |
71 | 116 | You can specify which headers are mapped for inbound and outbound messages by configuring the `inboundPatterns` and `outboundPatterns` on a mapper bean you provide. |
72 | 117 | 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. |
73 | 118 | Patterns are rather simple and can contain a leading wildcard (`\*`), a trailing wildcard, or both (for example, `*.cat.*`). |
74 | 119 | 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. |
76 | 121 |
|
77 | 122 | IMPORTANT: When you provide your own patterns, we recommend including `!id` and `!timestamp`, since these headers are read-only on the inbound side. |
0 commit comments