|
| 1 | +/* |
| 2 | + * Copyright 2022-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example; |
| 18 | + |
| 19 | +import org.apache.pulsar.client.api.Message; |
| 20 | +import org.apache.pulsar.client.api.Schema; |
| 21 | +import org.apache.pulsar.client.api.SubscriptionInitialPosition; |
| 22 | +import org.apache.pulsar.common.schema.SchemaType; |
| 23 | +import org.apache.pulsar.reactive.client.api.MessageResult; |
| 24 | +import org.apache.pulsar.reactive.client.api.MessageSpec; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import org.springframework.boot.ApplicationRunner; |
| 29 | +import org.springframework.boot.SpringApplication; |
| 30 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 31 | +import org.springframework.context.annotation.Bean; |
| 32 | +import org.springframework.context.annotation.Configuration; |
| 33 | +import org.springframework.pulsar.annotation.PulsarListener; |
| 34 | +import org.springframework.pulsar.reactive.config.annotation.ReactivePulsarListener; |
| 35 | +import org.springframework.pulsar.reactive.config.annotation.ReactivePulsarListenerMessageConsumerBuilderCustomizer; |
| 36 | +import org.springframework.pulsar.reactive.core.ReactivePulsarTemplate; |
| 37 | + |
| 38 | +import reactor.core.publisher.Flux; |
| 39 | +import reactor.core.publisher.Mono; |
| 40 | + |
| 41 | +@SpringBootApplication |
| 42 | +public class ReactiveSpringPulsarBootApp { |
| 43 | + |
| 44 | + private static final Logger LOG = LoggerFactory.getLogger(ReactiveSpringPulsarBootApp.class); |
| 45 | + |
| 46 | + public static void main(String[] args) { |
| 47 | + SpringApplication.run(ReactiveSpringPulsarBootApp.class, args); |
| 48 | + } |
| 49 | + |
| 50 | + @Configuration(proxyBeanMethods = false) |
| 51 | + static class ReactiveTemplateWithSimpleReactiveListener { |
| 52 | + |
| 53 | + private static final String TOPIC = "sample-reactive-topic1"; |
| 54 | + |
| 55 | + @Bean |
| 56 | + ApplicationRunner sendPrimitiveMessagesToPulsarTopic(ReactivePulsarTemplate<String> template) { |
| 57 | + return (args) -> Flux.range(0, 10) |
| 58 | + .map((i) -> MessageSpec.of("ReactiveTemplateWithSimpleReactiveListener:" + i)) |
| 59 | + .as(messages -> template.send(TOPIC, messages)) |
| 60 | + .doOnNext((msr) -> LOG.info("++++++PRODUCE {}------", msr.getMessageSpec().getValue())) |
| 61 | + .subscribe(); |
| 62 | + } |
| 63 | + |
| 64 | + @ReactivePulsarListener(topics = TOPIC, consumerCustomizer = "subscriptionInitialPositionEarliest") |
| 65 | + public Mono<Void> listenSimple(String msg) { |
| 66 | + LOG.info("++++++CONSUME {}------", msg); |
| 67 | + return Mono.empty(); |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + @Configuration(proxyBeanMethods = false) |
| 73 | + static class ReactiveTemplateWithStreamingReactiveListener { |
| 74 | + |
| 75 | + private static final String TOPIC = "sample-reactive-topic2"; |
| 76 | + |
| 77 | + @Bean |
| 78 | + ApplicationRunner sendComplexMessagesToPulsarTopic(ReactivePulsarTemplate<Foo> template) { |
| 79 | + var schema = Schema.JSON(Foo.class); |
| 80 | + return (args) -> Flux.range(0, 10) |
| 81 | + .map((i) -> MessageSpec.of(new Foo("Foo-" + i, "Bar-" + i))) |
| 82 | + .as(messages -> template.send(TOPIC, messages, schema)) |
| 83 | + .doOnNext((msr) -> LOG.info("++++++PRODUCE {}------", msr.getMessageSpec().getValue())) |
| 84 | + .subscribe(); |
| 85 | + } |
| 86 | + |
| 87 | + @ReactivePulsarListener(topics = TOPIC, stream = true, schemaType = SchemaType.JSON, |
| 88 | + consumerCustomizer = "subscriptionInitialPositionEarliest") |
| 89 | + public Flux<MessageResult<Void>> listenStreaming(Flux<Message<Foo>> messages) { |
| 90 | + return messages |
| 91 | + .doOnNext((msg) -> LOG.info("++++++CONSUME {}------", msg.getValue())) |
| 92 | + .map(MessageResult::acknowledge); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + @Configuration(proxyBeanMethods = false) |
| 98 | + static class ConsumerCustomizerConfig { |
| 99 | + |
| 100 | + @Bean |
| 101 | + ReactivePulsarListenerMessageConsumerBuilderCustomizer<String> subscriptionInitialPositionEarliest() { |
| 102 | + return b -> b.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest); |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + @Configuration(proxyBeanMethods = false) |
| 108 | + static class ReactiveTemplateWithImperativeListener { |
| 109 | + |
| 110 | + private static final String TOPIC = "sample-reactive-topic3"; |
| 111 | + |
| 112 | + @Bean |
| 113 | + ApplicationRunner sendMessagesToPulsarTopic(ReactivePulsarTemplate<String> template) { |
| 114 | + return (args) -> Flux.range(0, 10) |
| 115 | + .map((i) -> MessageSpec.of("ReactiveTemplateWithImperativeListener:" + i)) |
| 116 | + .as(messages -> template.send(TOPIC, messages)) |
| 117 | + .doOnNext((msr) -> LOG.info("++++++PRODUCE {}------", msr.getMessageSpec().getValue())) |
| 118 | + .subscribe(); |
| 119 | + } |
| 120 | + |
| 121 | + @PulsarListener(topics = TOPIC) |
| 122 | + void listenSimple(String msg) { |
| 123 | + LOG.info("++++++CONSUME {}------", msg); |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + record Foo(String foo, String bar) { |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments