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