22
22
import java .util .concurrent .CountDownLatch ;
23
23
import java .util .concurrent .TimeUnit ;
24
24
25
+ import org .apache .pulsar .client .api .Consumer ;
25
26
import org .apache .pulsar .client .api .Schema ;
27
+ import org .apache .pulsar .client .api .SubscriptionType ;
28
+ import org .apache .pulsar .client .impl .conf .ConsumerConfigurationData ;
26
29
import org .apache .pulsar .common .schema .SchemaType ;
30
+ import org .assertj .core .api .InstanceOfAssertFactories ;
27
31
import org .junit .jupiter .api .Test ;
28
32
29
33
import org .springframework .boot .SpringApplication ;
@@ -58,16 +62,17 @@ class PulsarListenerIntegrationTests implements PulsarTestContainerSupport {
58
62
59
63
private static final CountDownLatch LATCH_5 = new CountDownLatch (10 );
60
64
65
+ private static final CountDownLatch LATCH_CONFIG_PROPS = new CountDownLatch (1 );
66
+
61
67
@ Test
62
68
void basicPulsarListener () throws Exception {
63
69
SpringApplication app = new SpringApplication (BasicListenerConfig .class );
64
70
app .setWebApplicationType (WebApplicationType .NONE );
65
-
66
71
try (ConfigurableApplicationContext context = app
67
72
.run ("--spring.pulsar.client.serviceUrl=" + PulsarTestContainerSupport .getPulsarBrokerUrl ())) {
68
73
@ SuppressWarnings ("unchecked" )
69
74
PulsarTemplate <String > pulsarTemplate = context .getBean (PulsarTemplate .class );
70
- pulsarTemplate .send ("plt -basic-topic" , "John Doe" );
75
+ pulsarTemplate .send ("plit -basic-topic" , "John Doe" );
71
76
assertThat (LATCH_1 .await (20 , TimeUnit .SECONDS )).isTrue ();
72
77
}
73
78
}
@@ -76,12 +81,11 @@ void basicPulsarListener() throws Exception {
76
81
void basicPulsarListenerCustomType () throws Exception {
77
82
SpringApplication app = new SpringApplication (BasicListenerCustomTypeConfig .class );
78
83
app .setWebApplicationType (WebApplicationType .NONE );
79
-
80
84
try (ConfigurableApplicationContext context = app
81
85
.run ("--spring.pulsar.client.serviceUrl=" + PulsarTestContainerSupport .getPulsarBrokerUrl ())) {
82
86
@ SuppressWarnings ("unchecked" )
83
87
PulsarTemplate <Foo > pulsarTemplate = context .getBean (PulsarTemplate .class );
84
- pulsarTemplate .send ("plt -foo-topic1" , new Foo ("John Doe" ), Schema .JSON (Foo .class ));
88
+ pulsarTemplate .send ("plit -foo-topic1" , new Foo ("John Doe" ), Schema .JSON (Foo .class ));
85
89
assertThat (LATCH_2 .await (20 , TimeUnit .SECONDS )).isTrue ();
86
90
}
87
91
}
@@ -90,12 +94,11 @@ void basicPulsarListenerCustomType() throws Exception {
90
94
void basicPulsarListenerCustomTypeWithTypeMapping () throws Exception {
91
95
SpringApplication app = new SpringApplication (BasicListenerCustomTypeWithTypeMappingConfig .class );
92
96
app .setWebApplicationType (WebApplicationType .NONE );
93
-
94
97
try (ConfigurableApplicationContext context = app
95
98
.run ("--spring.pulsar.client.serviceUrl=" + PulsarTestContainerSupport .getPulsarBrokerUrl ())) {
96
99
@ SuppressWarnings ("unchecked" )
97
100
PulsarTemplate <Foo > pulsarTemplate = context .getBean (PulsarTemplate .class );
98
- pulsarTemplate .send ("plt -foo-topic2" , new Foo ("John Doe" ));
101
+ pulsarTemplate .send ("plit -foo-topic2" , new Foo ("John Doe" ));
99
102
assertThat (LATCH_3 .await (20 , TimeUnit .SECONDS )).isTrue ();
100
103
}
101
104
}
@@ -104,12 +107,11 @@ void basicPulsarListenerCustomTypeWithTypeMapping() throws Exception {
104
107
void basicPulsarListenerWithTopicMapping () throws Exception {
105
108
SpringApplication app = new SpringApplication (BasicListenerWithTopicMappingConfig .class );
106
109
app .setWebApplicationType (WebApplicationType .NONE );
107
-
108
110
try (ConfigurableApplicationContext context = app
109
111
.run ("--spring.pulsar.client.serviceUrl=" + PulsarTestContainerSupport .getPulsarBrokerUrl ())) {
110
112
@ SuppressWarnings ("unchecked" )
111
113
PulsarTemplate <Foo > pulsarTemplate = context .getBean (PulsarTemplate .class );
112
- pulsarTemplate .send ("plt -topicMapping-topic" , new Foo ("Crazy8z" ), Schema .JSON (Foo .class ));
114
+ pulsarTemplate .send ("plit -topicMapping-topic" , new Foo ("Crazy8z" ), Schema .JSON (Foo .class ));
113
115
assertThat (LATCH_4 .await (20 , TimeUnit .SECONDS )).isTrue ();
114
116
}
115
117
}
@@ -118,23 +120,38 @@ void basicPulsarListenerWithTopicMapping() throws Exception {
118
120
void batchPulsarListener () throws Exception {
119
121
SpringApplication app = new SpringApplication (BatchListenerConfig .class );
120
122
app .setWebApplicationType (WebApplicationType .NONE );
121
-
122
123
try (ConfigurableApplicationContext context = app
123
124
.run ("--spring.pulsar.client.serviceUrl=" + PulsarTestContainerSupport .getPulsarBrokerUrl ())) {
124
125
@ SuppressWarnings ("unchecked" )
125
126
PulsarTemplate <String > pulsarTemplate = context .getBean (PulsarTemplate .class );
126
127
for (int i = 0 ; i < 10 ; i ++) {
127
- pulsarTemplate .send ("plt -batch-topic" , "John Doe" );
128
+ pulsarTemplate .send ("plit -batch-topic" , "John Doe" );
128
129
}
129
130
assertThat (LATCH_5 .await (10 , TimeUnit .SECONDS )).isTrue ();
130
131
}
131
132
}
132
133
134
+ @ Test
135
+ void configPropsDrivenListener () throws Exception {
136
+ SpringApplication app = new SpringApplication (ConfigPropsDrivenListenerConfig .class );
137
+ app .setWebApplicationType (WebApplicationType .NONE );
138
+ try (ConfigurableApplicationContext context = app .run (
139
+ "--spring.pulsar.client.serviceUrl=" + PulsarTestContainerSupport .getPulsarBrokerUrl (), "--my.env=dev" ,
140
+ "--spring.pulsar.consumer.topics=plit-config-props-topic-${my.env}" ,
141
+ "--spring.pulsar.consumer.subscription.type=Shared" ,
142
+ "--spring.pulsar.consumer.subscription.name=plit-config-props-subs-${my.env}" )) {
143
+ @ SuppressWarnings ("unchecked" )
144
+ PulsarTemplate <String > pulsarTemplate = context .getBean (PulsarTemplate .class );
145
+ pulsarTemplate .send ("plit-config-props-topic-dev" , "hello config props driven" );
146
+ assertThat (LATCH_CONFIG_PROPS .await (20 , TimeUnit .SECONDS )).isTrue ();
147
+ }
148
+ }
149
+
133
150
@ EnableAutoConfiguration
134
151
@ SpringBootConfiguration
135
152
static class BasicListenerConfig {
136
153
137
- @ PulsarListener (subscriptionName = "plt -basic-sub" , topics = "plt -basic-topic" )
154
+ @ PulsarListener (subscriptionName = "plit -basic-sub" , topics = "plit -basic-topic" )
138
155
public void listen (String ignored ) {
139
156
LATCH_1 .countDown ();
140
157
}
@@ -145,7 +162,7 @@ public void listen(String ignored) {
145
162
@ SpringBootConfiguration
146
163
static class BasicListenerCustomTypeConfig {
147
164
148
- @ PulsarListener (subscriptionName = "plt -foo-sub1" , topics = "plt -foo-topic1" , schemaType = SchemaType .JSON )
165
+ @ PulsarListener (subscriptionName = "plit -foo-sub1" , topics = "plit -foo-topic1" , schemaType = SchemaType .JSON )
149
166
public void listen (Foo ignored ) {
150
167
LATCH_2 .countDown ();
151
168
}
@@ -163,7 +180,7 @@ SchemaResolver customSchemaResolver() {
163
180
return resolver ;
164
181
}
165
182
166
- @ PulsarListener (subscriptionName = "plt -foo-sub2" , topics = "plt -foo-topic2" )
183
+ @ PulsarListener (subscriptionName = "plit -foo-sub2" , topics = "plit -foo-topic2" )
167
184
public void listen (Foo ignored ) {
168
185
LATCH_3 .countDown ();
169
186
}
@@ -177,11 +194,11 @@ static class BasicListenerWithTopicMappingConfig {
177
194
@ Bean
178
195
TopicResolver customTopicResolver () {
179
196
DefaultTopicResolver resolver = new DefaultTopicResolver ();
180
- resolver .addCustomTopicMapping (Foo .class , "plt -topicMapping-topic" );
197
+ resolver .addCustomTopicMapping (Foo .class , "plit -topicMapping-topic" );
181
198
return resolver ;
182
199
}
183
200
184
- @ PulsarListener (subscriptionName = "plt -topicMapping-sub" , schemaType = SchemaType .JSON )
201
+ @ PulsarListener (subscriptionName = "plit -topicMapping-sub" , schemaType = SchemaType .JSON )
185
202
public void listen (Foo ignored ) {
186
203
LATCH_4 .countDown ();
187
204
}
@@ -192,13 +209,29 @@ public void listen(Foo ignored) {
192
209
@ SpringBootConfiguration
193
210
static class BatchListenerConfig {
194
211
195
- @ PulsarListener (subscriptionName = "plt -batch-sub" , topics = "plt -batch-topic" , batch = true )
212
+ @ PulsarListener (subscriptionName = "plit -batch-sub" , topics = "plit -batch-topic" , batch = true )
196
213
public void listen (List <String > foo ) {
197
214
foo .forEach (t -> LATCH_5 .countDown ());
198
215
}
199
216
200
217
}
201
218
219
+ @ EnableAutoConfiguration
220
+ @ SpringBootConfiguration
221
+ static class ConfigPropsDrivenListenerConfig {
222
+
223
+ @ PulsarListener
224
+ public void listen (String ignored , Consumer <String > consumer ) {
225
+ assertThat (consumer ).extracting ("conf" , InstanceOfAssertFactories .type (ConsumerConfigurationData .class ))
226
+ .satisfies ((conf ) -> {
227
+ assertThat (conf .getSubscriptionType ()).isEqualTo (SubscriptionType .Shared );
228
+ assertThat (conf .getSubscriptionName ()).isEqualTo ("plit-config-props-subs-dev" );
229
+ });
230
+ LATCH_CONFIG_PROPS .countDown ();
231
+ }
232
+
233
+ }
234
+
202
235
record Foo (String value ) {
203
236
}
204
237
0 commit comments