Skip to content

Commit adddeff

Browse files
committed
GH-3071 Add support for accessing producer/consumer properties
1 parent fcd7cba commit adddeff

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractMessageChannelBinder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,14 @@ public Map<String, Object> getExtendedInfo() {
368368
return doGetExtendedInfo(destination, producerProperties);
369369
}
370370

371+
@SuppressWarnings({ "unchecked", "hiding" })
372+
public <P> P getExtension() {
373+
if (producerProperties instanceof ExtendedProducerProperties extendedProperties) {
374+
return (P) extendedProperties.getExtension();
375+
}
376+
return null;
377+
}
378+
371379
@Override
372380
public boolean isInput() {
373381
return false;
@@ -551,6 +559,14 @@ public Map<String, Object> getExtendedInfo() {
551559
return doGetExtendedInfo(destination, properties);
552560
}
553561

562+
@SuppressWarnings({ "unchecked", "hiding" })
563+
public <P> P getExtension() {
564+
if (properties instanceof ExtendedConsumerProperties extendedProperties) {
565+
return (P) extendedProperties.getExtension();
566+
}
567+
return null;
568+
}
569+
554570
@Override
555571
public boolean isInput() {
556572
return true;

core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/Binding.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 the original author or authors.
2+
* Copyright 2016-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,15 @@ default Map<String, Object> getExtendedInfo() {
4141
return Collections.emptyMap();
4242
}
4343

44+
/**
45+
* Will return producer or consumer properties of this binding.
46+
* @param <P> type of producer or consumer properties
47+
* @return producer or consumer properties of this binding
48+
*/
49+
default <P> P getExtension() {
50+
return null;
51+
}
52+
4453
/**
4554
* Starts the target component represented by this instance. NOTE: At the time the
4655
* instance is created the component is already started. This operation is typically

core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BindingsLifecycleController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.cloud.stream.binder.Binding;
3232
import org.springframework.util.Assert;
3333
import org.springframework.util.ClassUtils;
34+
import org.springframework.util.CollectionUtils;
3435

3536
/**
3637
*
@@ -52,6 +53,7 @@ public class BindingsLifecycleController {
5253
@SuppressWarnings("unchecked")
5354
public BindingsLifecycleController(List<InputBindingLifecycle> inputBindingLifecycles,
5455
List<OutputBindingLifecycle> outputBindingsLifecycles) {
56+
5557
Assert.notEmpty(inputBindingLifecycles,
5658
"'inputBindingLifecycles' must not be null or empty");
5759
this.inputBindingLifecycles = inputBindingLifecycles;
@@ -72,6 +74,21 @@ public BindingsLifecycleController(List<InputBindingLifecycle> inputBindingLifec
7274
}
7375
}
7476

77+
/**
78+
* Will return producer or consumer properties for a specified binding. For example, calling `getExtensionProperties("foo-in-0")`
79+
* on Kafka binding will return an instance of KafkaConsumerProperties.
80+
* @param <T> type of producer or consumer properties for a specified binding
81+
* @param bindingName name of the binding
82+
* @return producer or consumer properties
83+
*/
84+
public <T> T getExtensionProperties(String bindingName) {
85+
List<Binding<?>> locateBinding = BindingsLifecycleController.this.locateBinding(bindingName);
86+
if (!CollectionUtils.isEmpty(locateBinding)) {
87+
return locateBinding.get(0).getExtension();
88+
}
89+
return null;
90+
}
91+
7592
/**
7693
* Provide an accessor for the custom ObjectMapper created by this controller.
7794
* @return {@link ObjectMapper}

0 commit comments

Comments
 (0)