Skip to content

Commit d4e816e

Browse files
committed
Binder sample app update with DLT
1 parent b1024a9 commit d4e816e

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

spring-pulsar-sample-apps/sample-pulsar-binder/src/main/java/org/springframework/pulsar/sample/binder/SpringPulsarBinderSampleApp.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,26 @@
2020
import java.util.function.Function;
2121
import java.util.function.Supplier;
2222

23+
import org.apache.pulsar.client.api.Schema;
24+
import org.apache.pulsar.common.schema.SchemaType;
2325
import org.slf4j.Logger;
2426
import org.slf4j.LoggerFactory;
2527

28+
import org.springframework.boot.ApplicationRunner;
2629
import org.springframework.boot.SpringApplication;
2730
import org.springframework.boot.autoconfigure.SpringBootApplication;
2831
import org.springframework.context.annotation.Bean;
32+
import org.springframework.pulsar.annotation.PulsarListener;
33+
import org.springframework.pulsar.core.PulsarTemplate;
2934

35+
/**
36+
* This sample binder app has an extra consumer that is equipped with Pulsar's DLT feature - timeLoggerToDlt.
37+
* However, this consumer is not part of the spring.cloud.function.definition.
38+
* In order to enable this, add the function timeLoggerToDlt to the definition in the application.yml file.
39+
* When doing this, in order to minimize verbose output and just to focus on the DLT feature, comment out the
40+
* regular supplier below (timeSupplier) and then un-comment the ApplicationRunner below.
41+
* The runner only sends a single message whereas the supplier sends a message every second.
42+
*/
3043
@SpringBootApplication
3144
public class SpringPulsarBinderSampleApp {
3245

@@ -41,6 +54,18 @@ public Supplier<Time> timeSupplier() {
4154
return () -> new Time(String.valueOf(System.currentTimeMillis()));
4255
}
4356

57+
// @Bean
58+
// ApplicationRunner runner(PulsarTemplate<Time> pulsarTemplate) {
59+
//
60+
// String topic = "timeSupplier-out-0";
61+
//
62+
// return args -> {
63+
// for (int i = 0; i < 1; i++) {
64+
// pulsarTemplate.send(topic, new Time(String.valueOf(System.currentTimeMillis())), Schema.JSON(Time.class));
65+
// }
66+
// };
67+
// }
68+
4469
@Bean
4570
public Function<Time, EnhancedTime> timeProcessor() {
4671
return (time) -> {
@@ -55,6 +80,19 @@ public Consumer<EnhancedTime> timeLogger() {
5580
return (time) -> this.logger.info("SINK: {}", time);
5681
}
5782

83+
@Bean
84+
public Consumer<EnhancedTime> timeLoggerToDlt() {
85+
return (time) -> {
86+
this.logger.info("SINK (TO DLT EVENTUALLY): {}", time);
87+
throw new RuntimeException("fail " + time);
88+
};
89+
}
90+
91+
@PulsarListener(id = "dlqListener", topics = "notification-dlq", schemaType = SchemaType.JSON)
92+
void listenDlq(EnhancedTime msg) {
93+
System.out.println("From DLQ: " + msg);
94+
}
95+
5896
record Time(String time) {
5997
}
6098

spring-pulsar-sample-apps/sample-pulsar-binder/src/main/resources/application.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
spring:
22
cloud:
33
function:
4-
definition: timeSupplier;timeProcessor;timeLogger;
4+
definition: timeSupplier;timeProcessor;timeLogger
55
stream:
66
bindings:
77
timeSupplier-out-0:
@@ -19,6 +19,10 @@ spring:
1919
destination: timeProcessor-out-0
2020
consumer:
2121
use-native-decoding: true
22+
timeLoggerToDlt-in-0:
23+
destination: timeProcessor-out-0
24+
consumer:
25+
use-native-decoding: true
2226
pulsar:
2327
bindings:
2428
timeSupplier-out-0:
@@ -37,3 +41,12 @@ spring:
3741
consumer:
3842
schema-type: JSON
3943
message-type: org.springframework.pulsar.sample.binder.SpringPulsarBinderSampleApp.EnhancedTime
44+
timeLoggerToDlt-in-0:
45+
consumer:
46+
subscription-type: Shared
47+
negative-ack-redelivery-delay: 1s
48+
dead-letter-policy:
49+
dead-letter-topic: notification-dlq
50+
max-redeliver-count: 5
51+
schema-type: JSON
52+
message-type: org.springframework.pulsar.sample.binder.SpringPulsarBinderSampleApp.EnhancedTime

0 commit comments

Comments
 (0)