Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-2806: Pulsar binder defaults properties config #2811

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2023-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.stream.binder.pulsar.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.cloud.stream.config.BindingHandlerAdvise;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
* {@link EnableAutoConfiguration Auto-configuration} for extended binding metadata for the Pulsar binder.
*
* @author Soby Chacko
* @since 4.1.0
*/
@Configuration(proxyBeanMethods = false)
public class ExtendedBindingHandlerMappingsProviderConfiguration {

@Bean
public BindingHandlerAdvise.MappingsProvider pulsarExtendedPropertiesDefaultMappingsProvider() {
return () -> {
Map<ConfigurationPropertyName, ConfigurationPropertyName> mappings = new HashMap<>();
mappings.put(
ConfigurationPropertyName.of("spring.cloud.stream.pulsar.bindings"),
ConfigurationPropertyName.of("spring.cloud.stream.pulsar.default"));
return mappings;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.cloud.stream.binder.pulsar.config.ExtendedBindingHandlerMappingsProviderConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2023-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.stream.binder.pulsar;

import java.util.function.Function;

import org.apache.pulsar.common.schema.SchemaType;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.stream.binder.pulsar.config.ExtendedBindingHandlerMappingsProviderConfiguration;
import org.springframework.cloud.stream.binder.pulsar.properties.PulsarExtendedBindingProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;


import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link ExtendedBindingHandlerMappingsProviderConfiguration}.
*
* @author Soby Chacko
*/
class PulsarExtendedBindingDefaultPropertiesTests implements PulsarTestContainerSupport {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(DefaultPropertiesTestApp.class)
.withPropertyValues(
"--spring.pulsar.client.service-url=" + PulsarTestContainerSupport.getPulsarBrokerUrl(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ❤️ the tests.

One question, is the discrepancy in the property pair prefix intended? (some have --)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it was not intended. Didn't cause an issue. Do you want me to take care of them? or will you adjust on merge?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can take care of them that would be good as I was hoping to just merge from Github for this one.

"--spring.pulsar.admin.service-url=" + PulsarTestContainerSupport.getHttpServiceUrl(),
"--spring.cloud.stream.pulsar.binder.partitionCount: 1",
"spring.cloud.stream.pulsar.default.consumer.schema-type: JSON",
"spring.cloud.stream.pulsar.default.consumer.receiverQueueSize: 5000",
"spring.cloud.stream.pulsar.default.consumer.startPaused: true",
"spring.cloud.stream.pulsar.default.consumer.subscription.name: my-subscription",
"spring.cloud.stream.pulsar.default.producer.schema-type: JSON",
"spring.cloud.stream.pulsar.default.producer.blockIfQueueFull: true",
"spring.cloud.stream.pulsar.default.producer.maxPendingMessages: 200",
"spring.cloud.stream.pulsar.default.producer.name: my-producer");

@Test
void defaultsUsedWhenNoCustomBindingProperties() {
this.contextRunner.run((context) -> {
assertThat(context)
.hasNotFailed()
.hasBean("pulsar_binderProducingContext");
ConfigurableApplicationContext pulsarBinderProducingContext =
context.getBean("pulsar_binderProducingContext", ConfigurableApplicationContext.class);
PulsarExtendedBindingProperties extendedBindingProperties = pulsarBinderProducingContext.getBean(PulsarExtendedBindingProperties.class);
assertThat(extendedBindingProperties.getExtendedConsumerProperties("process-in-0"))
.hasFieldOrPropertyWithValue("schemaType", SchemaType.JSON)
.hasFieldOrPropertyWithValue("receiverQueueSize", 5000)
.hasFieldOrPropertyWithValue("subscription.name", "my-subscription")
.hasFieldOrPropertyWithValue("startPaused", true);
assertThat(extendedBindingProperties.getExtendedProducerProperties("process-out-0"))
.hasFieldOrPropertyWithValue("schemaType", SchemaType.JSON)
.hasFieldOrPropertyWithValue("blockIfQueueFull", true)
.hasFieldOrPropertyWithValue("maxPendingMessages", 200)
.hasFieldOrPropertyWithValue("name", "my-producer");
});
}

@Test
void defaultsRespectedWhenCustomBindingProperties() {
this.contextRunner
.withPropertyValues(
"spring.cloud.stream.pulsar.bindings.process-in-0.consumer.receiverQueueSize: 8000",
"spring.cloud.stream.pulsar.bindings.process-out-0.producer.blockIfQueueFull: false",
"spring.cloud.stream.pulsar.bindings.process-out-0.producer.maxPendingMessages: 400")
.run((context) -> {
assertThat(context)
.hasNotFailed()
.hasBean("pulsar_binderProducingContext");
ConfigurableApplicationContext pulsarBinderProducingContext =
context.getBean("pulsar_binderProducingContext", ConfigurableApplicationContext.class);
PulsarExtendedBindingProperties extendedBindingProperties = pulsarBinderProducingContext.getBean(PulsarExtendedBindingProperties.class);
assertThat(extendedBindingProperties.getExtendedConsumerProperties("process-in-0"))
.hasFieldOrPropertyWithValue("schemaType", SchemaType.JSON)
.hasFieldOrPropertyWithValue("receiverQueueSize", 8000)
.hasFieldOrPropertyWithValue("subscription.name", "my-subscription")
.hasFieldOrPropertyWithValue("startPaused", true);
assertThat(extendedBindingProperties.getExtendedProducerProperties("process-out-0"))
.hasFieldOrPropertyWithValue("schemaType", SchemaType.JSON)
.hasFieldOrPropertyWithValue("blockIfQueueFull", false)
.hasFieldOrPropertyWithValue("maxPendingMessages", 400)
.hasFieldOrPropertyWithValue("name", "my-producer");
});
}


@EnableAutoConfiguration
static class DefaultPropertiesTestApp {

@Bean
public Function<String, String> process() {
return s -> s;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ public void onApplicationEvent(ApplicationEvent event) {

if (refresh) {
binderProducingContext.refresh();
if (this.context != null) {
onobc marked this conversation as resolved.
Show resolved Hide resolved
this.context.getBeanFactory().registerSingleton(configurationName + "_binderProducingContext",
binderProducingContext);
}
if (!useApplicationContextAsParent || "integration".equals(binderType.getDefaultName())) {
this.propagateSharedBeans((GenericApplicationContext) this.context, binderProducingContext);
}
Expand Down